From feb8923adada675b19dc1bc20f39ed6cfb0b99da Mon Sep 17 00:00:00 2001 From: Bruce MacDonald Date: Thu, 15 May 2025 15:45:52 -0700 Subject: [PATCH] cmd: add ellipses to truncated show metadata (#10717) When a piece of information has been truncated in the show output an ellipses to indicate that more data has not been displayed --- cmd/cmd.go | 50 ++++++++++++++++++++++++++++++++++++++++++------- cmd/cmd_test.go | 1 + 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 0f8072f0..df9af354 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -747,11 +747,38 @@ func showInfo(resp *api.ShowResponse, verbose bool, w io.Writer) error { case float64: v = fmt.Sprintf("%g", vData) case []any: - n := 3 - if len(vData) < n { - n = len(vData) + targetWidth := 10 // Small width where we are displaying the data in a column + + var itemsToShow int + totalWidth := 1 // Start with 1 for opening bracket + + // Find how many we can fit + for i := range vData { + itemStr := fmt.Sprintf("%v", vData[i]) + width := runewidth.StringWidth(itemStr) + + // Add separator width (", ") for all items except the first + if i > 0 { + width += 2 + } + + // Check if adding this item would exceed our width limit + if totalWidth+width > targetWidth && i > 0 { + break + } + + totalWidth += width + itemsToShow++ + } + + // Format the output + if itemsToShow < len(vData) { + v = fmt.Sprintf("%v", vData[:itemsToShow]) + v = strings.TrimSuffix(v, "]") + v += fmt.Sprintf(" ...+%d more]", len(vData)-itemsToShow) + } else { + v = fmt.Sprintf("%v", vData) } - v = fmt.Sprintf("%v", vData[:n]) default: v = fmt.Sprintf("%T", vData) } @@ -772,10 +799,19 @@ func showInfo(resp *api.ShowResponse, verbose bool, w io.Writer) error { head := func(s string, n int) (rows [][]string) { scanner := bufio.NewScanner(strings.NewReader(s)) - for scanner.Scan() && (len(rows) < n || n < 0) { - if text := scanner.Text(); text != "" { - rows = append(rows, []string{"", strings.TrimSpace(text)}) + count := 0 + for scanner.Scan() { + text := strings.TrimSpace(scanner.Text()) + if text == "" { + continue } + count++ + if n < 0 || count <= n { + rows = append(rows, []string{"", text}) + } + } + if n >= 0 && count > n { + rows = append(rows, []string{"", "..."}) } return } diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index eb2fb124..cf5fe7ca 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -225,6 +225,7 @@ Weigh anchor! System You are a pirate! Ahoy, matey! + ... ` if diff := cmp.Diff(expect, b.String()); diff != "" {