askr 0.1.7

Interactive CLI input tool with real-time validation and choice menus
Documentation
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
use super::{ColorScheme, Colorizer, LayoutManager, Screen, Terminal};
use crate::error::{PromptError, Result};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use std::io::{self, stderr};
use std::time::Duration;

pub struct ChoiceMenu {
    terminal: Terminal,
    choices: Vec<String>,
    allow_multiple: bool,
    min_choices: usize,
    max_choices: usize,
    selected_choices: Vec<bool>,
    current_index: usize,
    colorizer: Colorizer,
    validation_error: Option<String>,
    last_content_lines: u16, // Track how many content lines were drawn last time
    timeout: Duration,
    has_defaults: bool,        // Track if defaults were provided
    user_has_interacted: bool, // Track if user has made any selections/deselections
}

impl ChoiceMenu {
    pub fn new(
        mut terminal: Terminal,
        choices: Vec<String>,
        allow_multiple: bool,
        min_choices: usize,
        max_choices: usize,
        no_color: bool,
        timeout: Duration,
        default_selections: Vec<String>,
    ) -> Result<Self> {
        // Only enter raw mode if we have cursor control
        if terminal.capabilities().cursor_control {
            terminal.enter_raw_mode()?;
        }

        let color_scheme = if no_color {
            ColorScheme::no_color()
        } else {
            ColorScheme::default()
        };

        let colorizer = Colorizer::new(color_scheme, no_color);
        let mut selected_choices = vec![false; choices.len()];
        let has_defaults = !default_selections.is_empty();

        // Preselect default choices
        for default_choice in &default_selections {
            if let Some(index) = choices.iter().position(|choice| choice == default_choice) {
                selected_choices[index] = true;
            }
        }

        Ok(Self {
            terminal,
            choices,
            allow_multiple,
            min_choices,
            max_choices,
            selected_choices,
            current_index: 0,
            colorizer,
            validation_error: None,
            last_content_lines: 0,
            timeout,
            has_defaults,
            user_has_interacted: false,
        })
    }

    pub fn show(&mut self, prompt_text: &str) -> Result<Vec<String>> {
        use crossterm::{terminal::Clear, terminal::ClearType, ExecutableCommand};
        use std::io::stderr;

        // Clear any existing content on the current line first
        stderr().execute(Clear(ClearType::CurrentLine))?;

        let (width, height) = self.terminal.size()?;
        let layout = LayoutManager::new(width, height);
        let mut screen = Screen::new(stderr(), layout, self.colorizer.clone());

        // Calculate space needed and reserve it
        let reserved_lines = self.calculate_and_reserve_space(width, prompt_text)?;

        // Move cursor to the reserved prompt position
        self.move_to_prompt_position(reserved_lines)?;

        // Validate initial state and draw menu
        self.validate_selections();
        self.draw_menu(&mut screen, prompt_text)?;
        self.update_content_line_count();

        // Input loop
        loop {
            screen.flush()?;

            if event::poll(self.timeout)? {
                if let Event::Key(key_event) = event::read()? {
                    match self.handle_key_event(key_event)? {
                        MenuAction::Continue => {
                            // Validate selections after any change
                            self.validate_selections();
                            // Clear the area and redraw
                            self.clear_and_redraw(&mut screen, prompt_text)?;
                        }
                        MenuAction::Submit => {
                            // Only submit if constraints are met
                            if self.can_submit() {
                                let selected = self.get_selected_choices();
                                return Ok(selected);
                            } else {
                                // Validation failed, update error and redraw
                                self.validate_selections();
                                self.clear_and_redraw(&mut screen, prompt_text)?;
                            }
                        }
                        MenuAction::Cancel => {
                            return Err(PromptError::Interrupted);
                        }
                    }
                }
            } else {
                return Err(PromptError::Timeout);
            }
        }
    }

