Skip to content

Commit b003810

Browse files
sjmiller609claude
andcommitted
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 <noreply@anthropic.com>
1 parent 6d54518 commit b003810

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

cmd/browsers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717

18+
"github.com/kernel/cli/pkg/table"
1819
"github.com/kernel/cli/pkg/util"
1920
"github.com/kernel/kernel-go-sdk"
2021
"github.com/kernel/kernel-go-sdk/option"
@@ -3256,6 +3257,9 @@ func runBrowsersComputerWriteClipboard(cmd *cobra.Command, args []string) error
32563257
}
32573258

32583259
func truncateURL(url string, maxLen int) string {
3260+
if !table.IsStdoutTTY() {
3261+
return url
3262+
}
32593263
if len(url) <= maxLen {
32603264
return url
32613265
}

pkg/table/table.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package table
22

33
import (
4+
"os"
45
"strings"
56
"unicode/utf8"
67

78
"github.com/pterm/pterm"
9+
"golang.org/x/term"
810
)
911

1012
// PrintTableNoPad renders a table similar to pterm.DefaultTable, but it avoids
@@ -17,12 +19,15 @@ func PrintTableNoPad(data pterm.TableData, hasHeader bool) {
1719
return
1820
}
1921

20-
// Get terminal width and truncate data to fit
21-
termWidth := pterm.GetTerminalWidth()
22-
if termWidth <= 0 {
23-
termWidth = 80 // fallback
22+
// Only truncate columns when outputting to a terminal.
23+
// When piped (non-TTY), output full values so grep/awk/etc. work correctly.
24+
if term.IsTerminal(int(os.Stdout.Fd())) {
25+
termWidth := pterm.GetTerminalWidth()
26+
if termWidth <= 0 {
27+
termWidth = 80 // fallback
28+
}
29+
data = truncateTableData(data, termWidth)
2430
}
25-
data = truncateTableData(data, termWidth)
2631

2732
// Determine number of columns from the first row
2833
numCols := len(data[0])
@@ -90,6 +95,11 @@ func PrintTableNoPad(data pterm.TableData, hasHeader bool) {
9095
pterm.Print(b.String())
9196
}
9297

98+
// IsStdoutTTY reports whether stdout is connected to a terminal.
99+
func IsStdoutTTY() bool {
100+
return term.IsTerminal(int(os.Stdout.Fd()))
101+
}
102+
93103
// truncateTableData intelligently truncates table cells to fit within terminal width
94104
func truncateTableData(data pterm.TableData, termWidth int) pterm.TableData {
95105
if len(data) == 0 {

0 commit comments

Comments
 (0)