summaryrefslogtreecommitdiff
path: root/internal/ui/keyhandlers.go
blob: 4fe3efbabd951d23fb23224f7c3fcd6575e5cdc5 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package ui

import (
	"charm.land/bubbles/v2/viewport"
	tea "charm.land/bubbletea/v2"
)

// handleNormalMode handles keyboard input in normal mode (not editing)
func (m *Model) handleNormalMode(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
	// If help is shown, handle special cases
	if m.showHelp {
		switch msg.String() {
		case "H", "q":
			return m.handleQuitKey()
		case "esc":
			return m.handleEscapeKey()
		case "/", "?":
			return m.handleHelpSearch()
		case "n":
			return m.handleNextHelpSearchMatch()
		case "N":
			return m.handlePrevHelpSearchMatch()
		case "up", "k":
			m.helpViewport.ScrollUp(1)
			return m, nil
		case "down", "j":
			m.helpViewport.ScrollDown(1)
			return m, nil
		case "pgup", "b":
			m.helpViewport.PageUp()
			return m, nil
		case "pgdown", "space":
			m.helpViewport.PageDown()
			return m, nil
		case "g", "home":
			m.helpViewport.GotoTop()
			return m, nil
		case "G", "end":
			m.helpViewport.GotoBottom()
			return m, nil
		default:
			// Ignore other keys in help mode
			return m, nil
		}
	}

	switch msg.String() {
	case "H":
		return m.handleToggleHelp()
	case "q":
		return m.handleQuitKey()
	case "esc":
		return m.handleEscapeKey()
	case "e", "E":
		return m.handleEditTask()
	case "s":
		return m.handleToggleStart()
	case "d":
		return m.handleMarkDone()
	case "o":
		return m.handleOpenURL()
	case "U":
		return m.handleUndo()
	case "w":
		return m.handleSetDueDate()
	case "W":
		return m.handleRemoveDueDate()
	case "r":
		return m.handleRandomDueDate()
	case "R":
		return m.handleSetRecurrence()
	case "p":
		return m.handleSetPriority()
	case "a":
		return m.handleAnnotate(false)
	case "A":
		return m.handleAnnotate(true)
	case "f":
		return m.handleFilter()
	case "+":
		return m.handleAddTask()
	case "t":
		return m.handleEditTags()
	case "J":
		return m.handleEditProject()
	case "T":
		return m.handleTagToProject()
	case "c":
		return m.handleRandomTheme()
	case "C":
		return m.handleResetTheme()
	case "x":
		return m.handleToggleDisco()
	case "B":
		return m.handleToggleBlink()
	case "space":
		return m.handleRefresh()
	case "/", "?":
		return m.handleSearch()
	case "n":
		return m.handleNextSearchMatch()
	case "N":
		return m.handlePrevSearchMatch()
	case "enter":
		return m.handleShowTaskDetail()
	case "i":
		return m.handleEnterOrEdit()
	case "u":
		m.ultraClearFocusedID()
		m.showUltra = true
		m.ultraCursor = m.tbl.Cursor()
		m.ultraOffset = 0
		m.ultraEnsureVisible()
		return m, nil
	case "1":
		return m.handleJumpToRandomTask()
	case "2":
		return m.handleJumpToRandomTaskNoDue()
	default:
		// Pass through to table for navigation
		return m.handleTableNavigation(msg)
	}
}

func (m *Model) handleToggleHelp() (tea.Model, tea.Cmd) {
	m.showHelp = true
	// Initialize help viewport with proper dimensions
	width := m.tbl.Width() - 4   // Account for padding
	height := m.windowHeight - 6 // Leave room for status bars and search input
	if width <= 0 {
		width = 80 // Default width
	}
	if height <= 0 {
		height = 20 // Default height
	}
	m.helpViewport = viewport.New(viewport.WithWidth(width), viewport.WithHeight(height))
	// Set the content immediately
	m.helpViewport.SetContent(m.activeHelpContent())
	return m, nil
}

func (m *Model) handleQuitKey() (tea.Model, tea.Cmd) {
	if m.showHelp {
		m.showHelp = false
		// Clear help search state
		m.helpSearchRegex = nil
		m.helpSearchMatches = nil
		m.helpSearchIndex = 0
		m.helpSearchInput.SetValue("")
		// Reset help viewport
		m.helpViewport = viewport.Model{}
		return m, nil
	}
	if m.showUltra {
		return m.handleUltraExitKey(true)
	}
	if m.showTaskDetail {
		m.showTaskDetail = false
		m.currentTaskDetail = nil
		m.detailSearching = false
		m.detailSearchRegex = nil
		m.detailSearchInput.SetValue("")
		return m, nil
	}
	if m.cellExpanded {
		m.cellExpanded = false
		m.updateTableHeight()
		return m, nil
	}
	if m.searchRegex != nil {
		m.searchRegex = nil
		m.searchMatches = nil
		m.searchIndex = 0
		m.reloadAndReport()
		return m, nil
	}
	return m, tea.Quit
}

func (m *Model) handleEscapeKey() (tea.Model, tea.Cmd) {
	if m.showHelp {
		m.showHelp = false
		// Clear help search state
		m.helpSearchRegex = nil
		m.helpSearchMatches = nil
		m.helpSearchIndex = 0
		m.helpSearchInput.SetValue("")
		// Reset help viewport
		m.helpViewport = viewport.Model{}
		return m, nil
	}
	if m.showUltra {
		return m.handleUltraExitKey(false)
	}
	if m.showTaskDetail {
		m.showTaskDetail = false
		m.currentTaskDetail = nil
		m.detailSearching = false
		m.detailSearchRegex = nil
		m.detailSearchInput.SetValue("")
		return m, nil
	}
	if m.cellExpanded {
		m.cellExpanded = false
		m.updateTableHeight()
		return m, nil
	}
	if m.searchRegex != nil {
		m.searchRegex = nil
		m.searchMatches = nil
		m.searchIndex = 0
		m.reloadAndReport()
		return m, nil
	}
	return m, nil
}