summaryrefslogtreecommitdiff
path: root/internal/tui/keys.go
blob: 7f23407e8009ef51d3110533100c6aabe2f97536 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
package tui

import (
	tea "github.com/charmbracelet/bubbletea"
)

// KeyAction represents the action to take for a key press
type KeyAction int

const (
	// Global actions
	ActionNone KeyAction = iota
	ActionNextPane
	ActionPrevPane
	ActionPunchToggle
	ActionSearch
	ActionRefresh
	ActionQuit
	
	// Timer pane actions
	ActionTimerEnter
	ActionTimerDescribe
	
	// Projects pane actions
	ActionProjectsNext
	ActionProjectsPrev
	ActionProjectsEnter
	ActionProjectsNewProject
	ActionProjectsNewClient
	
	// History pane actions (level 1)
	ActionHistoryNext
	ActionHistoryPrev
	ActionHistoryEnter
	
	// History pane actions (level 2)
	ActionHistoryEdit
	ActionHistoryDelete
	ActionHistoryResume
	ActionHistoryBack
)

// KeyHandler processes key messages and returns the appropriate action
func HandleKeyPress(msg tea.KeyMsg, selectedBox BoxType, historyLevel HistoryViewLevel, hasActiveTimer bool) KeyAction {
	key := msg.String()
	
	// Global keybindings (always available)
	switch key {
	case "ctrl+n":
		return ActionNextPane
	case "ctrl+p":
		return ActionPrevPane
	case "p":
		return ActionPunchToggle
	case "/":
		return ActionSearch
	case "r":
		return ActionRefresh
	case "q", "ctrl+c", "ctrl+d":
		return ActionQuit
	}
	
	// Context-specific keybindings based on selected box
	switch selectedBox {
	case TimerBox:
		return handleTimerKeys(key, hasActiveTimer)
	case ClientsProjectsBox:
		return handleProjectsKeys(key)
	case HistoryBox:
		return handleHistoryKeys(key, historyLevel)
	}
	
	return ActionNone
}

// handleTimerKeys handles keys specific to the timer box
func handleTimerKeys(key string, hasActiveTimer bool) KeyAction {
	switch key {
	case "enter":
		return ActionTimerEnter
	case "d":
		if hasActiveTimer {
			return ActionTimerDescribe
		}
	}
	return ActionNone
}

// handleProjectsKeys handles keys specific to the projects box
func handleProjectsKeys(key string) KeyAction {
	switch key {
	case "j", "down":
		return ActionProjectsNext
	case "k", "up":
		return ActionProjectsPrev
	case "enter":
		return ActionProjectsEnter
	case "n":
		return ActionProjectsNewProject
	case "N":
		return ActionProjectsNewClient
	}
	return ActionNone
}

// handleHistoryKeys handles keys specific to the history box
func handleHistoryKeys(key string, level HistoryViewLevel) KeyAction {
	switch level {
	case HistoryLevelSummary:
		return handleHistoryLevel1Keys(key)
	case HistoryLevelDetails:
		return handleHistoryLevel2Keys(key)
	}
	return ActionNone
}

// handleHistoryLevel1Keys handles keys for history summary view
func handleHistoryLevel1Keys(key string) KeyAction {
	switch key {
	case "j", "down":
		return ActionHistoryNext
	case "k", "up":
		return ActionHistoryPrev
	case "enter":
		return ActionHistoryEnter
	}
	return ActionNone
}

// handleHistoryLevel2Keys handles keys for history details view
func handleHistoryLevel2Keys(key string) KeyAction {
	switch key {
	case "j", "down":
		return ActionHistoryNext
	case "k", "up":
		return ActionHistoryPrev
	case "e":
		return ActionHistoryEdit
	case "d":
		return ActionHistoryDelete
	case "enter":
		return ActionHistoryResume
	case "b", "escape":
		return ActionHistoryBack
	}
	return ActionNone
}

// GetContextualKeyBindings returns the key bindings that should be shown in the bottom bar
func GetContextualKeyBindings(selectedBox BoxType, historyLevel HistoryViewLevel, hasActiveTimer bool) []KeyBinding {
	var bindings []KeyBinding
	
	// Global bindings (always shown)
	bindings = append(bindings, []KeyBinding{
		{"Ctrl+n", "Next"},
		{"Ctrl+p", "Prev"},
	}...)
	
	// Add punch toggle binding
	if hasActiveTimer {
		bindings = append(bindings, KeyBinding{"p", "Punch Out"})
	} else {
		bindings = append(bindings, KeyBinding{"p", "Punch In"})
	}
	
	// Add search and refresh bindings
	bindings = append(bindings, []KeyBinding{
		{"/", "Search"},
		{"r", "Refresh"},
	}...)
	
	// Context-specific bindings
	switch selectedBox {
	case TimerBox:
		bindings = append(bindings, getTimerKeyBindings(hasActiveTimer)...)
	case ClientsProjectsBox:
		bindings = append(bindings, getProjectsKeyBindings()...)
	case HistoryBox:
		bindings = append(bindings, getHistoryKeyBindings(historyLevel)...)
	}
	
	// Always end with quit
	bindings = append(bindings, KeyBinding{"q", "Quit"})
	
	return bindings
}

// getTimerKeyBindings returns key bindings for the timer box
func getTimerKeyBindings(hasActiveTimer bool) []KeyBinding {
	if hasActiveTimer {
		return []KeyBinding{
			{"Enter", "Punch Out"},
			{"d", "Describe"},
		}
	}
	return []KeyBinding{
		{"Enter", "Resume Recent"},
	}
}

// getProjectsKeyBindings returns key bindings for the projects box
func getProjectsKeyBindings() []KeyBinding {
	return []KeyBinding{
		{"j/k", "Navigate"},
		{"Enter", "Select"},
		{"n", "New Project"},
		{"N", "New Client"},
	}
}

// getHistoryKeyBindings returns key bindings for the history box
func getHistoryKeyBindings(level HistoryViewLevel) []KeyBinding {
	switch level {
	case HistoryLevelSummary:
		return []KeyBinding{
			{"j/k", "Navigate"},
			{"Enter", "Details"},
		}
	case HistoryLevelDetails:
		return []KeyBinding{
			{"j/k", "Navigate"},
			{"Enter", "Resume"},
			{"e", "Edit"},
			{"d", "Delete"},
			{"b", "Back"},
		}
	}
	return []KeyBinding{}
}