Move between two columns with lists #314
-
Hi guys, I was wondering is it possible to move cursor between two lists that are in different columns so I can browse two lists independently? Like for example pressing tab key when you want to change a window? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Definitely! All you need is for your model to have two different lists as well as a bool to tell you which one it is currently selecting. Then in the Omitting some boiler plate, it would look something like the following: import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
leftList list.Model
rightList list.Model
is_selecting_left_list bool
}
func (m *model) selectedList() *list.Model {
if m.is_selecting_left_list{
return &m.leftList
}
return &m.rightList
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "tab":
m.is_selecting_left_list = !is_selecting_left_list // This swaps which list you're focusing on
default:
m.selectedList().Update(msg) // In practice, you should still send most updates to both lists (e.g. window resizing msgs)
}
}
return m, nil
} This could definitely be generalized further by giving your model Here's another example using the same idea but with colors and items in the list. |
Beta Was this translation helpful? Give feedback.
Definitely! All you need is for your model to have two different lists as well as a bool to tell you which one it is currently selecting. Then in the
Update
method, only send certain messages (e.g. keystrokes) to the list which is selected.Omitting some boiler plate, it would look something like the following: