mirror of
https://github.com/charmbracelet/gum.git
synced 2024-08-17 02:00:46 +03:00
feat: use shift+tab
to toggle selection and move up (#167)
* feat: use `shift+tab` to toggle selection and move up * refactor: extract CursorUp, CursorDown, ToggleSelection methods Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
parent
9b0f5f015b
commit
5c98432070
@ -119,34 +119,21 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.quitting = true
|
||||
return m, tea.Quit
|
||||
case "ctrl+n", "ctrl+j", "down":
|
||||
m.cursor = clamp(0, len(m.matches)-1, m.cursor+1)
|
||||
if m.cursor >= m.viewport.YOffset+m.viewport.Height {
|
||||
m.viewport.LineDown(1)
|
||||
}
|
||||
m.CursorDown()
|
||||
case "ctrl+p", "ctrl+k", "up":
|
||||
m.cursor = clamp(0, len(m.matches)-1, m.cursor-1)
|
||||
if m.cursor < m.viewport.YOffset {
|
||||
m.viewport.SetYOffset(m.cursor)
|
||||
}
|
||||
m.CursorUp()
|
||||
case "tab":
|
||||
if m.limit == 1 {
|
||||
break // no op
|
||||
}
|
||||
|
||||
// Tab is used to toggle selection of current item in the list
|
||||
if _, ok := m.selected[m.matches[m.cursor].Str]; ok {
|
||||
delete(m.selected, m.matches[m.cursor].Str)
|
||||
m.numSelected--
|
||||
} else if m.numSelected < m.limit {
|
||||
m.selected[m.matches[m.cursor].Str] = struct{}{}
|
||||
m.numSelected++
|
||||
}
|
||||
|
||||
// Go down by one line
|
||||
m.cursor = clamp(0, len(m.matches)-1, m.cursor+1)
|
||||
if m.cursor >= m.viewport.YOffset+m.viewport.Height {
|
||||
m.viewport.LineDown(1)
|
||||
m.ToggleSelection()
|
||||
m.CursorDown()
|
||||
case "shift+tab":
|
||||
if m.limit == 1 {
|
||||
break // no op
|
||||
}
|
||||
m.ToggleSelection()
|
||||
m.CursorUp()
|
||||
default:
|
||||
m.textinput, cmd = m.textinput.Update(msg)
|
||||
|
||||
@ -170,6 +157,30 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
func (m *model) CursorUp() {
|
||||
m.cursor = clamp(0, len(m.matches)-1, m.cursor-1)
|
||||
if m.cursor < m.viewport.YOffset {
|
||||
m.viewport.SetYOffset(m.cursor)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) CursorDown() {
|
||||
m.cursor = clamp(0, len(m.matches)-1, m.cursor+1)
|
||||
if m.cursor >= m.viewport.YOffset+m.viewport.Height {
|
||||
m.viewport.LineDown(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *model) ToggleSelection() {
|
||||
if _, ok := m.selected[m.matches[m.cursor].Str]; ok {
|
||||
delete(m.selected, m.matches[m.cursor].Str)
|
||||
m.numSelected--
|
||||
} else if m.numSelected < m.limit {
|
||||
m.selected[m.matches[m.cursor].Str] = struct{}{}
|
||||
m.numSelected++
|
||||
}
|
||||
}
|
||||
|
||||
func matchAll(options []string) []fuzzy.Match {
|
||||
var matches = make([]fuzzy.Match, len(options))
|
||||
for i, option := range options {
|
||||
|
Loading…
Reference in New Issue
Block a user