Skip to content

Commit

Permalink
* Make sure to avoid displaying near-zero values
Browse files Browse the repository at this point in the history
* Initialize table in cmd instead of smc/hid code
  • Loading branch information
dkorunic committed Apr 7, 2022
1 parent ffd64c6 commit f249c32
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/batt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var battCmd = &cobra.Command{
t := table.NewWriter()
defer t.Render()

setupTable(t)
fmt.Println("Battery:")

smc.PrintBatt(t)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/curr.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var currCmd = &cobra.Command{
t := table.NewWriter()
defer t.Render()

setupTable(t)
fmt.Println("Current:")

smc.PrintCurrent(t)
hid.PrintCurrent(t)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/fans.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var fansCmd = &cobra.Command{
t := table.NewWriter()
defer t.Render()

setupTable(t)
fmt.Println("Fans:")

smc.PrintFans(t)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var powerCmd = &cobra.Command{
t := table.NewWriter()
defer t.Render()

setupTable(t)
fmt.Println("Power:")

smc.PrintPower(t)
},
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Dinko Korunic
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"os"

"github.com/jedib0t/go-pretty/table"
)

var sensorOutputHeader = table.Row{"Description", "Key", "Value", "Type"} // row header definition

func setupTable(t table.Writer) {
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(sensorOutputHeader)
}
2 changes: 1 addition & 1 deletion cmd/temp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var tempCmd = &cobra.Command{
t := table.NewWriter()
defer t.Render()

setupTable(t)
fmt.Println("Temperature:")

smc.PrintTemp(t)
hid.PrintTemp(t)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/volt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var voltCmd = &cobra.Command{
t := table.NewWriter()
defer t.Render()

setupTable(t)
fmt.Println("Voltage:")

smc.PrintVoltage(t)
hid.PrintVoltage(t)
},
Expand Down
5 changes: 3 additions & 2 deletions hid/hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"C"
"bufio"
"fmt"
"math"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -54,7 +55,7 @@ func printGeneric(t table.Writer, unit string, cStr *C.char) {
continue
}

if val != -127.0 && val != 0.0 {
if val != -127.0 && val != 0.0 && math.Round(float64(val)*100)/100 != 0.0 {
if val < 0.0 {
val = -val
}
Expand All @@ -76,7 +77,7 @@ func printGeneric(t table.Writer, unit string, cStr *C.char) {
t.AppendRow([]interface{}{
name,
"",
fmt.Sprintf("%6.1f %s", val, unit),
fmt.Sprintf("%7.2f %s", val, unit),
SensorType,
})
}
Expand Down
29 changes: 8 additions & 21 deletions smc/smc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package smc

import (
"fmt"
"math"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -45,8 +46,6 @@ type SensorStat struct {

//go:generate ./gen-sensors.sh sensors.go

var sensorOutputHeader = table.Row{"Description", "Key", "Value", "Type"} // row header definition

// printGeneric prints a table of SMC keys, description and decoded values with units.
func printGeneric(t table.Writer, desc, unit string, smcSlice []SensorStat) {
c, res := gosmc.SMCOpen(AppleSMC)
Expand All @@ -56,10 +55,6 @@ func printGeneric(t table.Writer, desc, unit string, smcSlice []SensorStat) {
}
defer gosmc.SMCClose(c)

t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(sensorOutputHeader)

sort.Slice(smcSlice, func(i, j int) bool { return smcSlice[i].Key < smcSlice[j].Key })

for _, v := range smcSlice {
Expand Down Expand Up @@ -88,15 +83,15 @@ func getKeyAndPrint(t table.Writer, c uint, key string, desc string, unit string
}

// TODO: Do better task at ignoring and reporting invalid/missing values
if val != -127.0 && val != 0.0 {
if val != -127.0 && val != 0.0 && math.Round(float64(val)*100)/100 != 0.0 {
if val < 0.0 {
val = -val
}

t.AppendRow([]interface{}{
desc,
key,
fmt.Sprintf("%6.1f %s", val, unit),
fmt.Sprintf("%7.2f %s", val, unit),
smcType,
})
}
Expand Down Expand Up @@ -131,15 +126,11 @@ func PrintFans(t table.Writer) {
}
defer gosmc.SMCClose(c)

t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(sensorOutputHeader)

val, smcType, _ := getKeyUint32(c, FanNum) // Get number of fans
t.AppendRow([]interface{}{
fmt.Sprintf("%v", "Fan Count"),
FanNum,
fmt.Sprintf("%8v", val),
fmt.Sprintf("%9v", val),
smcType,
})

Expand All @@ -155,7 +146,7 @@ func PrintFans(t table.Writer) {
return
}

if val != -127.0 && val != 0.0 {
if val != -127.0 && val != 0.0 && math.Round(float64(val)*100)/100 != 0.0 {
if val < 0.0 {
val = -val
}
Expand All @@ -181,30 +172,26 @@ func PrintBatt(t table.Writer) {
}
defer gosmc.SMCClose(c)

t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredBright)
t.AppendHeader(sensorOutputHeader)

n, smcType1, _ := getKeyUint32(c, BattNum) // Get number of batteries
i, smcType2, _ := getKeyUint32(c, BattInf) // Get battery info (needs bit decoding)
b, smcType3, _ := getKeyBool(c, BattPwr) // Get AC status

t.AppendRow([]interface{}{
fmt.Sprintf("%v", "Battery Count"),
BattNum,
fmt.Sprintf("%8v", n),
fmt.Sprintf("%9v", n),
smcType1,
})
t.AppendRow([]interface{}{
fmt.Sprintf("%v", "Battery Info"),
BattInf,
fmt.Sprintf("%8v", i),
fmt.Sprintf("%9v", i),
smcType2,
}) // TODO: Needs decoding!
t.AppendRow([]interface{}{
fmt.Sprintf("%v", "Battery Powered"),
BattPwr,
fmt.Sprintf("%8v", b),
fmt.Sprintf("%9v", b),
smcType3,
})
}

0 comments on commit f249c32

Please sign in to comment.