mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 01:34:17 +02:00
fix too wide tables when in !tty mode
This commit is contained in:
2
TODO.md
2
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
|
||||
|
||||
@@ -18,8 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user