mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 02:24:18 +02:00
fix word wrapping
This commit is contained in:
16
TODO.md
16
TODO.md
@@ -6,18 +6,4 @@
|
|||||||
- add datastream support:
|
- add datastream support:
|
||||||
https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get-data-stream
|
https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-get-data-stream
|
||||||
|
|
||||||
- Fix: cluster status word wrapping adds one \n to much:
|
- no color when stdout ! tty
|
||||||
|
|
||||||
```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
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|||||||
3
go.mod
3
go.mod
@@ -17,6 +17,7 @@ module codeberg.org/scip/esctl
|
|||||||
go 1.26
|
go 1.26
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
codeberg.org/scip/mapmap v0.0.2
|
||||||
github.com/MichaelMure/go-term-markdown v0.1.4
|
github.com/MichaelMure/go-term-markdown v0.1.4
|
||||||
github.com/alecthomas/repr v0.5.3
|
github.com/alecthomas/repr v0.5.3
|
||||||
github.com/charmbracelet/bubbles v1.0.0
|
github.com/charmbracelet/bubbles v1.0.0
|
||||||
@@ -31,7 +32,6 @@ require (
|
|||||||
github.com/go-openapi/spec v0.22.5
|
github.com/go-openapi/spec v0.22.5
|
||||||
github.com/go-openapi/swag/loading v0.26.1
|
github.com/go-openapi/swag/loading v0.26.1
|
||||||
github.com/mattn/go-isatty v0.0.22
|
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/tidwall/gjson v1.19.0
|
||||||
github.com/tlinden/yadu v0.1.3
|
github.com/tlinden/yadu v0.1.3
|
||||||
github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65
|
github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65
|
||||||
@@ -40,7 +40,6 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
codeberg.org/scip/mapmap v0.0.2 // indirect
|
|
||||||
github.com/MichaelMure/go-term-text v0.3.1 // indirect
|
github.com/MichaelMure/go-term-text v0.3.1 // indirect
|
||||||
github.com/alecthomas/chroma v0.7.1 // indirect
|
github.com/alecthomas/chroma v0.7.1 // indirect
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
|||||||
4
go.sum
4
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 h1:0i61jOUwFmGVwPukrMzrx0Fi4T9Qru2nlmibuaJimBo=
|
||||||
codeberg.org/scip/mapmap v0.0.2/go.mod h1:/ojYo2P7dMA2FWEu+jHKmsKPeq5yDcCvXeHevqZd5OI=
|
codeberg.org/scip/mapmap v0.0.2/go.mod h1:/ojYo2P7dMA2FWEu+jHKmsKPeq5yDcCvXeHevqZd5OI=
|
||||||
github.com/MichaelMure/go-term-markdown v0.1.4 h1:Ir3kBXDUtOX7dEv0EaQV8CNPpH+T7AfTh0eniMOtNcs=
|
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/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 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
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 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
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=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
"github.com/seeruk/go-wordwrap"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -161,20 +160,8 @@ func (table *Table) PrintTSV() error {
|
|||||||
length := visibleLen(entry)
|
length := visibleLen(entry)
|
||||||
|
|
||||||
if length+currentWidth > table.maxwidth && table.maxwidth-currentWidth > 1 {
|
if length+currentWidth > table.maxwidth && table.maxwidth-currentWidth > 1 {
|
||||||
// text is too wide to be put into one line, wrap it
|
// // text is too wide to be put into one line, wrap it
|
||||||
wrapper := wordwrap.Wrapper(table.maxwidth-currentWidth, false)
|
entry = wrap(table.maxwidth-currentWidth, currentWidth+2, entry)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentWidth += table.lenHeaders[idx]
|
currentWidth += table.lenHeaders[idx]
|
||||||
@@ -202,6 +189,43 @@ func (table *Table) PrintTSV() error {
|
|||||||
return nil
|
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 {
|
func (table *Table) PrintCSV() error {
|
||||||
table.preprocessRows()
|
table.preprocessRows()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user