    fn handle_key_event(&mut self, key_event: KeyEvent) -> Result<MenuAction> {
        match key_event {
            // Navigation with arrow keys
            KeyEvent {
                code: KeyCode::Up, ..
            } => {
                if self.current_index > 0 {
                    self.current_index -= 1;
                    self.user_has_interacted = true;
                }
                Ok(MenuAction::Continue)
            }
            KeyEvent {
                code: KeyCode::Down,
                ..
            } => {
                if self.current_index < self.choices.len() - 1 {
                    self.current_index += 1;
                    self.user_has_interacted = true;
                }
                Ok(MenuAction::Continue)
            }

            // Selection
            KeyEvent {
                code: KeyCode::Enter,
                ..
            } => {
                if self.allow_multiple {
                    // In multiple choice mode, Enter submits current selections
                    Ok(MenuAction::Submit)
                } else {
                    // In single choice mode, check if we have defaults and user hasn't interacted
                    if self.has_defaults && !self.user_has_interacted {
                        // Submit with defaults (already preselected)
                        Ok(MenuAction::Submit)
                    } else {
                        // Select current item and submit
                        self.selected_choices.fill(false);
                        self.selected_choices[self.current_index] = true;
                        Ok(MenuAction::Submit)
                    }
                }
            }

            // Toggle selection with spacebar (for multiple choice)
            KeyEvent {
                code: KeyCode::Char(' '),
                ..
            } => {
                if self.allow_multiple {
                    self.selected_choices[self.current_index] =
                        !self.selected_choices[self.current_index];
                    self.user_has_interacted = true;
                }
                Ok(MenuAction::Continue)
            }

            // Cancel with Ctrl+C
            KeyEvent {
                code: KeyCode::Char('c'),
                modifiers: KeyModifiers::CONTROL,
                ..
            } => Ok(MenuAction::Cancel),

            // Cancel with Ctrl+D (EOF)
            KeyEvent {
                code: KeyCode::Char('d'),
                modifiers: KeyModifiers::CONTROL,
                ..
            } => Ok(MenuAction::Cancel),

            // Ignore other keys
            _ => Ok(MenuAction::Continue),
        }
    }

    fn draw_menu(&self, screen: &mut Screen<io::Stderr>, prompt_text: &str) -> Result<()> {
        use crossterm::{cursor::MoveToNextLine, ExecutableCommand};

        // Draw prompt at current position (which should be the reserved position)
        let _prompt_width = screen.write_prompt(prompt_text)?;

        // Move to next line for instructions and choices
        stderr().execute(MoveToNextLine(1))?;

        // Draw the menu content
        self.draw_menu_content()?;

        Ok(())
    }

    fn clear_and_redraw(
        &mut self,
        screen: &mut Screen<io::Stderr>,
        prompt_text: &str,
    ) -> Result<()> {
        use crossterm::{
            cursor::{MoveToColumn, MoveUp},
            terminal::Clear,
            terminal::ClearType,
            ExecutableCommand,
        };

        // Move back using the tracked number of content lines from last draw
        // Add 1 to account for the prompt line
        if self.last_content_lines > 0 {
            stderr().execute(MoveUp(self.last_content_lines + 1))?;
        }

        // Move to the beginning of the line to ensure proper cursor positioning
        stderr().execute(MoveToColumn(0))?;

        // Clear from cursor down to remove old menu content
        stderr().execute(Clear(ClearType::FromCursorDown))?;

        // Redraw the full menu (prompt + instruction + choices + error)
        self.draw_menu(screen, prompt_text)?;

        // Update the line count for next time
        self.update_content_line_count();

        Ok(())
    }

    /// Calculate and store how many lines the current content occupies
    fn update_content_line_count(&mut self) {
        let mut lines = 1; // instruction line
        lines += self.choices.len() as u16; // choice lines
        if self.validation_error.is_some() {
            lines += 1; // error line
        }
        // Note: prompt line is handled separately in clear_and_redraw
        self.last_content_lines = lines;
    }

