|
8 | 8 |
|
9 | 9 | "github.com/MrZoidberg/megarac/api" |
10 | 10 | "github.com/MrZoidberg/megarac/lgr" |
| 11 | + "github.com/fatih/color" |
11 | 12 | "github.com/urfave/cli/v2" |
12 | 13 | ) |
13 | 14 |
|
@@ -61,12 +62,14 @@ func SensorList(c *cli.Context) error { |
61 | 62 | if !showAll && sensor.State == "inactive" || sensor.Accessible == "inaccessible" { |
62 | 63 | continue |
63 | 64 | } |
| 65 | + |
64 | 66 | if find && !strings.Contains(sensor.Name, find_str) { |
65 | 67 | continue |
66 | 68 | } |
67 | 69 |
|
68 | 70 | 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)) |
70 | 73 | } |
71 | 74 | w.Flush() |
72 | 75 |
|
@@ -94,3 +97,35 @@ func SensorList(c *cli.Context) error { |
94 | 97 | } |
95 | 98 | return nil |
96 | 99 | } |
| 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 | +} |
0 commit comments