Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/browsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 15 additions & 5 deletions pkg/table/table.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated TTY check instead of using new helper

Low Severity

PrintTableNoPad calls term.IsTerminal(int(os.Stdout.Fd())) inline at the same time the new IsStdoutTTY() helper — which does exactly the same thing — is introduced in the same file. The helper exists specifically to centralize this check and is already exported for use by cmd/browsers.go, but PrintTableNoPad doesn't use it. If the TTY detection logic ever changes, it would need updating in two places.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b003810. Configure here.

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])
Expand Down Expand Up @@ -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 {
Expand Down
Loading