    fn draw_menu_content(&self) -> Result<()> {
        use crossterm::{cursor::MoveToNextLine, ExecutableCommand};

        // Add instruction text
        let instruction = if self.allow_multiple {
            if self.min_choices == self.max_choices {
                format!("Select exactly {} choice(s). Use ↑↓ to navigate, SPACE to toggle, ENTER to submit:", self.min_choices)
            } else {
                format!(
                    "Select {}-{} choice(s). Use ↑↓ to navigate, SPACE to toggle, ENTER to submit:",
                    self.min_choices, self.max_choices
                )
            }
        } else {
            "Use ↑↓ to navigate, ENTER to select:".to_string()
        };

        // Write instruction
        let colored_instruction = self.colorizer.help_text(&instruction);
        self.colorizer
            .write_colored(&mut std::io::stderr(), &colored_instruction)?;
        stderr().execute(MoveToNextLine(1))?;

        // Draw choices
        for (i, choice) in self.choices.iter().enumerate() {
            let is_current = i == self.current_index;
            let is_selected = self.selected_choices[i];

            let marker = if self.allow_multiple {
                if is_selected {
                    "[✓]"
                } else {
                    "[ ]"
                }
            } else if is_current {
                ">"
            } else {
                " "
            };

            let choice_text = format!("{} {}", marker, choice);

            if is_current {
                let colored_choice = self.colorizer.highlighted_text(&choice_text);
                self.colorizer
                    .write_colored(&mut std::io::stderr(), &colored_choice)?;
            } else {
                let colored_choice = self.colorizer.valid_text(&choice_text);
                self.colorizer
                    .write_colored(&mut std::io::stderr(), &colored_choice)?;
            }
            stderr().execute(MoveToNextLine(1))?;
        }

        // Display validation error if present
        if let Some(error_message) = &self.validation_error {
            stderr().execute(MoveToNextLine(1))?;
            let colored_error = self.colorizer.error_message(error_message);
            self.colorizer
                .write_colored(&mut std::io::stderr(), &colored_error)?;
        }

        Ok(())
    }

    fn get_selected_choices(&self) -> Vec<String> {
        self.selected_choices
            .iter()
            .enumerate()
            .filter_map(|(i, &selected)| {
                if selected {
                    Some(self.choices[i].clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Validate current selections against min/max constraints
    fn validate_selections(&mut self) {
        let selected_count = self.selected_choices.iter().filter(|&&s| s).count();

        self.validation_error = if selected_count < self.min_choices {
            Some(format!("At least {} choice(s) required", self.min_choices))
        } else if selected_count > self.max_choices {
            Some(format!("At most {} choice(s) allowed", self.max_choices))
        } else {
            None
        };
    }

    /// Check if current selections meet the constraints for submission
    fn can_submit(&self) -> bool {
        let selected_count = self.selected_choices.iter().filter(|&&s| s).count();
        selected_count >= self.min_choices && selected_count <= self.max_choices
    }

    fn calculate_and_reserve_space(&self, _width: u16, _prompt_text: &str) -> Result<u16> {
        use std::io::{self, Write};

        let mut total_lines = 0u16;

        // 1. Prompt line
        total_lines += 1;

        // 2. Instruction line
        total_lines += 1;

        // 3. Choice lines
        total_lines += self.choices.len() as u16;

        // 4. Validation error line (if present)
        total_lines += 1;

        // 5. Buffer for spacing
        total_lines += 2;

        // Ensure we don't try to reserve more lines than the terminal height
        let (_, terminal_height) = self.terminal.size()?;
        let max_reservable = terminal_height.saturating_sub(2);
        total_lines = total_lines.min(max_reservable);

        // Print blank lines to reserve the space
        let mut stderr = io::stderr();
        for _ in 0..total_lines {
            writeln!(stderr)?;
        }
        stderr.flush()?;

        Ok(total_lines)
    }

    fn move_to_prompt_position(&mut self, reserved_lines: u16) -> Result<()> {
        use crossterm::{cursor::MoveUp, ExecutableCommand};
        use std::io::stderr;

        // Move cursor back up to where we want to start the prompt
        stderr().execute(MoveUp(reserved_lines))?;

        Ok(())
    }
}

impl Drop for ChoiceMenu {
    fn drop(&mut self) {
        // Clean up terminal state only if we have cursor control
        if self.terminal.capabilities().cursor_control {
            let _ = self.terminal.leave_raw_mode();
        }
    }
}

#[derive(Debug)]
enum MenuAction {
    Continue,
    Submit,
    Cancel,
}