Skip to content

Commit 6d993ab

Browse files
committed
colorization
1 parent c56a0d4 commit 6d993ab

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

cmd/power.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/MrZoidberg/megarac/api"
88
"github.com/MrZoidberg/megarac/lgr"
9+
"github.com/fatih/color"
910
"github.com/urfave/cli/v2"
1011
)
1112

@@ -42,7 +43,7 @@ func PowerOn(c *cli.Context) error {
4243
}
4344

4445
if format == OutputFormatText {
45-
lgr.Logger.Logf("[INFO] Server %s is powering on", profile.Host)
46+
lgr.Logger.Logf("[INFO] Server %s is powering %s", profile.Host, lgr.ColorFormat(color.FgGreen, "on"))
4647
} else {
4748
result := map[string]interface{}{
4849
"host": profile.Host,
@@ -94,7 +95,7 @@ func PowerOff(c *cli.Context) error {
9495
}
9596

9697
if format == OutputFormatText {
97-
lgr.Logger.Logf("[INFO] Server %s is powering off", profile.Host)
98+
lgr.Logger.Logf("[INFO] Server %s is powering %s", profile.Host, lgr.ColorFormat(color.FgRed, "off"))
9899
} else {
99100
result := map[string]interface{}{
100101
"host": profile.Host,
@@ -152,7 +153,13 @@ func PowerStatus(c *cli.Context) error {
152153
}
153154

154155
if format == OutputFormatText {
155-
lgr.Logger.Logf("[INFO] Power status for %s: %v", profile.Host, powerStatus)
156+
powerStatusStr := ""
157+
if powerStatus == "on" {
158+
powerStatusStr = lgr.ColorFormat(color.FgGreen, powerStatus)
159+
} else {
160+
powerStatusStr = lgr.ColorFormat(color.FgRed, powerStatus)
161+
}
162+
lgr.Logger.Logf("[INFO] Power status for %s: %v", profile.Host, powerStatusStr)
156163
} else {
157164
result := map[string]interface{}{
158165
"host": profile.Host,

cmd/sensor.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/MrZoidberg/megarac/api"
1010
"github.com/MrZoidberg/megarac/lgr"
11+
"github.com/fatih/color"
1112
"github.com/urfave/cli/v2"
1213
)
1314

@@ -61,12 +62,14 @@ func SensorList(c *cli.Context) error {
6162
if !showAll && sensor.State == "inactive" || sensor.Accessible == "inaccessible" {
6263
continue
6364
}
65+
6466
if find && !strings.Contains(sensor.Name, find_str) {
6567
continue
6668
}
6769

6870
fmt.Fprintf(w, "%d\t%s\t%s\t%s %s\t%s\t%s\n", sensor.ID, sensor.Name, sensor.Type,
69-
sensor.Reading, sensor.Unit, sensor.Alert, sensor.State)
71+
colorizeReading(sensor.Reading, sensor.Alert, sensor.State), sensor.Unit,
72+
colorizeAlert(sensor.Alert), colorizeState(sensor.State))
7073
}
7174
w.Flush()
7275

@@ -94,3 +97,35 @@ func SensorList(c *cli.Context) error {
9497
}
9598
return nil
9699
}
100+
101+
func colorizeAlert(alert string) string {
102+
if len(alert) > 0 {
103+
return lgr.ColorFormat(color.FgRed, alert)
104+
}
105+
return "\t"
106+
}
107+
108+
func colorizeReading(reading string, alert string, state string) string {
109+
if len(alert) > 0 {
110+
return lgr.ColorFormat(color.FgRed, reading)
111+
}
112+
switch state {
113+
case "inactive":
114+
return lgr.ColorFormat(color.FgWhite, reading)
115+
case "active":
116+
return lgr.ColorFormat(color.FgGreen, reading)
117+
default:
118+
return reading
119+
}
120+
}
121+
122+
func colorizeState(state string) string {
123+
switch state {
124+
case "inactive":
125+
return lgr.ColorFormat(color.FgRed, state)
126+
case "active":
127+
return lgr.ColorFormat(color.FgGreen, state)
128+
default:
129+
return state
130+
}
131+
}

lgr/logger.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
var Logger lgr.L
1111

12+
// SetupLog sets up logger
1213
func SetupLog() {
1314
logOpts := []lgr.Option{lgr.Format("{{.Message}}")} // default to discard
1415
dbg := os.Getenv("DEBUG") == "true"
@@ -29,3 +30,8 @@ func SetupLog() {
2930

3031
Logger = lgr.Std
3132
}
33+
34+
// ColorFormat returns formatted string with color
35+
func ColorFormat(clr color.Attribute, format string, args ...interface{}) string {
36+
return color.New(clr).Sprintf(format, args...)
37+
}

0 commit comments

Comments
 (0)