diff --git a/README.md b/README.md index 93db00d..a18205a 100644 --- a/README.md +++ b/README.md @@ -132,10 +132,7 @@ Not in the list: Won't be supported because it's not READ operation ## Other features which currently unsupported but will be done in the future -- [x] make it works with -w option - [ ] Configuring custom colors -- [ ] specifying multiple resources at once (e.g. `kubectl get pod,replicaset`) - - This will actually work, but if you don't specify "--no-headers" it might look a bit strange. ## Supported kubectl version diff --git a/printer/table.go b/printer/table.go index f7bedb1..e173279 100644 --- a/printer/table.go +++ b/printer/table.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "strings" "github.com/dty1er/kubecolor/color" ) @@ -33,7 +34,7 @@ func (tp *TablePrinter) Print(r io.Reader, w io.Writer) { scanner := bufio.NewScanner(r) for scanner.Scan() { line := scanner.Text() - if tp.isHeader() { + if tp.isHeader(line) { fmt.Fprintf(w, "%s\n", color.Apply(line, getHeaderColorByBackground(tp.DarkBackground))) tp.isFirstLine = false continue @@ -43,8 +44,20 @@ func (tp *TablePrinter) Print(r io.Reader, w io.Writer) { } } -func (tp *TablePrinter) isHeader() bool { - return tp.WithHeader && tp.isFirstLine +func (tp *TablePrinter) isHeader(line string) bool { + // If every character is upper case, probably it's a header line. + // e.g. + // kubecolor get pod,rs + // NAME READY STATUS RESTARTS AGE + // pod/nginx-8spn9 1/1 Running 1 19d + // pod/nginx-dplns 1/1 Running 1 19d + // pod/nginx-lpv5x 1/1 Running 1 19d + + // NAME DESIRED CURRENT READY AGE <- this + // replicaset.apps/nginx 3 3 3 19d + // replicaset.apps/nginx-6799fc88d8 3 3 3 19d + isEveryCharacterUpper := strings.ToUpper(line) == line + return (tp.WithHeader && tp.isFirstLine) || isEveryCharacterUpper } // printTableFormat prints a line to w in kubectl "table" Format. diff --git a/printer/table_test.go b/printer/table_test.go index cee66e3..d6f7bd1 100644 --- a/printer/table_test.go +++ b/printer/table_test.go @@ -36,6 +36,32 @@ func Test_TablePrinter_Print(t *testing.T) { nginx-qdf9b 1/1 Running 0 6d6h `), }, + { + name: "multiple headers", + colorDeciderFn: nil, + withHeader: true, + darkBackground: true, + input: testutil.NewHereDoc(` + NAME READY STATUS RESTARTS AGE + pod/nginx-8spn9 1/1 Running 1 19d + pod/nginx-dplns 1/1 Running 1 19d + pod/nginx-lpv5x 1/1 Running 1 19d + + NAME DESIRED CURRENT READY AGE + replicaset.apps/nginx 3 3 3 19d + replicaset.apps/nginx-6799fc88d8 3 3 3 19d + `), + expected: testutil.NewHereDoc(` + NAME READY STATUS RESTARTS AGE + pod/nginx-8spn9 1/1 Running 1 19d + pod/nginx-dplns 1/1 Running 1 19d + pod/nginx-lpv5x 1/1 Running 1 19d +  + NAME DESIRED CURRENT READY AGE + replicaset.apps/nginx 3 3 3 19d + replicaset.apps/nginx-6799fc88d8 3 3 3 19d + `), + }, { name: "withheader=false, 1st line is not colored in header color but colored as a content of table", colorDeciderFn: nil,