From b00381065c94a59cd039a87f1e0869d1f5f275d7 Mon Sep 17 00:00:00 2001 From: sjmiller609 <7516283+sjmiller609@users.noreply.github.com> Date: Fri, 10 Apr 2026 15:58:14 +0000 Subject: [PATCH] Skip column truncation when stdout is piped (non-TTY) When stdout is not a terminal (e.g. piped to grep, awk, cat), output full column values without truncation. This follows the standard CLI pattern used by docker ps, kubectl get, etc. - Table column truncation in PrintTableNoPad now checks IsTerminal - truncateURL also skips truncation in non-TTY mode - TTY behavior (truncate to terminal width) is unchanged Co-Authored-By: Claude Opus 4.6 --- cmd/browsers.go | 4 ++++ pkg/table/table.go | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/browsers.go b/cmd/browsers.go index d799667f..9d9a01a2 100644 --- a/cmd/browsers.go +++ b/cmd/browsers.go @@ -15,6 +15,7 @@ import ( "strconv" "strings" + "github.com/kernel/cli/pkg/table" "github.com/kernel/cli/pkg/util" "github.com/kernel/kernel-go-sdk" "github.com/kernel/kernel-go-sdk/option" @@ -3256,6 +3257,9 @@ func runBrowsersComputerWriteClipboard(cmd *cobra.Command, args []string) error } func truncateURL(url string, maxLen int) string { + if !table.IsStdoutTTY() { + return url + } if len(url) <= maxLen { return url } diff --git a/pkg/table/table.go b/pkg/table/table.go index a1ad832f..9082c639 100644 --- a/pkg/table/table.go +++ b/pkg/table/table.go @@ -1,10 +1,12 @@ package table import ( + "os" "strings" "unicode/utf8" "github.com/pterm/pterm" + "golang.org/x/term" ) // PrintTableNoPad renders a table similar to pterm.DefaultTable, but it avoids @@ -17,12 +19,15 @@ func PrintTableNoPad(data pterm.TableData, hasHeader bool) { return } - // Get terminal width and truncate data to fit - termWidth := pterm.GetTerminalWidth() - if termWidth <= 0 { - termWidth = 80 // fallback + // Only truncate columns when outputting to a terminal. + // When piped (non-TTY), output full values so grep/awk/etc. work correctly. + if term.IsTerminal(int(os.Stdout.Fd())) { + termWidth := pterm.GetTerminalWidth() + if termWidth <= 0 { + termWidth = 80 // fallback + } + data = truncateTableData(data, termWidth) } - data = truncateTableData(data, termWidth) // Determine number of columns from the first row numCols := len(data[0]) @@ -90,6 +95,11 @@ func PrintTableNoPad(data pterm.TableData, hasHeader bool) { pterm.Print(b.String()) } +// IsStdoutTTY reports whether stdout is connected to a terminal. +func IsStdoutTTY() bool { + return term.IsTerminal(int(os.Stdout.Fd())) +} + // truncateTableData intelligently truncates table cells to fit within terminal width func truncateTableData(data pterm.TableData, termWidth int) pterm.TableData { if len(data) == 0 {