-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmode_indicator.patch
More file actions
431 lines (422 loc) · 17 KB
/
Copy pathmode_indicator.patch
File metadata and controls
431 lines (422 loc) · 17 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
diff --git a/src/utils/agent_chat/mode_indicator.rs b/src/utils/agent_chat/mode_indicator.rs
new file mode 100644
index 0000000..e8f1a2d
--- /dev/null
+++ b/src/utils/agent_chat/mode_indicator.rs
@@ -0,0 +1,202 @@
+//! Mode indicator and contextual help for chat interface
+//!
+//! Provides visual feedback about current input mode and available actions.
+//! Implements progressive disclosure - help text changes based on context.
+
+use super::Colors;
+use crossterm::terminal;
+use std::io::{self, Write};
+
+/// Current input mode for display purposes
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum DisplayMode {
+ /// Normal input mode - user is typing queries
+ Input,
+ /// Task selection mode - navigating todo list
+ TaskSelection,
+}
+
+/// Render the mode indicator status line
+/// This appears on the right side of the input prompt
+///
+/// # Arguments
+/// * `mode` - Current input mode (Input or TaskSelection)
+/// * `suggestions_visible` - Whether suggestion dropdown is currently shown
+///
+/// # Returns
+/// Formatted string with ANSI color codes for mode indicator and help text
+pub fn render_mode_indicator(mode: DisplayMode, suggestions_visible: bool) -> String {
+ let (cols, _) = terminal::size().unwrap_or((80, 24));
+
+ // Compact version for narrow terminals
+ if cols < 80 {
+ let compact_tag = match mode {
+ DisplayMode::Input => {
+ if suggestions_visible {
+ "[IN↕]" // Arrows indicate suggestions active
+ } else {
+ "[IN]"
+ }
+ }
+ DisplayMode::TaskSelection => "[TK]",
+ };
+ return format!("{}{}{}", Colors::DIM, compact_tag, Colors::RESET);
+ }
+
+ // Full version with contextual help for wider terminals
+ let (mode_tag, help_text) = match mode {
+ DisplayMode::Input => {
+ if suggestions_visible {
+ // Suggestions active - show suggestion navigation help
+ ("[INPUT]", "↑↓ select │ Tab accept │ Esc cancel")
+ } else {
+ // Normal input - show general commands
+ ("[INPUT]", "Ctrl+T tasks │ Ctrl+L clear │ Ctrl+C exit")
+ }
+ }
+ DisplayMode::TaskSelection => {
+ // Task mode - show task-specific commands
+ ("[TASKS]", "↑↓ navigate │ Enter toggle │ Esc exit")
+ }
+ };
+
+ format!(
+ "{}{}{} {}│{} {}",
+ Colors::DIM,
+ mode_tag,
+ Colors::RESET,
+ Colors::DIM,
+ Colors::RESET,
+ help_text
+ )
+}
+
+/// Update the mode indicator in place (right-aligned)
+/// Call this after any mode change or when redrawing input
+///
+/// # Arguments
+/// * `mode` - Current input mode
+/// * `suggestions_visible` - Whether suggestions are shown
+///
+/// # Example
+/// ```no_run
+/// update_mode_indicator_inline(DisplayMode::Input, false)?;
+/// ```
+pub fn update_mode_indicator_inline(
+ mode: DisplayMode,
+ suggestions_visible: bool,
+) -> io::Result<()> {
+ let indicator = render_mode_indicator(mode, suggestions_visible);
+ let (cols, _) = terminal::size().unwrap_or((80, 24));
+
+ // Calculate position: right-align with 2-char margin
+ let indicator_len = strip_ansi_codes(&indicator).chars().count();
+ let padding = cols.saturating_sub(indicator_len as u16 + 2);
+
+ // Save cursor position, move to right side, print, restore cursor
+ print!("\x1B[s"); // Save cursor
+ print!("\x1B[{}G", padding); // Move to column
+ print!("{}", indicator);
+ print!("\x1B[u"); // Restore cursor
+
+ io::stdout().flush()
+}
+
+/// Strip ANSI color codes for accurate length calculation
+/// Required because colored text is longer than it appears on screen
+///
+/// # Arguments
+/// * `text` - String potentially containing ANSI escape codes
+///
+/// # Returns
+/// String with all ANSI codes removed
+fn strip_ansi_codes(text: &str) -> String {
+ let mut result = String::new();
+ let mut in_escape = false;
+
+ for ch in text.chars() {
+ if ch == '\x1B' {
+ in_escape = true;
+ } else if in_escape && (ch == 'm' || ch == 'K') {
+ in_escape = false;
+ } else if !in_escape {
+ result.push(ch);
+ }
+ }
+
+ result
+}
+
+/// Render input box with mode indicator in header
+/// Use this for the full bordered input box
+///
+/// # Arguments
+/// * `mode` - Current input mode
+/// * `suggestions_visible` - Whether suggestions are shown
+///
+/// # Returns
+/// Result indicating success/failure of terminal I/O
+pub fn render_input_box_with_mode(
+ mode: DisplayMode,
+ suggestions_visible: bool,
+) -> io::Result<()> {
+ let (cols, _) = terminal::size().unwrap_or((80, 24));
+ let indicator = render_mode_indicator(mode, suggestions_visible);
+ let indicator_len = strip_ansi_codes(&indicator).chars().count();
+
+ // Calculate border width
+ let border_width = cols.saturating_sub(10) as usize;
+ let title_space = border_width.saturating_sub(indicator_len + 10); // "─ Input " = 8 chars
+
+ // Top border with mode indicator embedded
+ print!(
+ "{}┌─ Input {}",
+ Colors::GREEN,
+ "─".repeat(title_space)
+ );
+
+ // Position indicator on right side of border
+ print!("\x1B[{}G", cols.saturating_sub(indicator_len as u16 + 3));
+ print!("{} {}┐{}", indicator, Colors::GREEN, Colors::RESET);
+
+ // Move to start of next line for input field
+ print!("\n{}│{} > ", Colors::GREEN, Colors::RESET);
+
+ io::stdout().flush()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_strip_ansi_codes() {
+ let colored = "\x1B[32m[INPUT]\x1B[0m Ctrl+C exit";
+ let stripped = strip_ansi_codes(colored);
+ assert_eq!(stripped, "[INPUT] Ctrl+C exit");
+ }
+
+ #[test]
+ fn test_strip_ansi_codes_complex() {
+ let complex = "\x1B[2m[INPUT]\x1B[0m \x1B[2m│\x1B[0m Help text";
+ let stripped = strip_ansi_codes(complex);
+ assert_eq!(stripped, "[INPUT] │ Help text");
+ }
+
+ #[test]
+ fn test_mode_indicator_lengths() {
+ let input_mode = render_mode_indicator(DisplayMode::Input, false);
+ let task_mode = render_mode_indicator(DisplayMode::TaskSelection, false);
+
+ // Ensure both fit in reasonable terminal width
+ assert!(strip_ansi_codes(&input_mode).len() < 70);
+ assert!(strip_ansi_codes(&task_mode).len() < 70);
+ }
+
+ #[test]
+ fn test_suggestions_change_help_text() {
+ let no_suggestions = render_mode_indicator(DisplayMode::Input, false);
+ let with_suggestions = render_mode_indicator(DisplayMode::Input, true);
+
+ assert!(no_suggestions.contains("Ctrl+T"));
+ assert!(with_suggestions.contains("↑↓ select"));
+ assert_ne!(no_suggestions, with_suggestions);
+ }
+}
diff --git a/src/utils/agent_chat/mod.rs b/src/utils/agent_chat/mod.rs
index a1b2c3d..e8f1a2d 100644
--- a/src/utils/agent_chat/mod.rs
+++ b/src/utils/agent_chat/mod.rs
@@ -13,6 +13,7 @@ pub mod fuzzy_matcher;
pub mod input_handler;
pub mod responsive_layout;
pub mod streaming_output;
+pub mod mode_indicator;
pub mod suggestions;
pub mod system_status_bar;
pub mod task_state;
@@ -32,6 +33,7 @@ pub use input_handler::{
};
pub use responsive_layout::{ComponentArea, TerminalRenderer};
pub use streaming_output::{ContextTracker, MultilineInput};
+pub use mode_indicator::{render_mode_indicator, update_mode_indicator_inline, DisplayMode};
pub use suggestions::RealtimeSuggestion;
pub use system_status_bar::SystemStatusBarManager;
pub use task_state::{InputMode, TaskState, TodoItem, TodoPriority};
diff --git a/src/utils/agent_chat/task_state.rs b/src/utils/agent_chat/task_state.rs
index a1b2c3d..e8f1a2d 100644
--- a/src/utils/agent_chat/task_state.rs
+++ b/src/utils/agent_chat/task_state.rs
@@ -14,6 +14,22 @@ pub enum InputMode {
TaskSelection,
}
+impl std::fmt::Display for InputMode {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ InputMode::InputField => write!(f, "Input"),
+ InputMode::TaskSelection => write!(f, "Tasks"),
+ }
+ }
+}
+
+impl From<InputMode> for crate::utils::agent_chat::mode_indicator::DisplayMode {
+ fn from(mode: InputMode) -> Self {
+ match mode {
+ InputMode::InputField => crate::utils::agent_chat::mode_indicator::DisplayMode::Input,
+ InputMode::TaskSelection => crate::utils::agent_chat::mode_indicator::DisplayMode::TaskSelection,
+ }
+ }
+}
/// Priority levels for TODO items
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
diff --git a/src/utils/agent_chat/chat_application.rs b/src/utils/agent_chat/chat_application.rs
index a1b2c3d..e8f1a2d 100644
--- a/src/utils/agent_chat/chat_application.rs
+++ b/src/utils/agent_chat/chat_application.rs
@@ -12,6 +12,7 @@ use crate::services::mcp_service::{McpServerConfig, McpService, McpTool};
use anyhow::{anyhow, Context, Result};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::terminal;
+use super::mode_indicator::{update_mode_indicator_inline, DisplayMode};
use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use serde_json::Value;
@@ -427,9 +428,16 @@ async fn get_enhanced_input_with_status(
input_state.update_mcp_tools_cache(mcp_tools_cache.to_vec());
// Clean input prompt - Claude Code style
- print!("> ");
+ print!("> ");
+
+ // Show mode indicator (right-aligned)
+ let mode: DisplayMode = task_state.input_mode.into();
+ if let Err(e) = update_mode_indicator_inline(mode, false) {
+ debug!("Failed to render mode indicator: {}", e);
+ }
+
io::stdout()
- .flush()
+ .flush()
.map_err(|e| anyhow!("Failed to flush stdout: {}", e))?;
// Enable raw mode for character-by-character input (unless in test/fallback mode)
@@ -536,26 +544,38 @@ async fn get_enhanced_input_with_status(
InputChar::CtrlT => {
// Toggle between input field and task selection
task_state.toggle_input_mode();
+
+ // Get new mode for indicator
+ let new_mode: DisplayMode = task_state.input_mode.into();
match task_state.input_mode {
InputMode::TaskSelection => {
+ // Clear and show task list
clear_suggestions_display();
- task_state.current_reasoning =
- "Task selection mode active. Use ↑↓ to navigate, Enter to toggle."
- .to_string();
- }
- InputMode::InputField => {
- task_state.current_reasoning =
- "Input mode active. Type your command or query.".to_string();
+ print!("\x1B[2J\x1B[1;1H");
+ show_enhanced_status_bar(task_state);
+ show_task_details_below_input(task_state);
+
+ // Show mode indicator
+ if let Err(e) = update_mode_indicator_inline(new_mode, false) {
+ debug!("Failed to update mode indicator: {}", e);
+ }
}
+ InputMode::InputField => {
+ // Return to input mode
+ print!("\x1B[2J\x1B[1;1H");
+ show_enhanced_status_bar(task_state);
+ print!("> {}", input_state.input);
+
+ // Update mode indicator
+ if let Err(e) = update_mode_indicator_inline(new_mode, !input_state.suggestions.is_empty()) {
+ debug!("Failed to update mode indicator: {}", e);
+ }
+ }
}
- // Refresh display
- print!("\x1B[2J\x1B[1;1H");
- show_enhanced_status_bar(task_state);
-
- if task_state.input_mode == InputMode::InputField {
+ // Final positioning
+ if task_state.input_mode == InputMode::InputField {
println!(
"{}┌─ Input ──────────────────────────────────────────────┐{}",
Colors::GREEN,
@@ -574,7 +594,13 @@ async fn get_enhanced_input_with_status(
InputChar::Escape => {
if task_state.input_mode == InputMode::TaskSelection {
// Cancel task selection and return to input mode
- task_state.input_mode = InputMode::InputField;
+ task_state.input_mode = InputMode::InputField;
+ let new_mode: DisplayMode = task_state.input_mode.into();
+
task_state.current_reasoning =
"Cancelled task selection. Returned to input mode.".to_string();
@@ -592,6 +618,11 @@ async fn get_enhanced_input_with_status(
Colors::RESET,
input_state.input
);
+
+ // Update mode indicator
+ if let Err(e) = update_mode_indicator_inline(new_mode, !input_state.suggestions.is_empty()) {
+ debug!("Failed to update mode indicator: {}", e);
+ }
+
io::stdout()
.flush()
.map_err(|e| anyhow!("Failed to flush stdout: {}", e))?;
@@ -608,11 +639,19 @@ async fn get_enhanced_input_with_status(
input_state.suggestions_suppressed = true;
input_state.sug_win_start = 0;
- clear_suggestions_display();
+ clear_suggestions_display();
+
+ // Update indicator (suggestions now hidden)
+ let mode: DisplayMode = task_state.input_mode.into();
+ if let Err(e) = update_mode_indicator_inline(mode, false) {
+ debug!("Failed to update mode indicator: {}", e);
+ }
+
redraw_input_line(&input_state.input)?;
} else {
// No suggestions visible - clear input as before
- input_state.clear();
+ input_state.clear();
+
clear_suggestions_display();
redraw_input_line(&input_state.input)?;
}
@@ -627,12 +666,25 @@ async fn get_enhanced_input_with_status(
InputChar::Arrow(arrow_key) => match task_state.input_mode {
InputMode::InputField => {
handle_arrow_key(&mut input_state, arrow_key).await?;
+
+ // Update indicator if suggestions visibility changed
+ let mode: DisplayMode = task_state.input_mode.into();
+ if let Err(e) = update_mode_indicator_inline(mode, !input_state.suggestions.is_empty()) {
+ debug!("Failed to update mode indicator: {}", e);
+ }
}
InputMode::TaskSelection => match arrow_key {
ArrowKey::Up => {
task_state.navigate_todo_up();
if let Some(selected_task) = task_state.get_selected_task_details() {
task_state.current_reasoning = selected_task.reasoning.clone();
+ }
+
+ // Redraw with mode indicator
+ let mode: DisplayMode = task_state.input_mode.into();
+ if let Err(e) = update_mode_indicator_inline(mode, false) {
+ debug!("Failed to update mode indicator: {}", e);
}
print!("\x1B[2J\x1B[1;1H");
show_enhanced_status_bar(task_state);
@@ -653,10 +705,19 @@ async fn get_enhanced_input_with_status(
},
InputChar::Regular(ch) => {
if task_state.input_mode == InputMode::InputField {
- handle_regular_character(&mut input_state, ch, suggestion_tx).await?;
+ handle_regular_character(&mut input_state, ch, suggestion_tx).await?;
+
+ // Update indicator based on suggestions
+ let mode: DisplayMode = task_state.input_mode.into();
+ if let Err(e) = update_mode_indicator_inline(mode, !input_state.suggestions.is_empty()) {
+ debug!("Failed to update mode indicator: {}", e);
+ }
// Update reasoning based on input
- if input_state.input.len() > 0 {
+ if !input_state.input.is_empty() {
task_state.current_reasoning =
format!("User typing: '{}'", input_state.input);
}