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
|
//! TUI event handling.
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use std::time::Duration;
use super::app::{Action, App, InputMode, ViewType};
/// Poll for events with timeout
pub fn poll_event(timeout: Duration) -> std::io::Result<Option<Event>> {
if event::poll(timeout)? {
Ok(Some(event::read()?))
} else {
Ok(None)
}
}
/// Handle a key event and return the resulting action
pub fn handle_key_event(app: &App, key: KeyEvent) -> Action {
// Special handling for TaskOutput view
if app.view_type == ViewType::TaskOutput && app.input_mode == InputMode::Normal {
return handle_output_mode(key);
}
match app.input_mode {
InputMode::Normal => handle_normal_mode(key),
InputMode::Search => handle_search_mode(key),
InputMode::Confirm => handle_confirm_mode(key),
InputMode::EditName | InputMode::EditDescription => handle_edit_mode(key),
}
}
/// Handle key events in normal navigation mode
fn handle_normal_mode(key: KeyEvent) -> Action {
// Check for Ctrl+C first
if key.modifiers.contains(KeyModifiers::CONTROL) {
match key.code {
KeyCode::Char('c') => return Action::Quit,
_ => {}
}
}
match key.code {
// Navigation
KeyCode::Up | KeyCode::Char('k') => Action::Up,
KeyCode::Down | KeyCode::Char('j') => Action::Down,
// Drill-down into selected item (Enter or l for vim-style)
KeyCode::Enter | KeyCode::Char('l') => Action::DrillDown,
// Go back (Backspace, h for vim-style, or Esc)
KeyCode::Backspace | KeyCode::Char('h') => Action::GoBack,
// Other actions
KeyCode::Char('e') => Action::Edit,
KeyCode::Char('d') => Action::Delete,
KeyCode::Char('c') => Action::Navigate, // cd to worktree
// Preview toggle (space to show details in preview pane)
KeyCode::Char(' ') => Action::Select,
// Search
KeyCode::Char('/') => Action::EnterSearch,
// Refresh
KeyCode::Char('r') => Action::Refresh,
// Quit (only q, Esc now goes back)
KeyCode::Char('q') => Action::Quit,
KeyCode::Esc => Action::GoBack,
_ => Action::None,
}
}
/// Handle key events in search mode
fn handle_search_mode(key: KeyEvent) -> Action {
// Check for Ctrl+C first
if key.modifiers.contains(KeyModifiers::CONTROL) {
match key.code {
KeyCode::Char('c') => return Action::Quit,
KeyCode::Char('u') => return Action::ClearSearch,
_ => {}
}
}
match key.code {
// Exit search mode
KeyCode::Esc => Action::ExitSearch,
KeyCode::Enter => Action::ExitSearch,
// Text input
KeyCode::Char(c) => Action::SearchChar(c),
KeyCode::Backspace => Action::SearchBackspace,
// Navigation while searching
KeyCode::Up => Action::Up,
KeyCode::Down => Action::Down,
_ => Action::None,
}
}
/// Handle key events in confirmation mode
fn handle_confirm_mode(key: KeyEvent) -> Action {
// Check for Ctrl+C first
if key.modifiers.contains(KeyModifiers::CONTROL) {
if let KeyCode::Char('c') = key.code {
return Action::Quit;
}
}
match key.code {
// Confirm
KeyCode::Char('y') | KeyCode::Char('Y') => Action::ConfirmYes,
// Cancel
KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => Action::ConfirmNo,
_ => Action::None,
}
}
/// Handle key events in task output view mode
fn handle_output_mode(key: KeyEvent) -> Action {
// Check for Ctrl+C first
if key.modifiers.contains(KeyModifiers::CONTROL) {
if let KeyCode::Char('c') = key.code {
return Action::Quit;
}
}
match key.code {
// Scroll
KeyCode::Up | KeyCode::Char('k') => Action::ScrollUp,
KeyCode::Down | KeyCode::Char('j') => Action::ScrollDown,
KeyCode::PageUp => Action::ScrollUp,
KeyCode::PageDown => Action::ScrollDown,
// Scroll to bottom
KeyCode::Char('G') | KeyCode::End => Action::ScrollToBottom,
// Go back (Backspace, h for vim-style, q, or Esc)
KeyCode::Backspace | KeyCode::Char('h') | KeyCode::Esc => Action::GoBack,
KeyCode::Char('q') => Action::GoBack,
// Refresh (re-connect WebSocket)
KeyCode::Char('r') => Action::Refresh,
// Navigate to worktree
KeyCode::Char('c') => Action::Navigate,
_ => Action::None,
}
}
/// Handle key events in edit mode
fn handle_edit_mode(key: KeyEvent) -> Action {
// Check for Ctrl+C first
if key.modifiers.contains(KeyModifiers::CONTROL) {
if let KeyCode::Char('c') = key.code {
return Action::Quit;
}
}
match key.code {
// Save
KeyCode::Enter => Action::EditSave,
// Cancel
KeyCode::Esc => Action::EditCancel,
// Switch fields
KeyCode::Tab => Action::EditNextField,
// Text input
KeyCode::Char(c) => Action::EditChar(c),
KeyCode::Backspace => Action::EditBackspace,
_ => Action::None,
}
}
/// Get help text for current mode
pub fn get_help_text(mode: InputMode) -> &'static str {
match mode {
InputMode::Normal => "j/k: nav | Enter: open | Esc/h: back | e: edit | d: del | c: cd | /: search | q: quit",
InputMode::Search => "Type to search | Enter/Esc: exit search | Up/Down: navigate",
InputMode::Confirm => "y: confirm | n/Esc: cancel",
InputMode::EditName | InputMode::EditDescription => "Type to edit | Tab: switch field | Enter: save | Esc: cancel",
}
}
/// Get help text for output view
pub fn get_output_help_text() -> &'static str {
"j/k: scroll | G: bottom | c: cd | q/Esc: back | r: refresh"
}
|