abstract terminal interface
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"finally-a-monolithic-linux-hw-monitor/app/common"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -8,6 +9,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const milliCperC = 1000.0
|
||||
|
||||
type HwmonDriver struct {
|
||||
hwmonDir string
|
||||
hwmonDriver string
|
||||
@@ -38,20 +41,20 @@ func NewHwmonDriver(expectedDrivers []string) (*HwmonDriver, error) {
|
||||
return nil, fmt.Errorf("could not find %#v in /sys/class/hwmon", expectedDrivers)
|
||||
}
|
||||
|
||||
func (r *HwmonDriver) Render(console *strings.Builder) error {
|
||||
func (r *HwmonDriver) Render(term common.Terminal) error {
|
||||
t, err := r.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
temps := t.([]TemperatureSensor)
|
||||
term.Subheader("Temperature")
|
||||
|
||||
fmt.Fprint(console, "--- Temperature ---\033[K\n")
|
||||
for _, sensor := range temps {
|
||||
tempC := sensor.TempC
|
||||
tempF := (tempC * 9 / 5) + 32
|
||||
// \033[K clears from cursor to end of line (prevents lingering chars)
|
||||
fmt.Fprintf(console, "%-16s | %.2f °C (%.3f °F)\033[K\n", sensor.Name, tempC, tempF)
|
||||
term.Print(fmt.Sprintf("%-16s | %.2f °C (%.3f °F)", sensor.Name, tempC, tempF))
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -87,7 +90,7 @@ func (r *HwmonDriver) Read() (any, error) {
|
||||
temps = append(temps, TemperatureSensor{
|
||||
path: inputPath,
|
||||
Name: label,
|
||||
TempC: milliC / 1000.0,
|
||||
TempC: milliC / milliCperC,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"finally-a-monolithic-linux-hw-monitor/app/common"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -28,18 +29,18 @@ func NewRaplDriver() (*RaplDriver, error) {
|
||||
return &r, err
|
||||
}
|
||||
|
||||
func (r *RaplDriver) Render(console *strings.Builder) error {
|
||||
fmt.Fprint(console, "--- Power ---\033[K\n")
|
||||
func (r *RaplDriver) Render(term common.Terminal) error {
|
||||
p, err := r.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
power := p.([]PowerSensor)
|
||||
term.Subheader("Power")
|
||||
|
||||
for _, sensor := range power {
|
||||
// \033[K clears from cursor to end of line (prevents lingering chars)
|
||||
fmt.Fprintf(console, "%-16s | %.2f W\033[K\n", sensor.Name, sensor.Watts)
|
||||
term.Print(fmt.Sprintf("%-16s | %.2f W", sensor.Name, sensor.Watts))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"finally-a-monolithic-linux-hw-monitor/app/common"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -17,18 +18,18 @@ func NewSysDriver() (*SysDriver, error) {
|
||||
return &r, err
|
||||
}
|
||||
|
||||
func (r *SysDriver) Render(console *strings.Builder) error {
|
||||
fmt.Fprint(console, "--- Clock Frequency ---\033[K\n")
|
||||
func (r *SysDriver) Render(term common.Terminal) error {
|
||||
c, err := r.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clock := c.([]ClockSensor)
|
||||
term.Subheader("Frequency")
|
||||
|
||||
for _, sensor := range clock {
|
||||
// \033[K clears from cursor to end of line (prevents lingering chars)
|
||||
fmt.Fprintf(console, "%-16s | %.2f GHz\033[K\n", sensor.Name, sensor.FrequencyGHz)
|
||||
term.Print(fmt.Sprintf("%-16s | %.2f GHz", sensor.Name, sensor.FrequencyGHz))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+3
-3
@@ -8,10 +8,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (proc *Proc) RenderProc(console *strings.Builder) {
|
||||
fmt.Fprintf(console, "=== %s ===\033[K\n", proc.Model())
|
||||
func (proc *Proc) Render(term common.Terminal) {
|
||||
term.Header(proc.Model())
|
||||
for _, driver := range proc.drivers {
|
||||
driver.Render(console)
|
||||
driver.Render(term)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user