From 357050207e10bc4cddf20f12bd7072186e7a1d82 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Sat, 1 Dec 2018 20:51:51 -0800 Subject: [PATCH] Refactor inline celsius conversion into a function --- src/utils/utils.go | 4 ++++ src/widgets/temp_darwin.go | 8 ++++++-- src/widgets/temp_linux.go | 3 ++- src/widgets/temp_windows.go | 3 ++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/utils/utils.go b/src/utils/utils.go index 6c9ae36..f3e11ef 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -14,6 +14,10 @@ var ( TB = uint64(math.Pow(2, 40)) ) +func CelsiusToFahrenheit(c int) int { + return c*9/5 + 32 +} + func BytesToKB(b uint64) float64 { return float64(b) / float64(KB) } diff --git a/src/widgets/temp_darwin.go b/src/widgets/temp_darwin.go index f4a7d09..b71a5c5 100644 --- a/src/widgets/temp_darwin.go +++ b/src/widgets/temp_darwin.go @@ -4,7 +4,11 @@ package widgets // #cgo LDFLAGS: -framework IOKit // #include "include/smc.c" -import "C" +import ( + "C" + + "github.com/cjbassi/gotop/src/utils" +} type TemperatureStat struct { SensorKey string `json:"sensorKey"` @@ -54,7 +58,7 @@ func (self *Temp) update() { for _, sensor := range sensors { if sensor.Temperature != 0 { if self.Fahrenheit { - self.Data[sensor.SensorKey] = int(sensor.Temperature*9/5 + 32) + self.Data[sensor.SensorKey] = utils.CelsiusToFahrenheit(int(sensor.Temperature)) } else { self.Data[sensor.SensorKey] = int(sensor.Temperature) } diff --git a/src/widgets/temp_linux.go b/src/widgets/temp_linux.go index 38e3cce..40c1978 100644 --- a/src/widgets/temp_linux.go +++ b/src/widgets/temp_linux.go @@ -3,6 +3,7 @@ package widgets import ( "strings" + "github.com/cjbassi/gotop/src/utils" psHost "github.com/shirou/gopsutil/host" ) @@ -14,7 +15,7 @@ func (self *Temp) update() { // removes '_input' from the end of the sensor name label := sensor.SensorKey[:strings.Index(sensor.SensorKey, "_input")] if self.Fahrenheit { - self.Data[label] = int(sensor.Temperature*9/5 + 32) + self.Data[label] = utils.CelsiusToFahrenheit(int(sensor.Temperature)) } else { self.Data[label] = int(sensor.Temperature) } diff --git a/src/widgets/temp_windows.go b/src/widgets/temp_windows.go index 0d3f6c3..7ac35d5 100644 --- a/src/widgets/temp_windows.go +++ b/src/widgets/temp_windows.go @@ -1,6 +1,7 @@ package widgets import ( + "github.com/cjbassi/gotop/src/utils" psHost "github.com/shirou/gopsutil/host" ) @@ -9,7 +10,7 @@ func (self *Temp) update() { for _, sensor := range sensors { if sensor.Temperature != 0 { if self.Fahrenheit { - self.Data[sensor.SensorKey] = int(sensor.Temperature*9/5 + 32) + self.Data[sensor.SensorKey] = utils.CelsiusToFahrenheit(int(sensor.Temperature)) } else { self.Data[sensor.SensorKey] = int(sensor.Temperature) }