mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #2127 from mortent/FixColorOutput
Fix printing to make sure always match setting colors with a reset
This commit is contained in:
@@ -19,11 +19,12 @@ const (
|
|||||||
statusColumn = "status"
|
statusColumn = "status"
|
||||||
messageColumn = "message"
|
messageColumn = "message"
|
||||||
|
|
||||||
ESC = 27
|
RESET = 0
|
||||||
RED color = 31
|
ESC = 27
|
||||||
GREEN color = 32
|
RED color = 31
|
||||||
YELLOW color = 33
|
GREEN color = 32
|
||||||
WHITE color = 37
|
YELLOW color = 33
|
||||||
|
DEFAULT color = -1 // This is not a valid ANSI escape code. It is used here to mean that no color should be set.
|
||||||
)
|
)
|
||||||
|
|
||||||
type color int
|
type color int
|
||||||
@@ -36,10 +37,6 @@ func eraseCurrentLine(w io.Writer) {
|
|||||||
printOrDie(w, "%c[2K\r", ESC)
|
printOrDie(w, "%c[2K\r", ESC)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setColor(w io.Writer, color color) {
|
|
||||||
printOrDie(w, "%c[%dm", ESC, color)
|
|
||||||
}
|
|
||||||
|
|
||||||
type colorFunc func(s status.Status) color
|
type colorFunc func(s status.Status) color
|
||||||
type contentFunc func(resource ResourceStatusData) string
|
type contentFunc func(resource ResourceStatusData) string
|
||||||
|
|
||||||
@@ -51,7 +48,7 @@ type tableColumnInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func defaultColorFunc(_ status.Status) color {
|
func defaultColorFunc(_ status.Status) color {
|
||||||
return WHITE
|
return DEFAULT
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -137,7 +134,6 @@ func (s *TablePrinter) Print() {
|
|||||||
|
|
||||||
func (s *TablePrinter) PrintUntil(stop <-chan struct{}, interval time.Duration) <-chan struct{} {
|
func (s *TablePrinter) PrintUntil(stop <-chan struct{}, interval time.Duration) <-chan struct{} {
|
||||||
completed := make(chan struct{})
|
completed := make(chan struct{})
|
||||||
setColor(s.out, WHITE)
|
|
||||||
s.printTable(s.statusInfo.CurrentStatus(), false)
|
s.printTable(s.statusInfo.CurrentStatus(), false)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(completed)
|
defer close(completed)
|
||||||
@@ -167,9 +163,7 @@ func (s *TablePrinter) printTable(data StatusData, deleteUp bool) {
|
|||||||
eraseCurrentLine(s.out)
|
eraseCurrentLine(s.out)
|
||||||
if s.showAggStatus {
|
if s.showAggStatus {
|
||||||
printOrDie(s.out, "AggregateStatus: ")
|
printOrDie(s.out, "AggregateStatus: ")
|
||||||
setColor(s.out, colorForStatus(data.AggregateStatus))
|
printWithColorOrDie(s.out, colorForStatus(data.AggregateStatus), "%s\n", data.AggregateStatus)
|
||||||
printOrDie(s.out, "%s\n", data.AggregateStatus)
|
|
||||||
setColor(s.out, WHITE)
|
|
||||||
}
|
}
|
||||||
s.printTableRow(headers())
|
s.printTableRow(headers())
|
||||||
for _, resource := range data.ResourceStatuses {
|
for _, resource := range data.ResourceStatuses {
|
||||||
@@ -179,13 +173,12 @@ func (s *TablePrinter) printTable(data StatusData, deleteUp bool) {
|
|||||||
|
|
||||||
func (s *TablePrinter) printTableRow(rowData []RowData) {
|
func (s *TablePrinter) printTableRow(rowData []RowData) {
|
||||||
for i, row := range rowData {
|
for i, row := range rowData {
|
||||||
setColor(s.out, row.color)
|
|
||||||
format := fmt.Sprintf("%%-%ds", row.width)
|
format := fmt.Sprintf("%%-%ds", row.width)
|
||||||
printOrDie(s.out, format, trimString(row.content, row.width))
|
printWithColorOrDie(s.out, row.color, format, trimString(row.content, row.width))
|
||||||
if i != len(rowData)-1 {
|
if i != len(rowData)-1 {
|
||||||
printOrDie(s.out, " ")
|
printOrDie(s.out, " ")
|
||||||
}
|
}
|
||||||
setColor(s.out, WHITE)
|
|
||||||
}
|
}
|
||||||
printOrDie(s.out, "\n")
|
printOrDie(s.out, "\n")
|
||||||
}
|
}
|
||||||
@@ -202,7 +195,7 @@ func headers() []RowData {
|
|||||||
column := tableColumns[columnName]
|
column := tableColumns[columnName]
|
||||||
headers = append(headers, RowData{
|
headers = append(headers, RowData{
|
||||||
content: column.header,
|
content: column.header,
|
||||||
color: WHITE,
|
color: DEFAULT,
|
||||||
width: column.width,
|
width: column.width,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -331,18 +324,26 @@ func printOrDie(w io.Writer, format string, a ...interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printWithColorOrDie(w io.Writer, color color, format string, a ...interface{}) {
|
||||||
|
if color == DEFAULT {
|
||||||
|
printOrDie(w, format, a...)
|
||||||
|
} else {
|
||||||
|
printOrDie(w, "%c[%dm", ESC, color)
|
||||||
|
printOrDie(w, format, a...)
|
||||||
|
printOrDie(w, "%c[%dm", ESC, RESET)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func colorForStatus(s status.Status) color {
|
func colorForStatus(s status.Status) color {
|
||||||
switch s {
|
switch s {
|
||||||
case status.CurrentStatus:
|
case status.CurrentStatus:
|
||||||
return GREEN
|
return GREEN
|
||||||
case status.UnknownStatus:
|
|
||||||
return WHITE
|
|
||||||
case status.InProgressStatus:
|
case status.InProgressStatus:
|
||||||
return YELLOW
|
return YELLOW
|
||||||
case status.FailedStatus:
|
case status.FailedStatus:
|
||||||
return RED
|
return RED
|
||||||
}
|
}
|
||||||
return WHITE
|
return DEFAULT
|
||||||
}
|
}
|
||||||
|
|
||||||
func trimString(str string, maxLength int) string {
|
func trimString(str string, maxLength int) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user