summaryrefslogtreecommitdiff
path: root/menu/input.go
blob: cd07543f5dd52819b7a86be45114ad71fd3b2f35 (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
43
44
45
46
47
48
49
50
51
52
53
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 "i", "I":
		if m.Alt {
			m.Alt = false
			m.AutoAlt = false
			return tea.ExitAltScreen, nil
		} else {
			m.Alt = true
			m.AutoAlt = false
			return tea.EnterAltScreen, nil
		}
	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.Cancel = true
		return tea.Quit, nil
	case "space", " ", "enter", "ctrl+d":
		m.Done = true
		return tea.Quit, nil
	}

	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
		}
	}
}