diff --git a/TODO.md b/TODO.md index c7dd77f..ef7d40e 100644 --- a/TODO.md +++ b/TODO.md @@ -5,5 +5,3 @@ - add datastream support: https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get-data-stream - -- no color when stdout ! tty diff --git a/pkg/printer/color.go b/pkg/printer/color.go index dab9f79..573d6cf 100644 --- a/pkg/printer/color.go +++ b/pkg/printer/color.go @@ -18,8 +18,11 @@ along with this program. If not, see . package printer import ( + "os" + "codeberg.org/scip/esctl/pkg/cfg" "github.com/fatih/color" + "github.com/mattn/go-isatty" ) var ( @@ -31,6 +34,10 @@ var ( ) func Colorize(conf *cfg.Config, col, what string) string { + if !isatty.IsTerminal(os.Stdout.Fd()) { + return what + } + switch conf.Output { case "json", "yaml": return what @@ -49,3 +56,11 @@ func Colorize(conf *cfg.Config, col, what string) string { return what } + +func Bold(what string) string { + if !isatty.IsTerminal(os.Stdout.Fd()) { + return what + } + + return bold(what) +} diff --git a/pkg/printer/table.go b/pkg/printer/table.go index 240b669..87f3834 100644 --- a/pkg/printer/table.go +++ b/pkg/printer/table.go @@ -155,12 +155,16 @@ func (table *Table) PrintTSV() error { for _, entries := range table.rows { currentWidth := 0 + columns := len(entries) for idx, entry := range entries { length := visibleLen(entry) - if length+currentWidth > table.maxwidth && table.maxwidth-currentWidth > 1 { - // // text is too wide to be put into one line, wrap it + if length+currentWidth > table.maxwidth && + table.maxwidth-currentWidth > 1 && + idx == columns-1 { + // text is too wide to be put into one line, and + // it's the last cell, so wrap it entry = wrap(table.maxwidth-currentWidth, currentWidth+2, entry) } @@ -193,6 +197,10 @@ func (table *Table) PrintTSV() error { // further lines will be indented. Used within Print() to print large // cell text. func wrap(width, indent int, text string) string { + if len(text) <= width { + return text + } + wrapped := "" line := "" @@ -267,7 +275,7 @@ func (table *Table) Addheaders(headers ...string) { case "json", "yaml": table.Headers[idx] = strings.ReplaceAll(strings.ToLower(header), " ", "_") default: - table.Headers[idx] = bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-")) + table.Headers[idx] = Bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-")) } table.RawHeaders[idx] = header