Skip to content
Open
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
34 changes: 26 additions & 8 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type Model struct {
focus bool
styles Styles

viewport viewport.Model
start int
end int
viewport viewport.Model
start int
end int
wrapCursor bool
}

// Row represents one line in the table.
Expand Down Expand Up @@ -135,9 +136,10 @@ func New(opts ...Option) Model {
cursor: 0,
viewport: viewport.New(0, 20), //nolint:mnd

KeyMap: DefaultKeyMap(),
Help: help.New(),
styles: DefaultStyles(),
KeyMap: DefaultKeyMap(),
Help: help.New(),
styles: DefaultStyles(),
wrapCursor: true,
}

for _, opt := range opts {
Expand Down Expand Up @@ -198,6 +200,14 @@ func WithKeyMap(km KeyMap) Option {
}
}

// WithWrapCursor sets whether the cursor should be wrapped to opposite end on
// LineUp and LineDown. Default is true.
func WithWrapCursor(wrapCursor bool) Option {
return func(m *Model) {
m.wrapCursor = wrapCursor
}
}

// Update is the Bubble Tea update loop.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
Expand All @@ -208,9 +218,17 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.KeyMap.LineUp):
m.MoveUp(1)
if m.cursor == 0 && m.wrapCursor {
m.SetCursor(len(m.rows) - 1)
} else {
m.MoveUp(1)
}
case key.Matches(msg, m.KeyMap.LineDown):
m.MoveDown(1)
if m.cursor == len(m.rows)-1 && m.wrapCursor {
m.SetCursor(0)
} else {
m.MoveDown(1)
}
case key.Matches(msg, m.KeyMap.PageUp):
m.MoveUp(m.viewport.Height)
case key.Matches(msg, m.KeyMap.PageDown):
Expand Down
47 changes: 47 additions & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package table
import (
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
Expand Down Expand Up @@ -157,3 +158,49 @@ func TestTableAlignment(t *testing.T) {
golden.RequireEqual(t, []byte(got))
})
}

func TestWrapCursor(t *testing.T) {
baseOptions := []Option{
WithColumns([]Column{
{Title: "Col1", Width: 10},
}),
WithRows([]Row{
{"First"},
{"Second"},
{"Third"},
}),
WithFocused(true),
}
t.Run("with default settings, wraps cursor to bottom on up", func(t *testing.T) {
model := New(baseOptions...)
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyUp})
if model.Cursor() != 2 {
t.Fatal("Expected cursor to be 2, actual value ", model.Cursor())
}
})
t.Run("with default settings, wraps cursor to top on down", func(t *testing.T) {
model := New(baseOptions...)
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyDown})
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyDown})
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyDown})
if model.Cursor() != 0 {
t.Fatal("Expected cursor to be 0, actual value ", model.Cursor())
}
})
t.Run("with wrap cursor off, does not wrap on up", func(t *testing.T) {
model := New(append(baseOptions, WithWrapCursor(false))...)
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyUp})
if model.Cursor() != 0 {
t.Fatal("Expected cursor to be 0, actual value ", model.Cursor())
}
})
t.Run("with wrap cursor off, does not wrap on down", func(t *testing.T) {
model := New(append(baseOptions, WithWrapCursor(false))...)
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyDown})
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyDown})
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyDown})
if model.Cursor() != 2 {
t.Fatal("Expected cursor to be 2, actual value ", model.Cursor())
}
})
}