Refactor inline celsius conversion into a function

This commit is contained in:
Caleb Bassi 2018-12-01 20:51:51 -08:00
parent f66833c2cc
commit 357050207e
4 changed files with 14 additions and 4 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}