Skip to content

Commit f9dd0e5

Browse files
committed
Better UI
1 parent a40daf1 commit f9dd0e5

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

cool.go

+57-18
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,29 @@ import (
1818
"github.com/olekukonko/ts"
1919
)
2020

21+
// TODO: auto scrolling
22+
2123
var (
2224
version = "dev"
23-
helpMsg = ``
24-
chart = true
25+
helpMsg = `Cool - Never let the heat slow your Mac down
26+
Usage: cool [-c/--no-chart] [<temperature>]
27+
cool [-h/--help | -v/--version]`
28+
chart = true
29+
defaultTemp = 75.0
2530
)
2631

2732
func main() {
33+
if hasOption, i := argsHaveOption("no-chart", "c"); hasOption {
34+
chart = false
35+
os.Args = removeKeepOrder(os.Args, i)
36+
main()
37+
return
38+
}
39+
if len(os.Args) > 2 {
40+
handleErrStr("Too many arguments")
41+
fmt.Println(helpMsg)
42+
return
43+
}
2844
if hasOption, _ := argsHaveOption("help", "h"); hasOption {
2945
fmt.Println(helpMsg)
3046
return
@@ -33,12 +49,6 @@ func main() {
3349
fmt.Println("Cool " + version)
3450
return
3551
}
36-
if hasOption, i := argsHaveOption("no-chart", "c"); hasOption {
37-
chart = false
38-
os.Args = removeKeepOrder(os.Args, i)
39-
main()
40-
return
41-
}
4252
currentUser, err := user.Current()
4353
if err != nil {
4454
handleErr(err)
@@ -48,7 +58,7 @@ func main() {
4858
return
4959
}
5060
if len(os.Args) == 1 {
51-
cool(75)
61+
cool(defaultTemp)
5262
}
5363
temp, err := strconv.ParseFloat(os.Args[1], 32)
5464
if err != nil {
@@ -69,49 +79,74 @@ func cool(target float64) {
6979
setupInterrupt()
7080
var (
7181
speed int
72-
tplot []float64
73-
splot = []float64{1200} // start at default
7482
termsize ts.Size
7583
temp = getTemp()
7684
timeTaken = ""
7785
alreadyReachedTarget = false
7886
green = color.New(color.FgHiGreen)
87+
cyan = color.New(color.FgCyan)
88+
yellow = color.New(color.FgHiYellow)
7989
start = time.Now()
90+
tplot []float64
91+
splot = []float64{float64(getFanSpeed())} // start at current because we'll change it soon
92+
lastTemp = temp
93+
arrLengthLim = 1000 // we'll keep an array limit so the values "scroll"
8094
)
8195

8296
setFanSpeed(1200 + int(math.Round(150*(temp-target)))) // quickly set it at the start
8397
for ; ; time.Sleep(time.Second * 2) { // fine tuning
8498
speed = getFanSpeed()
99+
lastTemp = temp
85100
temp = getTemp()
86101

87102
if chart {
88103
termsize, _ = ts.GetSize()
89104
termenv.ClearScreen()
90-
fmt.Println("Target", color.YellowString("%v °C", target), timeTaken)
105+
fmt.Println("Target", color.HiGreenString("%v °C", target), timeTaken)
91106
// fmt.Println()
92107

93108
tplot = append(tplot, temp)
109+
if len(tplot) > arrLengthLim {
110+
tplot = tplot[len(tplot)-arrLengthLim:] // cut off the front so we have max 100 vals
111+
}
94112
fmt.Println(ag.Plot(tplot, ag.Height((termsize.Row()/2)-2-2), ag.Width(termsize.Col()-7), ag.Caption("Temperature (C)")))
95113

96114
splot = append(splot, float64(speed))
115+
if len(splot) > arrLengthLim {
116+
splot = splot[len(splot)-arrLengthLim:]
117+
}
97118
fmt.Println(ag.Plot(splot, ag.Height((termsize.Row()/2)-2-2), ag.Width(termsize.Col()-7), ag.Offset(4), ag.Caption("Fan speed (RPM)")))
98119

99-
fmt.Printf("Now at %v, %v RPM\n", color.YellowString("%.1f °C", temp), speed)
100120
if math.Round(target) == math.Round(temp) { // nolint
101-
green.Print("At target!")
121+
green.Print("·")
122+
fmt.Println(" At target!")
102123
if !alreadyReachedTarget {
103124
timeTaken = "reached in " + color.HiGreenString(time.Since(start).Round(time.Second).String())
104125
alreadyReachedTarget = true
105126
}
106127
} else if target > temp {
107-
color.New(color.FgCyan).Print("Cooler than target!")
128+
cyan.Print("↓")
129+
fmt.Println(" Cooler than target!")
130+
} else {
131+
yellow.Print("↑")
132+
fmt.Println(" Hotter than target")
133+
}
134+
135+
if lastTemp == temp { // nolint
136+
green.Print("·")
137+
fmt.Println(" Temperature is stable")
138+
} else if lastTemp > temp {
139+
cyan.Print("↓")
140+
fmt.Println(" Temperature is decreasing")
108141
} else {
109-
color.New(color.FgRed).Print("Hotter than target")
142+
yellow.Print("↑")
143+
fmt.Println(" Temperature is increasing")
110144
}
145+
fmt.Printf("Now at %.1f °C %v RPM %v\n", temp, speed, time.Since(start).Round(time.Second))
111146
} else {
112-
fmt.Printf("%v %8v RPM\n", color.YellowString("%.1f °C", temp), speed)
147+
fmt.Printf("Now at %.1f °C %v RPM", temp, speed)
113148
}
114-
setFanSpeed(speed + int(math.Round(temp-float64(target)))) // set current to current + the difference in temps. This will automatically correct when temp is too low.
149+
setFanSpeed(speed + int(math.Round(temp-target))) // set current to current + the difference in temps. This will automatically correct when temp is too low.
115150
}
116151
}
117152

@@ -191,3 +226,7 @@ func handleErrStr(str string) {
191226
func removeKeepOrder(s []string, i int) []string {
192227
return append(s[:i], s[i+1:]...)
193228
}
229+
230+
func removefirstn(s []float64, n int) []float64 {
231+
return s[n:] // 0, 1, 2, 3, 4
232+
}

0 commit comments

Comments
 (0)