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
62 changes: 61 additions & 1 deletion internal/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,9 @@ func TestMarkdownRenderObjectPreservesURLs(t *testing.T) {
}

func TestStyledRenderTablePreservesURLs(t *testing.T) {
url := "https://3.basecampapi.com/1234567/buckets/12345678/todolists/9876543210.json"
// URL must be >40 chars (to exercise the cap exemption) but short enough
// to fit beside "name" in an 80-char default terminal.
url := "https://3.basecampapi.com/1234567/todolists/98765.json"
data := []any{
map[string]any{"name": "Tasks", "todolists_url": url},
}
Expand Down Expand Up @@ -2466,6 +2468,64 @@ func TestSelectColumnsUsesFormattedDateWidth(t *testing.T) {
}
}

// =============================================================================
// URL Column Width Exemption
// =============================================================================

func TestSelectColumnsExemptsURLColumnsFromWidthCap(t *testing.T) {
r := &Renderer{width: 120}
cols := []column{
{key: "name", header: "Name", priority: 2},
{key: "href", header: "Href", priority: 5},
{key: "description", header: "Description", priority: 8},
}
data := []map[string]any{
{
"name": "Alice",
"href": "https://3.basecampapi.com/1234567/people/9999999.json",
"description": "This is a long description that exceeds forty characters easily",
},
}
selected := r.selectColumns(cols, data)

var hrefCol, descCol column
for _, col := range selected {
switch col.key {
case "href":
hrefCol = col
case "description":
descCol = col
}
}

assert.Greater(t, hrefCol.width, 40, "URL column should retain actual width")
assert.True(t, hrefCol.containsURL, "URL column should be flagged")
assert.Equal(t, 40, descCol.width, "non-URL column should be capped at 40")
assert.False(t, descCol.containsURL, "non-URL column should not be flagged")
}

func TestSelectColumnsExemptsURLColumnsForSuffixFields(t *testing.T) {
r := &Renderer{width: 120}
cols := []column{
{key: "name", header: "Name", priority: 2},
{key: "todolists_url", header: "Todolists Url", priority: 5},
}
data := []map[string]any{
{
"name": "Project Alpha",
"todolists_url": "https://3.basecampapi.com/1234567/buckets/8888/todolists.json",
},
}
selected := r.selectColumns(cols, data)

for _, col := range selected {
if col.key == "todolists_url" {
assert.Greater(t, col.width, 40, "URL column with _url suffix should retain actual width")
assert.True(t, col.containsURL, "URL column should be flagged")
}
}
}

// =============================================================================
// updated_at Omission in Generic Tables
// =============================================================================
Expand Down
23 changes: 15 additions & 8 deletions internal/output/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,12 @@ var skipColumns = map[string]bool{
}

type column struct {
key string
header string
priority int
muted bool
width int
key string
header string
priority int
muted bool
width int
containsURL bool
}

func (r *Renderer) renderTable(b *strings.Builder, data []map[string]any) {
Expand Down Expand Up @@ -498,13 +499,19 @@ func (r *Renderer) selectColumns(cols []column, data []map[string]any) []column
for i := range cols {
cols[i].width = lipgloss.Width(cols[i].header)
for _, row := range data {
cellWidth := lipgloss.Width(formatTableCell(cols[i].key, row[cols[i].key]))
formatted := formatTableCell(cols[i].key, row[cols[i].key])
cellWidth := lipgloss.Width(formatted)
if cellWidth > cols[i].width {
cols[i].width = cellWidth
}
if !cols[i].containsURL && isURL(formatted) {
cols[i].containsURL = true
}
}
// Cap width at 40 for long content
if cols[i].width > 40 {
// Cap width at 40 for long content. URL columns keep actual
// width so column-dropping math matches what formatCell (which
// never truncates URLs) actually renders.
if cols[i].width > 40 && !cols[i].containsURL {
cols[i].width = 40
Comment thread
jeremy marked this conversation as resolved.
}
}
Expand Down
Loading