From 744cdcb73300956431ee79c18a952b057020c756 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Fri, 17 Jul 2026 09:52:55 +0200 Subject: [PATCH] fix word wrapping --- TODO.md | 16 +------------ go.mod | 3 +-- go.sum | 4 ---- pkg/printer/table.go | 54 ++++++++++++++++++++++++++++++++------------ 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/TODO.md b/TODO.md index ddca1ae..c7dd77f 100644 --- a/TODO.md +++ b/TODO.md @@ -6,18 +6,4 @@ - add datastream support: https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get-data-stream -- Fix: cluster status word wrapping adds one \n to much: - -```console -Bad health shards_availability This cluster has 1 unavailable replica shard. - -> cause Elasticsearch isn't allowed to allocate some shards from these indices to any of the nodes in the - - cluster. - -> affected indices foo-t-log-logs-002947 - -> suggested action to fix Diagnose the issue by calling the allocation explain API for an index [GET - - _cluster/allocation/explain]. Choose a node to which you expect a shard to be allocated, find this - - node in the node-by-node explanation, and address the reasons which prevent Elasticsearch from -``` - +- no color when stdout ! tty diff --git a/go.mod b/go.mod index 4f2a692..e87b72c 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ module codeberg.org/scip/esctl go 1.26 require ( + codeberg.org/scip/mapmap v0.0.2 github.com/MichaelMure/go-term-markdown v0.1.4 github.com/alecthomas/repr v0.5.3 github.com/charmbracelet/bubbles v1.0.0 @@ -31,7 +32,6 @@ require ( github.com/go-openapi/spec v0.22.5 github.com/go-openapi/swag/loading v0.26.1 github.com/mattn/go-isatty v0.0.22 - github.com/seeruk/go-wordwrap v0.0.0-20191208221741-14ec4aac9550 github.com/tidwall/gjson v1.19.0 github.com/tlinden/yadu v0.1.3 github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65 @@ -40,7 +40,6 @@ require ( ) require ( - codeberg.org/scip/mapmap v0.0.2 // indirect github.com/MichaelMure/go-term-text v0.3.1 // indirect github.com/alecthomas/chroma v0.7.1 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect diff --git a/go.sum b/go.sum index c9b40a4..7f1e805 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -codeberg.org/scip/mapmap v0.0.1 h1:L1jMBo/UNp19MXUF/ONjc1XKVGeuF1x05z0pEhjgkzA= -codeberg.org/scip/mapmap v0.0.1/go.mod h1:/ojYo2P7dMA2FWEu+jHKmsKPeq5yDcCvXeHevqZd5OI= codeberg.org/scip/mapmap v0.0.2 h1:0i61jOUwFmGVwPukrMzrx0Fi4T9Qru2nlmibuaJimBo= codeberg.org/scip/mapmap v0.0.2/go.mod h1:/ojYo2P7dMA2FWEu+jHKmsKPeq5yDcCvXeHevqZd5OI= github.com/MichaelMure/go-term-markdown v0.1.4 h1:Ir3kBXDUtOX7dEv0EaQV8CNPpH+T7AfTh0eniMOtNcs= @@ -155,8 +153,6 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= -github.com/seeruk/go-wordwrap v0.0.0-20191208221741-14ec4aac9550 h1:C3CfUXH/qmWuQFRqnPm3Sx8PFxa+pqACjhV5CaNO8pw= -github.com/seeruk/go-wordwrap v0.0.0-20191208221741-14ec4aac9550/go.mod h1:Sl541M2Em6rRG3V9WObycR7MYFZiERVkd/TJg0Gt0U4= github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/pkg/printer/table.go b/pkg/printer/table.go index bcb5826..240b669 100644 --- a/pkg/printer/table.go +++ b/pkg/printer/table.go @@ -25,7 +25,6 @@ import ( "strings" "codeberg.org/scip/esctl/pkg/cfg" - "github.com/seeruk/go-wordwrap" "gopkg.in/yaml.v3" ) @@ -161,20 +160,8 @@ func (table *Table) PrintTSV() error { length := visibleLen(entry) if length+currentWidth > table.maxwidth && table.maxwidth-currentWidth > 1 { - // text is too wide to be put into one line, wrap it - wrapper := wordwrap.Wrapper(table.maxwidth-currentWidth, false) - wrapped := wrapper(entry) - - // and indent it - first := true - for line := range strings.Lines(wrapped) { - if first { - entry = line - first = false - } else { - entry += "\n " + strings.Repeat(" ", currentWidth) + line - } - } + // // text is too wide to be put into one line, wrap it + entry = wrap(table.maxwidth-currentWidth, currentWidth+2, entry) } currentWidth += table.lenHeaders[idx] @@ -202,6 +189,43 @@ func (table *Table) PrintTSV() error { return nil } +// Wrap a text into multiple lines, first line is not indented, all +// further lines will be indented. Used within Print() to print large +// cell text. +func wrap(width, indent int, text string) string { + wrapped := "" + line := "" + + for word := range strings.FieldsSeq(text) { + if len(line)+len(word)+1 <= width { + // appending word to current line doesn't exceed width + if line != "" { + line += " " + } + line += word + } else { + // it exceeds it, so we need to wrap + if wrapped == "" { + // beginning of output, no indenting here + wrapped = line + "\n" + } else { + // we're in the middle of the text, so add the indent + wrapped += strings.Repeat(" ", indent) + line + "\n" + } + + // remember the current word for the next round + line = word + } + } + + if line != "" { + // last line, no newline needed here + wrapped += strings.Repeat(" ", indent) + line + } + + return wrapped +} + func (table *Table) PrintCSV() error { table.preprocessRows()