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
//! Key handlers for the small text-input / list-navigation modes.
//!
//! Mirrors the `cmd_*.rs` split: each of these used to be a multi-arm
//! `match key.code` block inline in `handle_key`'s top-level mode
//! dispatch, which made the dispatch site itself hundreds of lines
//! long. Lifting them out leaves the dispatcher as one-liner method
//! calls and lets each mode's body sit next to nothing else.
//!
//! Bigger modes (`Detail`, `Action`, `Dlq`, `Form`, `Shell`) already
//! had their own `handle_*_key` helpers — those stay where they were.
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use super::{is_text_input, App, Mode};
impl App {
/// `Mode::Filter` — typing builds up `self.filter` and re-runs
/// `rebuild_view` so the table reflects the search live; Esc
/// clears + exits; Enter commits and exits.
pub(super) fn handle_filter_key(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc => {
self.filter.clear();
self.mode = Mode::Normal;
self.rebuild_view();
}
KeyCode::Enter => self.mode = Mode::Normal,
KeyCode::Backspace => {
self.filter.pop();
self.rebuild_view();
}
KeyCode::Char(c) if is_text_input(&key) => {
self.filter.push(c);
self.rebuild_view();
}
_ => {}
}
}
/// `Mode::Help` — Esc / `?` / q dismisses (restoring the
/// pre-help mode + overlay so `?` from a Detail / overlay
/// context returns the operator to where they were). j/k scroll.
pub(super) fn handle_help_key(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc | KeyCode::Char('?') | KeyCode::Char('q') => {
// Restore the screen the user was on before opening
// help. `pre_help_mode` is set at every `?` keypress; if
// somehow missing, fall back to Normal so we don't get
// stuck in Help.
self.mode = self.help.pre_mode.take().unwrap_or(Mode::Normal);
if let Some(overlay) = self.help.pre_overlay.take() {
self.current_overlay = Some(overlay);
}
self.help.scroll = 0;
}
KeyCode::Char('j') | KeyCode::Down => {
// Clamp to the last-known content bound so scrolling
// past the end doesn't accumulate phantom offsets.
self.help.scroll = self.help.scroll.saturating_add(1).min(self.help.max_scroll);
}
KeyCode::Char('k') | KeyCode::Up => {
self.help.scroll = self.help.scroll.saturating_sub(1);
}
_ => {}
}
}
/// `Mode::Command` — the `:` prompt. Enter dispatches via
/// `execute_command`; Tab / Shift-Tab cycles through completion
/// matches; any printable key (besides Tab) resets the cycle.
pub(super) fn handle_command_key(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc => {
self.command_input.clear();
self.completion.origin = None;
self.mode = Mode::Normal;
}
KeyCode::Enter => {
let cmd = self.command_input.clone();
self.command_input.clear();
self.completion.origin = None;
self.mode = Mode::Normal;
self.execute_command(&cmd);
}
KeyCode::Backspace => {
// Reset the completion cycle — the operator
// is editing, not cycling. Same intent as
// typing a new character.
self.completion.origin = None;
self.command_input.pop();
}
KeyCode::Tab => self.command_completion_step(1),
KeyCode::BackTab => self.command_completion_step(-1),
KeyCode::Char(c) if is_text_input(&key) => {
// Any printable key resets the completion
// cycle so the operator's next Tab starts a
// fresh search.
self.completion.origin = None;
self.command_input.push(c);
}
_ => {}
}
}
/// `Mode::Palette` — the `Ctrl-K` fuzzy command palette. ↑/↓
/// moves the cursor, Enter dispatches the selection, any printable
/// re-filters.
pub(super) fn handle_palette_key(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc => {
self.mode = Mode::Normal;
self.palette_input.clear();
}
KeyCode::Down => self.palette_move(1),
KeyCode::Up => self.palette_move(-1),
KeyCode::Enter => self.palette_execute(),
KeyCode::Backspace => {
self.palette_input.pop();
self.palette_refilter();
}
KeyCode::Char(c) if is_text_input(&key) => {
self.palette_input.push(c);
self.palette_refilter();
}
_ => {}
}
}
/// `Mode::QuickJump` — the `'`-prefixed name-prefix jump. Typing
/// moves the table cursor to the first env whose name starts with
/// the buffer; Enter / Esc commit / cancel respectively (both
/// return to Normal — the cursor stays where it landed on the
/// last typed character).
pub(super) fn handle_quickjump_key(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc => {
self.quickjump_input.clear();
self.mode = Mode::Normal;
}
KeyCode::Enter => {
self.quickjump_input.clear();
self.mode = Mode::Normal;
}
KeyCode::Backspace => {
self.quickjump_input.pop();
self.quickjump_apply();
}
KeyCode::Char(c) if is_text_input(&key) => {
self.quickjump_input.push(c);
self.quickjump_apply();
}
_ => {}
}
}
/// `Mode::Picker` — generic single-select list picker used by
/// region / profile / log-group / swap-target. j/k or ↑/↓ moves;
/// typing filters; Enter applies; Esc cancels.
pub(super) fn handle_picker_key(&mut self, key: KeyEvent) {
match key.code {
KeyCode::Esc => {
self.picker = None;
self.mode = Mode::Normal;
}
KeyCode::Enter => {
if let Some(picker) = self.picker.take() {
let kind = picker.kind;
if let Some(value) = picker.selected_value() {
self.apply_picker_choice(kind, value);
}
}
self.mode = Mode::Normal;
}
KeyCode::Down | KeyCode::Char('j')
if !key.modifiers.contains(KeyModifiers::CONTROL) =>
{
if let Some(p) = self.picker.as_mut() {
p.move_selection(1);
}
}
KeyCode::Up | KeyCode::Char('k') if !key.modifiers.contains(KeyModifiers::CONTROL) => {
if let Some(p) = self.picker.as_mut() {
p.move_selection(-1);
}
}
KeyCode::Backspace => {
if let Some(p) = self.picker.as_mut() {
p.filter.pop();
}
}
KeyCode::Char(c) if is_text_input(&key) => {
if let Some(p) = self.picker.as_mut() {
p.filter.push(c);
let filt = p.filtered();
if !filt.iter().any(|i| Some(*i) == p.list_state.selected()) {
p.list_state.select(filt.first().copied());
}
}
}
_ => {}
}
}
}