v2.31.0-dev23 58a92f3

table_test.go RawHistoryFolder

1package components
2
3import (
4	"regexp"
5	"strings"
6	"testing"
7
8	"github.com/charmbracelet/lipgloss"
9	"github.com/mattn/go-runewidth"
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/require"
12)
13
14func TestTableRender_AppliesConfiguredColumnWidth(t *testing.T) {
15	tableModel := NewTable(
16		WithColumns([]TableColumn{{Title: "ID", Width: 5}}),
17		WithRows([][]string{{"abc"}}),
18		WithHeaderStyle(lipgloss.NewStyle()),
19		WithCellStyle(lipgloss.NewStyle()),
20	)
21
22	rendered := stripANSI(tableModel.Render())
23	lines := strings.Split(rendered, "\n")
24
25	var rowLine string
26	for _, line := range lines {
27		if strings.Contains(line, "abc") {
28			rowLine = line
29			break
30		}
31	}
32
33	require.NotEmpty(t, rowLine)
34	assert.Contains(t, rowLine, "abc  ")
35}
36
37func TestTableRender_TruncatesLongCellTextWithEllipsis(t *testing.T) {
38	tableModel := NewTable(
39		WithColumns([]TableColumn{{Title: "ID", Width: 5}}),
40		WithRows([][]string{{"abcdef"}}),
41		WithHeaderStyle(lipgloss.NewStyle()),
42		WithCellStyle(lipgloss.NewStyle()),
43	)
44
45	rendered := stripANSI(tableModel.Render())
46	assert.Contains(t, rendered, "ab...")
47	assert.NotContains(t, rendered, "abcdef")
48}
49
50func TestTruncateCell_LeavesShortTextUnchanged(t *testing.T) {
51	assert.Equal(t, "abc", truncateCell("abc", 5))
52}
53
54func TestTruncateCell_EdgeCases(t *testing.T) {
55	tests := []struct {
56		name     string
57		value    string
58		maxWidth int
59		expected string
60	}{
61		{name: "zero width passthrough", value: "abcdef", maxWidth: 0, expected: "abcdef"},
62		{name: "width three all dots", value: "abcdef", maxWidth: 3, expected: "..."},
63		{name: "ascii truncates", value: "abcdef", maxWidth: 5, expected: "ab..."},
64		{name: "cjk truncates by display width", value: "你好世界", maxWidth: 5, expected: "你..."},
65		{name: "grapheme-safe emoji truncation", value: "👨<U+200D>👩<U+200D>👧<U+200D>👦abcd", maxWidth: 5, expected: "👨<U+200D>👩<U+200D>👧<U+200D>👦..."},
66	}
67
68	for _, tt := range tests {
69		tt := tt
70		t.Run(tt.name, func(t *testing.T) {
71			got := truncateCell(tt.value, tt.maxWidth)
72			assert.Equal(t, tt.expected, got)
73			if tt.maxWidth > 0 {
74				assert.LessOrEqual(t, runewidth.StringWidth(got), tt.maxWidth)
75			}
76		})
77	}
78}
79
80func TestTruncateCell_AnsiInputPassthrough(t *testing.T) {
81	styled := "\x1b[32mactive\x1b[0m"
82	got := truncateCell(styled, 3)
83	assert.Equal(t, styled, got)
84	assert.Contains(t, got, "\x1b[")
85}
86
87func stripANSI(input string) string {
88	ansiPattern := regexp.MustCompile(`\x1b\[[0-9;]*[A-Za-z]`)
89	return ansiPattern.ReplaceAllString(input, "")
90}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close