-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.go
More file actions
158 lines (139 loc) · 3.82 KB
/
Copy pathdisplay.go
File metadata and controls
158 lines (139 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"math"
"strings"
)
// Tokyo Night truecolor palette.
const (
reset = "\033[0m"
bold = "\033[1m"
// Foreground / structural
tnFg = "\033[38;2;192;202;245m" // #c0caf5 soft white
tnComment = "\033[38;2;86;95;137m" // #565f89 muted blue-gray (labels)
tnDim = "\033[38;2;86;95;137m" // #565f89
// Accent colors
tnRed = "\033[38;2;247;118;142m" // #f7768e
tnOrange = "\033[38;2;255;158;100m" // #ff9e64
tnYellow = "\033[38;2;224;175;104m" // #e0af68
tnGreen = "\033[38;2;158;206;106m" // #9ece6a
tnCyan = "\033[38;2;125;207;255m" // #7dcfff
tnBlue = "\033[38;2;122;162;247m" // #7aa2f7
tnPurple = "\033[38;2;187;154;247m" // #bb9af7
)
func printStats(s *Stats, batteryCapacityWh float64) {
gridLabel, gridValue, gridColor := formatGrid(s.GridPower)
printRow("Battery", formatSOC(s.BatterySOC)+" "+tnCyan+formatBatteryPower(s.BatteryPower, batteryCapacityWh)+reset)
for _, b := range s.Batteries {
printRow(fmt.Sprintf("Batt %d", b.Index), formatBatteryDetail(b))
}
printRow("Load", tnFg+formatWatts(s.LoadPower)+reset)
printRow("Solar PV", colorSolar(s.PVPower)+formatWatts(s.PVPower)+reset)
printRow("Grid", gridColor+formatWatts(math.Abs(s.GridPower))+reset+
tnDim+" ("+gridLabel+gridValue+")"+reset)
printRow("Grid V", formatGridVoltage(s.GridVoltage))
if s.InverterTemperature != 0 {
printRow("Inv Temp", formatTemp(s.InverterTemperature))
}
printRow("Output", tnPurple+s.OutputSourcePriority+reset)
printRow("Charger", tnPurple+s.ChargerSourcePriority+reset)
}
func formatBatteryDetail(b Battery) string {
parts := []string{
tnBlue + fmt.Sprintf("%.1f V", b.Voltage) + reset,
tnFg + fmt.Sprintf("%.1f A", b.Current) + reset,
socColor(b.StateOfCharge) + fmt.Sprintf("%.0f%%", b.StateOfCharge) + reset,
}
if b.Temperature != 0 {
parts = append(parts, formatTemp(b.Temperature))
}
return strings.Join(parts, tnDim+" "+reset)
}
func formatTemp(c float64) string {
var color string
switch {
case c < 40:
color = tnGreen
case c < 55:
color = tnYellow
default:
color = tnRed
}
return color + fmt.Sprintf("%.1f°C", c) + reset
}
func printRow(label, value string) {
fmt.Printf(tnComment+"%-12s"+reset+" %s\n", label+":", value)
}
func colorSolar(w float64) string {
if w > 0 {
return tnGreen
}
return tnYellow
}
func formatSOC(pct float64) string {
bar := socBar(pct)
c := socColor(pct)
return fmt.Sprintf("%s%.0f%%%s %s", bold+c, pct, reset, bar)
}
func socColor(pct float64) string {
switch {
case pct >= 60:
return tnGreen
case pct >= 30:
return tnYellow
default:
return tnRed
}
}
func socBar(pct float64) string {
const width = 20
filled := int(math.Round(pct / 100 * width))
if filled > width {
filled = width
}
c := socColor(pct)
bar := ""
for i := 0; i < width; i++ {
if i < filled {
bar += c + "█" + reset
} else {
bar += tnDim + "░" + reset
}
}
return tnDim + "[" + reset + bar + tnDim + "]" + reset
}
func formatGridVoltage(v float64) string {
if v == 0 {
return bold + tnRed + "0 V (power cut)" + reset
}
return tnGreen + fmt.Sprintf("%.1f V", v) + reset
}
func formatBatteryPower(w float64, capacityWh float64) string {
var rate string
if capacityWh > 0 && w != 0 {
pctPerHour := math.Abs(w) / capacityWh * 100
rate = fmt.Sprintf(", "+tnOrange+"%.1f%%/h"+tnCyan, pctPerHour)
}
if w < 0 {
return fmt.Sprintf("(discharging %s%s)", formatWatts(math.Abs(w)), rate)
}
if w > 0 {
return fmt.Sprintf("(charging %s%s)", formatWatts(w), rate)
}
return "(idle)"
}
func formatWatts(w float64) string {
if w >= 1000 {
return fmt.Sprintf("%.2f kW", w/1000)
}
return fmt.Sprintf("%.0f W", w)
}
func formatGrid(w float64) (string, string, string) {
if w < 0 {
return "exporting ", formatWatts(math.Abs(w)), tnGreen
}
if w == 0 {
return "off-grid ", "0 W", tnDim
}
return "importing ", formatWatts(w), tnRed
}