summaryrefslogtreecommitdiff
path: root/menu/input.go
blob: 6d057494be50a741345a47a14cd9c38823cac08a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package menu

import tea "github.com/charmbracelet/bubbletea"

func (m *Model) HandleInput(msg tea.KeyMsg) (tea.Cmd, error) {
	var cmd tea.Cmd
	switch msg.String() {
	case "h", "left":
		m.Leap(-1)
	case "l", "right":
		m.Leap(1)
	case "up", "k":
		m.Index--
	case "down", "j":
		m.Index++
	case "q", "esc", "ctrl+c":
		m.Quit = true
		cmd = tea.Quit
	case "space", " ", "enter", "ctrl+d":
		cmd = tea.Quit
	}

	m.Validate()

	return cmd, nil
}

func (m *Model) Leap(direction int) {
	for {
		source, target := m.Result()
		m.Index += direction
		m.Validate()
		newSource, newTarget := m.Result()
		if target == newTarget {
			return
		}

		if source != newSource {
			return
		}
	}
}