Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
dynamic line update in watch mode
Browse files Browse the repository at this point in the history
Overrides existing lines based on first column, which is
assumed to be an identifier. This greatly simplifies the
output as it's immediately obvious how many resources
exist and what state they are in.
  • Loading branch information
Mikulas committed Dec 23, 2017
1 parent 5cf5e81 commit 9a44cf9
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions main/builtin-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"regexp"
"github.com/k0kubun/go-ansi"
"io/ioutil"
)

var (
Expand All @@ -23,15 +25,41 @@ func (b builtinGet) filter(command string) bool {

func (b builtinGet) run(command string) error {
variableIndex := 0

lines := make(map[string]int, 0)

return shHandler(kubectl(command), func(line string) {
if strings.HasPrefix(line, "NAME ") {
firstColumn := strings.Split(line, " ")[0]
lineOffset, lineDefined := lines[firstColumn]

if lineDefined {
ansi.CursorPreviousLine(lineOffset)
ansi.EraseInLine(2) // clear entire line
fmt.Printf(" \t%s\n", line)

} else {
variableIndex++
key := fmt.Sprintf("%d", variableIndex)
printIndexedLine(key, line)
if strings.HasPrefix(line, "NAME ") {
fmt.Printf(" \t%s\n", line)

} else {
variableIndex++
key := fmt.Sprintf("%d", variableIndex)

// shift existing lines
for k, _ := range lines {
lines[k] += 1
}

lines[firstColumn] = 1
printIndexedLine(key, line)
}
}

ioutil.WriteFile("/dev/ttys012", []byte(fmt.Sprintf("%#v\n", lines)), 777)
ansi.CursorNextLine(999) // reset cursor

// save variable
key := fmt.Sprintf("%d", variableIndex)
variables[key] = strings.Split(line, " ")[0]
variables[key] = firstColumn
})
}

0 comments on commit 9a44cf9

Please sign in to comment.