Skip to content

Commit 2220ea8

Browse files
colour now support colour modifier
1 parent b712ca1 commit 2220ea8

File tree

5 files changed

+68
-48
lines changed

5 files changed

+68
-48
lines changed

pkg/plugin/resources.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (s *resource) BuildBranch(info BuilderInformation, rows [][]Cell) ([]Cell,
218218
rowOut[3].colour = setColourValue(int(val))
219219
}
220220

221-
usedColour := 0
221+
usedColour := [2]int{0, 0}
222222
if rowOut[3].float > rowOut[4].float {
223223
usedColour = setColourValue(int(rowOut[4].float))
224224
} else {
@@ -251,11 +251,12 @@ func (s *resource) statsProcessTableRow(res v1.ResourceRequirements, metrics v1.
251251
var rawRequest, rawLimit, rawValue int64
252252
var rawPercentRequest, rawPercentLimit float64
253253
var requestCell, limitCell Cell
254-
var percentRequestColour, percentLimitColour int
255254

256255
log := logger{location: "resources:statsProcessTableRow"}
257256
log.Debug("Start")
258257

258+
percentRequestColour := [2]int{-1, 0}
259+
percentLimitColour := [2]int{-1, 0}
259260
floatfmt := "%.6f"
260261

261262
if resource == "cpu" {
@@ -349,7 +350,7 @@ func (s *resource) statsProcessTableRow(res v1.ResourceRequirements, metrics v1.
349350
if res.Limits.Memory().AsApproximateFloat64() == 0 {
350351
percentLimit = "-"
351352
rawPercentLimit = 0.0
352-
percentLimitColour = -1
353+
percentLimitColour = [2]int{-1, 0}
353354
} else {
354355
val := validateFloat64(memVal / res.Limits.Memory().AsApproximateFloat64() * 100)
355356
percentLimit = fmt.Sprintf(floatfmt, val)
@@ -361,7 +362,7 @@ func (s *resource) statsProcessTableRow(res v1.ResourceRequirements, metrics v1.
361362
if res.Requests.Memory().AsApproximateFloat64() == 0 {
362363
percentRequest = "-"
363364
rawPercentRequest = 0.0
364-
percentRequestColour = -1
365+
percentRequestColour = [2]int{-1, 0}
365366
} else {
366367
val := validateFloat64(memVal / res.Requests.Memory().AsApproximateFloat64() * 100)
367368
percentRequest = fmt.Sprintf(floatfmt, val)
@@ -373,8 +374,8 @@ func (s *resource) statsProcessTableRow(res v1.ResourceRequirements, metrics v1.
373374
}
374375
}
375376

376-
usedColour := 0
377-
if percentLimitColour > percentRequestColour {
377+
usedColour := [2]int{-1, 0}
378+
if percentLimitColour[0] > percentRequestColour[0] {
378379
usedColour = percentLimitColour
379380
} else {
380381
usedColour = percentRequestColour

pkg/plugin/status.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func Status(cmd *cobra.Command, kubeFlags *genericclioptions.ConfigFlags, args [
9393
}
9494

9595
table := Table{}
96+
table.ColourOutput = commonFlagList.outputAsColour
97+
9698
builder.Table = &table
9799
log.Debug("commonFlagList.showTreeView =", commonFlagList.showTreeView)
98100
builder.ShowTreeView = commonFlagList.showTreeView
@@ -201,20 +203,20 @@ func (s *status) BuildBranch(info BuilderInformation, rows [][]Cell) ([]Cell, er
201203
// rowOut[10] // message
202204

203205
rowOut[0].text = "true"
204-
rowOut[0].colour = colourOk
206+
rowOut[0].colour = [2]int{colourOk, colourModOk}
205207
rowOut[1].text = "true"
206-
rowOut[1].colour = colourOk
208+
rowOut[1].colour = [2]int{colourOk, colourModOk}
207209

208210
// loop through each row in podTotals and add the columns in each row
209211
for _, r := range rows {
210212
if r[0].text == "false" {
211213
// ready = false
212214
rowOut[0].text = "false" // ready
213-
rowOut[0].colour = colourBad
215+
rowOut[0].colour = [2]int{colourBad, colourModBad}
214216
}
215217
if r[1].text == "false" {
216218
rowOut[1].text = "false" // started
217-
rowOut[1].colour = colourBad
219+
rowOut[1].colour = [2]int{colourBad, colourModBad}
218220
}
219221
rowOut[2].number += r[2].number // restarts
220222

@@ -230,7 +232,7 @@ func (s *status) BuildBranch(info BuilderInformation, rows [][]Cell) ([]Cell, er
230232
rowOut[3].text = string(info.Data.pod.Status.Phase) // state
231233
} else {
232234
rowOut[3].text = "Terminating" // state
233-
rowOut[3].colour = colourWarn
235+
rowOut[3].colour = [2]int{colourWarn, colourModWarn}
234236
}
235237
rowOut[4].text = info.Data.pod.Status.Reason // reason
236238
rowOut[8].text = info.Data.pod.CreationTimestamp.Format(timestampFormat) // timestamp
@@ -255,12 +257,14 @@ func (s *status) BuildContainerStatus(container v1.ContainerStatus, info Builder
255257
var age string
256258
var state v1.ContainerState
257259
var rawExitCode, rawSignal, rawRestarts int64
258-
var colourcode, readyColour, startColour int
259-
// var id string
260260

261261
log := logger{location: "Status:BuildContainerStatus"}
262262
log.Debug("Start")
263263

264+
colourcode := [2]int{-1, 0}
265+
readyColour := [2]int{-1, 0}
266+
startColour := [2]int{-1, 0}
267+
264268
if s.ShowPrevious {
265269
state = container.LastTerminationState
266270
} else {
@@ -273,7 +277,7 @@ func (s *status) BuildContainerStatus(container v1.ContainerStatus, info Builder
273277
message = state.Waiting.Message
274278
// waiting state dosent have a start time so we skip setting the age variable, used further down
275279
skipAgeCalculation = true
276-
colourcode = colourWarn
280+
colourcode = [2]int{colourWarn, colourModWarn}
277281
}
278282

279283
if state.Terminated != nil {
@@ -288,17 +292,17 @@ func (s *status) BuildContainerStatus(container v1.ContainerStatus, info Builder
288292
message = state.Terminated.Message
289293

290294
if rawExitCode == 0 {
291-
colourcode = colourOk
295+
colourcode = [2]int{colourOk, colourModOk}
292296
} else {
293-
colourcode = colourBad
297+
colourcode = [2]int{colourBad, colourModBad}
294298
}
295299
}
296300

297301
if state.Running != nil {
298302
strState = "Running"
299303
startedAt = state.Running.StartedAt.Format(timestampFormat)
300304
startTime = state.Running.StartedAt.Time
301-
colourcode = colourOk
305+
colourcode = [2]int{colourOk, colourModOk}
302306
}
303307

304308
if container.Started != nil {

pkg/plugin/table.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Cell struct {
2525
typ int // 0=string, 1=int64, 2=float64, 3=placeholder
2626
phRef int // placeholder reference id, used to track the row thats used as a placeholder
2727
indent int // the number of indents required in the output
28-
colour int
28+
colour [2]int
2929
}
3030

3131
type Table struct {
@@ -175,17 +175,29 @@ func (t *Table) HideOnlyNamedColumns(columnName []string) error {
175175

176176
// Print outputs the table on the terminal, taking the column order and visibiliy into account
177177
func (t *Table) Print() {
178-
var cellcolour int
178+
var cellcolour [2]int
179179
var withColour bool
180180
var visibleColumns int
181181
headLine := ""
182-
colourArray := make([]int, t.headCount)
182+
colourArray := make([][2]int, t.headCount)
183183

184184
if t.ColourOutput != COLOUR_NONE {
185185
withColour = true
186186

187+
maxColours := 7
188+
modFlip := 0
189+
187190
for i := 0; i < t.headCount; i++ {
188-
colourArray[i] = int(math.Mod(float64(i), float64(7))) + 30
191+
colourCode := int(math.Mod(float64(i), float64(maxColours)))
192+
colourArray[i][0] = colourCode + 30
193+
colourArray[i][1] = modFlip
194+
195+
if colourCode >= maxColours-1 {
196+
modFlip += 1
197+
if modFlip > 1 {
198+
modFlip = 0
199+
}
200+
}
189201
}
190202
}
191203

@@ -208,7 +220,7 @@ func (t *Table) Print() {
208220
}
209221

210222
if t.ColourOutput == COLOUR_MIX || t.ColourOutput == COLOUR_COLUMNS {
211-
word = fmt.Sprintf("\033[%dm%s%s", cellcolour, word, colourEnd)
223+
word = fmt.Sprintf("\033[%d;%dm%s%s", cellcolour[1], cellcolour[0], word, colourEnd)
212224
}
213225
pad := strings.Repeat(" ", t.head[idx].columnLength-runelen)
214226

@@ -263,16 +275,16 @@ func (t *Table) Print() {
263275
// colour output has been set and the cell has data
264276
if withColour {
265277
if t.ColourOutput == COLOUR_MIX || t.ColourOutput == COLOUR_COLUMNS {
266-
celltxt = fmt.Sprintf("\033[%dm%s%s", cellcolour, origtxt, colourEnd)
278+
celltxt = fmt.Sprintf("\033[%d;%dm%s%s", cellcolour[1], cellcolour[0], origtxt, colourEnd)
267279
}
268280

269281
// we check for errors last so it can overwrite the column colours when we are using the mix colour set
270-
if cell.colour > -1 && (t.ColourOutput == COLOUR_ERRORS || t.ColourOutput == COLOUR_MIX) {
282+
if cell.colour[0] > -1 && (t.ColourOutput == COLOUR_ERRORS || t.ColourOutput == COLOUR_MIX) {
271283
// error colour set uses red/yellow/green for ok/warning/problem
272-
if cell.colour == 0 && t.ColourOutput == COLOUR_MIX {
273-
celltxt = fmt.Sprintf("\033[%dm%s%s", cellcolour, origtxt, colourEnd)
284+
if cell.colour[0] == 0 && t.ColourOutput == COLOUR_MIX {
285+
celltxt = fmt.Sprintf("\033[%d;%dm%s%s", cellcolour[1], cellcolour[0], origtxt, colourEnd)
274286
} else {
275-
celltxt = fmt.Sprintf("\033[%dm%s%s", cell.colour, origtxt, colourEnd)
287+
celltxt = fmt.Sprintf("\033[%d;%dm%s%s", cell.colour[1], cell.colour[0], origtxt, colourEnd)
276288
}
277289
}
278290
}
@@ -580,7 +592,7 @@ func strMatch(str string, pattern string) bool {
580592
func NewCellEmpty() Cell {
581593
return Cell{
582594
typ: -1,
583-
colour: -1,
595+
colour: [2]int{-1, 0},
584596
}
585597
}
586598

@@ -594,7 +606,7 @@ func NewCellText(text string) Cell {
594606

595607
return Cell{
596608
text: temp,
597-
colour: -1,
609+
colour: [2]int{-1, 0},
598610
}
599611
}
600612

@@ -611,7 +623,7 @@ func NewCellTextIndent(text string, indentLevel int) Cell {
611623
return Cell{
612624
text: temp,
613625
indent: indentLevel,
614-
colour: -1,
626+
colour: [2]int{-1, 0},
615627
}
616628
}
617629

@@ -621,7 +633,7 @@ func NewCellInt(text string, value int64) Cell {
621633
text: text,
622634
number: value,
623635
typ: 1,
624-
colour: -1,
636+
colour: [2]int{-1, 0},
625637
}
626638
}
627639

@@ -631,12 +643,12 @@ func NewCellFloat(text string, value float64) Cell {
631643
text: text,
632644
float: value,
633645
typ: 2,
634-
colour: -1,
646+
colour: [2]int{-1, 0},
635647
}
636648
}
637649

638650
// NewCellColourText quick wrapper to return a cell object containing the given string and the colour to be used
639-
func NewCellColourText(colour int, text string) Cell {
651+
func NewCellColourText(colour [2]int, text string) Cell {
640652

641653
temp := strings.Replace(text, "\r", "\\r", -1)
642654
temp = strings.Replace(temp, "\f", "\\f", -1)
@@ -650,7 +662,7 @@ func NewCellColourText(colour int, text string) Cell {
650662
}
651663

652664
// NewCellColorInt quick wrapper to return a cell object containing the given colour, string and int
653-
func NewCellColourInt(colour int, text string, value int64) Cell {
665+
func NewCellColourInt(colour [2]int, text string, value int64) Cell {
654666
return Cell{
655667
text: text,
656668
number: value,
@@ -660,7 +672,7 @@ func NewCellColourInt(colour int, text string, value int64) Cell {
660672
}
661673

662674
// NewCellFloat quick wrapper to return a cell object containing the given colour, string and float
663-
func NewCellColourFloat(colour int, text string, value float64) Cell {
675+
func NewCellColourFloat(colour [2]int, text string, value float64) Cell {
664676
return Cell{
665677
text: text,
666678
float: value,

pkg/plugin/table_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ type addRowTest struct {
5050
}
5151

5252
var addRowTests = []addRowTest{
53-
{[]Cell{NewCellText("one")}, 1, 5, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, -1}}}},
54-
{[]Cell{NewCellText("two")}, 2, 5, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, -1}}, {Cell{"two", 0, 0, 0, 0, 0, -1}}}},
55-
{[]Cell{NewCellText("three")}, 3, 7, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, -1}}, {Cell{"two", 0, 0, 0, 0, 0, -1}}, {Cell{"three", 0, 0, 0, 0, 0, -1}}}},
56-
{[]Cell{NewCellText("four"), NewCellText("extra"), NewCellText("larger")}, 4, 7, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, -1}}, {Cell{"two", 0, 0, 0, 0, 0, -1}}, {Cell{"three", 0, 0, 0, 0, 0, -1}}, {Cell{"four", 0, 0, 0, 0, 0, -1}, Cell{"extra", 0, 0, 0, 0, 0, -1}, Cell{"larger", 0, 0, 0, 0, 0, -1}}}},
53+
{[]Cell{NewCellText("one")}, 1, 5, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, [2]int{-1, 0}}}}},
54+
{[]Cell{NewCellText("two")}, 2, 5, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, [2]int{-1, 0}}}, {Cell{"two", 0, 0, 0, 0, 0, [2]int{-1, 0}}}}},
55+
{[]Cell{NewCellText("three")}, 3, 7, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, [2]int{-1, 0}}}, {Cell{"two", 0, 0, 0, 0, 0, [2]int{-1, 0}}}, {Cell{"three", 0, 0, 0, 0, 0, [2]int{-1, 0}}}}},
56+
{[]Cell{NewCellText("four"), NewCellText("extra"), NewCellText("larger")}, 4, 7, [][]Cell{{Cell{"one", 0, 0, 0, 0, 0, [2]int{-1, 0}}}, {Cell{"two", 0, 0, 0, 0, 0, [2]int{-1, 0}}}, {Cell{"three", 0, 0, 0, 0, 0, [2]int{-1, 0}}}, {Cell{"four", 0, 0, 0, 0, 0, [2]int{-1, 0}}, Cell{"extra", 0, 0, 0, 0, 0, [2]int{-1, 0}}, Cell{"larger", 0, 0, 0, 0, 0, [2]int{-1, 0}}}}},
5757
}
5858

5959
func TestAddRow(t *testing.T) {

pkg/plugin/utils.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import (
1111
const colourEnd = "\033[0m"
1212
const colourNone = -1
1313
const colourBad = 31
14+
const colourModBad = 0
1415
const colourOk = 32
16+
const colourModOk = 0
1517
const colourWarn = 33
18+
const colourModWarn = 0
1619

1720
// always returns false if the flagList.container is empty as we expect to show all containers
1821
// returns true if we dont have a match
@@ -134,26 +137,26 @@ func portAsString(port intstr.IntOrString) string {
134137
return ""
135138
}
136139

137-
func setColourValue(value int) int {
138-
var colour int
140+
func setColourValue(value int) [2]int {
141+
var colour [2]int
139142

140-
colour = colourOk
143+
colour = [2]int{colourOk, colourModOk}
141144
if value > 90 {
142-
colour = colourBad
145+
colour = [2]int{colourBad, colourModBad}
143146
} else if value > 75 {
144-
colour = colourWarn
147+
colour = [2]int{colourWarn, colourModWarn}
145148
}
146149

147150
return colour
148151
}
149152

150-
func setColourBoolean(value bool) int {
151-
var colour int
153+
func setColourBoolean(value bool) [2]int {
154+
var colour [2]int
152155

153156
if value {
154-
colour = colourOk
157+
colour = [2]int{colourOk, colourModOk}
155158
} else {
156-
colour = colourBad
159+
colour = [2]int{colourBad, colourModBad}
157160
}
158161

159162
return colour

0 commit comments

Comments
 (0)