mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 09:44: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:
|
- 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
|
||||||
|
|
||||||
- no color when stdout ! tty
|
|
||||||
|
|||||||
@@ -18,8 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package printer
|
package printer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"codeberg.org/scip/esctl/pkg/cfg"
|
"codeberg.org/scip/esctl/pkg/cfg"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -31,6 +34,10 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Colorize(conf *cfg.Config, col, what string) string {
|
func Colorize(conf *cfg.Config, col, what string) string {
|
||||||
|
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
||||||
|
return what
|
||||||
|
}
|
||||||
|
|
||||||
switch conf.Output {
|
switch conf.Output {
|
||||||
case "json", "yaml":
|
case "json", "yaml":
|
||||||
return what
|
return what
|
||||||
@@ -49,3 +56,11 @@ func Colorize(conf *cfg.Config, col, what string) string {
|
|||||||
|
|
||||||
return what
|
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 {
|
for _, entries := range table.rows {
|
||||||
currentWidth := 0
|
currentWidth := 0
|
||||||
|
columns := len(entries)
|
||||||
|
|
||||||
for idx, entry := range entries {
|
for idx, entry := range entries {
|
||||||
length := visibleLen(entry)
|
length := visibleLen(entry)
|
||||||
|
|
||||||
if length+currentWidth > table.maxwidth && table.maxwidth-currentWidth > 1 {
|
if length+currentWidth > table.maxwidth &&
|
||||||
// // text is too wide to be put into one line, wrap it
|
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)
|
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
|
// further lines will be indented. Used within Print() to print large
|
||||||
// cell text.
|
// cell text.
|
||||||
func wrap(width, indent int, text string) string {
|
func wrap(width, indent int, text string) string {
|
||||||
|
if len(text) <= width {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
wrapped := ""
|
wrapped := ""
|
||||||
line := ""
|
line := ""
|
||||||
|
|
||||||
@@ -267,7 +275,7 @@ func (table *Table) Addheaders(headers ...string) {
|
|||||||
case "json", "yaml":
|
case "json", "yaml":
|
||||||
table.Headers[idx] = strings.ReplaceAll(strings.ToLower(header), " ", "_")
|
table.Headers[idx] = strings.ReplaceAll(strings.ToLower(header), " ", "_")
|
||||||
default:
|
default:
|
||||||
table.Headers[idx] = bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
|
table.Headers[idx] = Bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
|
||||||
}
|
}
|
||||||
|
|
||||||
table.RawHeaders[idx] = header
|
table.RawHeaders[idx] = header
|
||||||
|
|||||||
Reference in New Issue
Block a user