Skip to content

Commit 9f7b3df

Browse files
sjmiller609claude
andauthored
Skip column truncation when stdout is piped (non-TTY) (#148)
## Summary Fixes CLI table output truncation when stdout is piped to other tools (grep, awk, cat, etc.). **Before:** `kernel browsers list | grep hype` returned nothing because URLs and other columns were truncated even in pipe mode. **After:** - TTY (interactive terminal): columns truncate to fit terminal width (unchanged) - Non-TTY (piped): full column values output without truncation This follows the standard CLI pattern used by `docker ps`, `kubectl get`, etc. ### Changes - `pkg/table/table.go`: Skip `truncateTableData` when stdout is not a TTY; add `IsStdoutTTY()` helper - `cmd/browsers.go`: `truncateURL` skips truncation in non-TTY mode ## Test plan - [ ] `kernel browsers list` in terminal — columns still truncate to fit width - [ ] `kernel browsers list | cat` — full URLs visible, no truncation - [ ] `kernel browsers list | grep <pattern>` — matches found in full output - [ ] All existing tests pass (`go test ./...`) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk: changes are limited to CLI formatting behavior and only affect output when stdout is not a terminal (piped), with no impact on API calls or data mutation. > > **Overview** > Stops truncating CLI output when stdout is piped (non-TTY) so full column values are preserved for tools like `grep`/`awk`. > > `pkg/table.PrintTableNoPad` now truncates columns only for terminal output and adds a shared `table.IsStdoutTTY()` helper; `cmd/browsers.go`’s `truncateURL` also skips truncation when not on a TTY. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b003810. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: sjmiller609 <7516283+sjmiller609@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6d54518 commit 9f7b3df

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

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)