Skip to content

Commit b5ad9cd

Browse files
author
Tim Middleton
authored
Monitor formatting enhancements for monitor clusters (#337)
* Monitor formatting enhancements for monitor clusters * Fix minor formatting for services
1 parent aa8d973 commit b5ad9cd

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

pkg/cmd/formatting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ func FormatServices(services []config.ServiceSummary) string {
17121712
table.AddFormattingFunction(8, endangeredPartitionsFormatter)
17131713
table.AddFormattingFunction(9, vulnerablePartitionsFormatter)
17141714
table.AddFormattingFunction(10, vulnerablePartitionsFormatter)
1715-
table.AddFormattingFunction(11, yesBoolFormatter)
1715+
table.AddFormattingFunction(12, yesBoolFormatter)
17161716
} else {
17171717
table.WithAlignment(L, L, R, L, R, R, R, L)
17181718
}

pkg/cmd/formatting_functions.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2025 Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* https://oss.oracle.com/licenses/upl.
55
*/
66

77
package cmd
88

99
import (
10+
"fmt"
1011
"github.com/fatih/color"
1112
"strconv"
1213
"strings"
@@ -17,11 +18,25 @@ var (
1718
yellow = color.New(color.FgHiYellow).SprintFunc()
1819
)
1920

20-
const stopped = "stopped"
21+
const (
22+
stopped = "stopped"
23+
startRed = "\x01"
24+
startYellow = "\x02"
25+
endColor = "\x03"
26+
)
27+
28+
func setupColors() {
29+
red = func(a ...interface{}) string {
30+
return startRed + fmt.Sprint(a...) + endColor
31+
}
32+
yellow = func(a ...interface{}) string {
33+
return startYellow + fmt.Sprint(a...) + endColor
34+
}
35+
}
2136

2237
// statusHAFormatter formats a column value and makes it Red if contains ENDANGERED.
2338
var statusHAFormatter = func(s string) string {
24-
if monitorCluster || isWindows() {
39+
if isWindows() {
2540
return s
2641
}
2742
if strings.Contains(s, endangered) {
@@ -33,7 +48,7 @@ var statusHAFormatter = func(s string) string {
3348

3449
// hitRateFormatter formats a column value which represents a cache hit rate.
3550
var hitRateFormatter = func(s string) string {
36-
if monitorCluster || isWindows() {
51+
if isWindows() {
3752
return s
3853
}
3954
floatValue, err := strconv.ParseFloat(trimPercent(s), 32)
@@ -52,7 +67,7 @@ var hitRateFormatter = func(s string) string {
5267

5368
// machineMemoryFormatting formats a column value which represents machine percent memory used.
5469
var machineMemoryFormatting = func(s string) string {
55-
if monitorCluster || isWindows() {
70+
if isWindows() {
5671
return s
5772
}
5873
floatValue, err := strconv.ParseFloat(trimPercent(s), 32)
@@ -71,7 +86,7 @@ var machineMemoryFormatting = func(s string) string {
7186

7287
// errorFormatter formats a column value which represents an error or number that needs to be highlighted.
7388
var errorFormatter = func(s string) string {
74-
if monitorCluster || isWindows() {
89+
if isWindows() {
7590
return s
7691
}
7792
v, err := getInt64Value(s)
@@ -87,7 +102,7 @@ var errorFormatter = func(s string) string {
87102

88103
// endangeredPartitionsFormatter formats a column value which represents the number of endangered partitions.
89104
var endangeredPartitionsFormatter = func(s string) string {
90-
if monitorCluster || isWindows() {
105+
if isWindows() {
91106
return s
92107
}
93108
v, err := getInt64Value(s)
@@ -100,7 +115,7 @@ var endangeredPartitionsFormatter = func(s string) string {
100115

101116
// vulnerablePartitionsFormatter formats a column value which represents the number of vulnerable or unbalanced partitions.
102117
var vulnerablePartitionsFormatter = func(s string) string {
103-
if monitorCluster || isWindows() {
118+
if isWindows() {
104119
return s
105120
}
106121
v, err := getInt64Value(s)
@@ -113,7 +128,7 @@ var vulnerablePartitionsFormatter = func(s string) string {
113128

114129
// packetFormatter formats a column value which represents packages where higher numbers need to be highlighted.
115130
var packetFormatter = func(s string) string {
116-
if monitorCluster || isWindows() {
131+
if isWindows() {
117132
return s
118133
}
119134
v, err := getInt64Value(s)
@@ -133,7 +148,7 @@ var packetFormatter = func(s string) string {
133148

134149
// healthFormatter formats a column value when false will be displayed in red.
135150
var healthFormatter = func(s string) string {
136-
if monitorCluster || isWindows() {
151+
if isWindows() {
137152
return s
138153
}
139154
if s == stringFalse {
@@ -144,7 +159,7 @@ var healthFormatter = func(s string) string {
144159

145160
// reporterFormatter formats a column value when "Stopped" will be displayed in red.
146161
var reporterFormatter = func(s string) string {
147-
if monitorCluster || isWindows() {
162+
if isWindows() {
148163
return s
149164
}
150165
if strings.Contains(strings.ToLower(s), stopped) {
@@ -155,7 +170,7 @@ var reporterFormatter = func(s string) string {
155170

156171
// trueBoolFormatter formats a column value when true will be displayed in red.
157172
var trueBoolFormatter = func(s string) string {
158-
if monitorCluster || isWindows() {
173+
if isWindows() {
159174
return s
160175
}
161176
if s == stringTrue {
@@ -166,7 +181,7 @@ var trueBoolFormatter = func(s string) string {
166181

167182
// falseBoolFormatter formats a column value when false will be displayed in red.
168183
var falseBoolFormatter = func(s string) string {
169-
if monitorCluster || isWindows() {
184+
if isWindows() {
170185
return s
171186
}
172187
if s == stringFalse {
@@ -177,7 +192,7 @@ var falseBoolFormatter = func(s string) string {
177192

178193
// yesBoolFormatter formats a column value when yes will be displayed in red.
179194
var yesBoolFormatter = func(s string) string {
180-
if monitorCluster || isWindows() {
195+
if isWindows() {
181196
return s
182197
}
183198
if strings.Contains(s, "yes") {
@@ -188,7 +203,7 @@ var yesBoolFormatter = func(s string) string {
188203

189204
// healthSummaryFormatter formats a column value for a health summary.
190205
var healthSummaryFormatter = func(s string) string {
191-
if monitorCluster || isWindows() {
206+
if isWindows() {
192207
return s
193208
}
194209
if !strings.Contains(s, "/") {

pkg/cmd/monitor_cluster.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ Use --show-panels to show all available panels.`,
253253
// set to tru to turn off incompatible color formatting
254254
monitorCluster = true
255255

256+
setupColors()
257+
256258
found, _ := GetClusterConnection(clusterName)
257259
if !found {
258260
return errors.New(UnableToFindClusterMsg + clusterName)
@@ -1588,8 +1590,23 @@ func drawHeader(screen tcell.Screen, w, h int, cluster config.Cluster, dataFetch
15881590
func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
15891591
row := y1
15901592
col := x1
1593+
currentStyle := style
1594+
15911595
for _, r := range text {
1592-
s.SetContent(col, row, r, nil, style)
1596+
switch r {
1597+
case '\x01': // startRed
1598+
currentStyle = currentStyle.Foreground(tcell.ColorRed)
1599+
continue
1600+
case '\x02': // startYellow
1601+
currentStyle = currentStyle.Foreground(tcell.ColorYellow)
1602+
continue
1603+
case '\x03': // endColor
1604+
// Reset to the originally passed style
1605+
currentStyle = style
1606+
continue
1607+
}
1608+
1609+
s.SetContent(col, row, r, nil, currentStyle)
15931610
col++
15941611
if col >= x2 {
15951612
row++

0 commit comments

Comments
 (0)