ironclaw 0.22.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Interactive prompt utilities for the setup wizard.
//!
//! Provides terminal UI components for:
//! - Single selection menus
//! - Multi-select with toggles
//! - Password/secret input (hidden)
//! - Yes/no confirmations
//! - Styled headers and step indicators

use std::io::{self, Write};

use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
    execute,
    style::{Color, Print, ResetColor, SetForegroundColor},
    terminal::{self, ClearType},
};
use secrecy::SecretString;

/// Drain any residual key events already queued in the terminal buffer.
///
/// On Windows, transitioning between raw mode and cooked mode (or between
/// successive raw-mode prompts) can leave stale events (e.g. the Release
/// half of an Enter keypress) in the queue. Consuming them with a
/// non-blocking poll prevents the next prompt from mis-firing.
fn drain_pending_events() {
    while event::poll(std::time::Duration::ZERO).unwrap_or(false) {
        let _ = event::read();
    }
}

/// Display a numbered menu and get user selection.
///
/// Returns the index (0-based) of the selected option.
/// Pressing Enter without input selects the first option (index 0).
///
/// # Example
///
/// ```ignore
/// let choice = select_one("Choose an option:", &["Option A", "Option B"]);
/// ```
pub fn select_one(prompt: &str, options: &[&str]) -> io::Result<usize> {
    let mut stdout = io::stdout();

    // Print prompt
    writeln!(stdout, "{}", prompt)?;
    writeln!(stdout)?;

    // Print options
    for (i, option) in options.iter().enumerate() {
        writeln!(stdout, "  [{}] {}", i + 1, option)?;
    }
    writeln!(stdout)?;

    loop {
        print!("> ");
        stdout.flush()?;

        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let input = input.trim();

        // Handle empty input as first option
        if input.is_empty() {
            return Ok(0);
        }

        // Parse number
        if let Ok(num) = input.parse::<usize>()
            && num >= 1
            && num <= options.len()
        {
            return Ok(num - 1);
        }

        writeln!(
            stdout,
            "Invalid choice. Please enter a number 1-{}.",
            options.len()
        )?;
    }
}

/// Multi-select with space to toggle, enter to confirm.
///
/// `options` is a slice of (label, initially_selected) tuples.
/// Returns indices of selected options.
///
/// # Example
///
/// ```ignore
/// let selected = select_many("Select channels:", &[
///     ("CLI/TUI", true),
///     ("HTTP webhook", false),
///     ("Telegram", false),
/// ])?;
/// ```
pub fn select_many(prompt: &str, options: &[(&str, bool)]) -> io::Result<Vec<usize>> {
    if options.is_empty() {
        return Ok(vec![]);
    }

    let mut stdout = io::stdout();
    let mut selected: Vec<bool> = options.iter().map(|(_, s)| *s).collect();
    let mut cursor_pos = 0;

    terminal::enable_raw_mode()?;
    drain_pending_events();
    execute!(stdout, cursor::Hide)?;

    let result = (|| {
        loop {
            // Clear and redraw
            execute!(stdout, cursor::MoveToColumn(0))?;

            writeln!(stdout, "{}\r", prompt)?;
            writeln!(stdout, "\r")?;
            writeln!(
                stdout,
                "  (Use arrow keys to navigate, space to toggle, enter to confirm)\r"
            )?;
            writeln!(stdout, "\r")?;

            for (i, (label, _)) in options.iter().enumerate() {
                if i == cursor_pos {
                    // Cursor line: cyan cursor, then colored checkbox
                    execute!(stdout, SetForegroundColor(Color::Cyan))?;
                    write!(stdout, "  \u{25b8} ")?;
                    if selected[i] {
                        execute!(stdout, SetForegroundColor(Color::Green))?;
                        write!(stdout, "[\u{2713}]")?;
                    } else {
                        execute!(stdout, SetForegroundColor(Color::DarkGrey))?;
                        write!(stdout, "[\u{00b7}]")?;
                    }
                    execute!(stdout, SetForegroundColor(Color::Cyan))?;
                    writeln!(stdout, " {}\r", label)?;
                    execute!(stdout, ResetColor)?;
                } else {
                    write!(stdout, "    ")?;
                    if selected[i] {
                        execute!(stdout, SetForegroundColor(Color::Green))?;
                        write!(stdout, "[\u{2713}]")?;
                        execute!(stdout, ResetColor)?;
                    } else {
                        execute!(stdout, SetForegroundColor(Color::DarkGrey))?;
                        write!(stdout, "[\u{00b7}]")?;
                        execute!(stdout, ResetColor)?;
                    }
                    writeln!(stdout, " {}\r", label)?;
                }
            }

            stdout.flush()?;

            // Read key — only act on Press events to avoid double-firing
            // from Release/Repeat events on Windows.
            if let Event::Key(KeyEvent {
                code,
                modifiers,
                kind: KeyEventKind::Press,
                ..
            }) = event::read()?
            {
                match code {
                    KeyCode::Up => {
                        cursor_pos = cursor_pos.saturating_sub(1);
                    }
                    KeyCode::Down => {
                        if cursor_pos < options.len() - 1 {
                            cursor_pos += 1;
                        }
                    }
                    KeyCode::Char(' ') => {
                        selected[cursor_pos] = !selected[cursor_pos];
                    }
                    KeyCode::Enter => {
                        break;
                    }
                    KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => {
                        return Err(io::Error::new(io::ErrorKind::Interrupted, "Ctrl-C"));
                    }
                    _ => {}
                }

                // Move cursor up to redraw
                execute!(
                    stdout,
                    cursor::MoveUp((options.len() + 4) as u16),
                    terminal::Clear(ClearType::FromCursorDown)
                )?;
            }
        }
        Ok(())
    })();

    // Cleanup
    execute!(stdout, cursor::Show)?;
    terminal::disable_raw_mode()?;
    writeln!(stdout)?;

    result?;

    Ok(selected
        .iter()
        .enumerate()
        .filter_map(|(i, &s)| if s { Some(i) } else { None })
        .collect())
}

/// Password/secret input with hidden characters.
///
/// # Example
///
/// ```ignore
/// let token = secret_input("Bot token")?;
/// ```
pub fn secret_input(prompt: &str) -> io::Result<SecretString> {
    let mut stdout = io::stdout();

    print!("{}: ", prompt);
    stdout.flush()?;

    terminal::enable_raw_mode()?;
    let result = read_secret_line();
    terminal::disable_raw_mode()?;

    writeln!(stdout)?;
    result
}

fn read_secret_line() -> io::Result<SecretString> {
    let mut input = String::new();
    let mut stdout = io::stdout();

    drain_pending_events();

    loop {
        // Only act on Press events to avoid double-firing from
        // Release/Repeat events on Windows.
        if let Event::Key(KeyEvent {
            code,
            modifiers,
            kind: KeyEventKind::Press,
            ..
        }) = event::read()?
        {
            match code {
                KeyCode::Enter => {
                    break;
                }
                KeyCode::Backspace => {
                    if !input.is_empty() {
                        input.pop();
                        execute!(stdout, Print("\x08 \x08"))?;
                        stdout.flush()?;
                    }
                }
                KeyCode::Char('c') if modifiers.contains(KeyModifiers::CONTROL) => {
                    return Err(io::Error::new(io::ErrorKind::Interrupted, "Ctrl-C"));
                }
                KeyCode::Char(c) => {
                    input.push(c);
                    execute!(stdout, Print('*'))?;
                    stdout.flush()?;
                }
                _ => {}
            }
        }
    }

    Ok(SecretString::from(input))
}

/// Yes/no confirmation prompt.
///
/// # Example
///
/// ```ignore
/// if confirm("Enable Telegram channel?", false)? {
///     // ...
/// }
/// ```
pub fn confirm(prompt: &str, default: bool) -> io::Result<bool> {
    let mut stdout = io::stdout();

    let hint = if default { "[Y/n]" } else { "[y/N]" };
    print!("{} {} ", prompt, hint);
    stdout.flush()?;

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let input = input.trim().to_lowercase();

    Ok(match input.as_str() {
        "" => default,
        "y" | "yes" => true,
        "n" | "no" => false,
        _ => default,
    })
}

/// Print a minimal wordmark banner.
pub fn print_banner() {
    use crate::cli::fmt;
    println!();
    println!("  {}ironclaw{}", fmt::bold_accent(), fmt::reset());
    println!();
}

/// Print a styled header box.
///
/// # Example
///
/// ```ignore
/// print_header("IronClaw Setup Wizard");
/// ```
pub fn print_header(text: &str) {
    let width = text.len() + 4;
    let border = "".repeat(width);

    println!();
    println!("{}", border);
    println!("{}", text);
    println!("{}", border);
    println!();
}

/// Print a compact dot-based step indicator.
///
/// `●` = completed (green/success), `◉` = current (accent), `○` = remaining (dim).
///
/// # Example
///
/// ```ignore
/// print_step(3, 5, "Model Selection");
/// // Output:   ● ● ◉ ○ ○   Model Selection
/// ```
pub fn print_step(current: usize, total: usize, name: &str) {
    use crate::cli::fmt;
    let mut dots = String::new();
    for i in 1..=total {
        if i > 1 {
            dots.push(' ');
        }
        if i < current {
            dots.push_str(&format!("{}\u{25CF}{}", fmt::success(), fmt::reset())); // ● green
        } else if i == current {
            dots.push_str(&format!("{}\u{25C9}{}", fmt::accent(), fmt::reset())); // ◉ accent
        } else {
            dots.push_str(&format!("{}\u{25CB}{}", fmt::dim(), fmt::reset())); // ○ dim
        }
    }
    println!("  {}   {}", dots, name);
    println!();
}

/// Print a success message with green checkmark.
pub fn print_success(message: &str) {
    let mut stdout = io::stdout();
    let _ = execute!(stdout, SetForegroundColor(Color::Green));
    print!("");
    let _ = execute!(stdout, ResetColor);
    println!(" {}", message);
}

/// Print an error message with red X.
pub fn print_error(message: &str) {
    let mut stderr = io::stderr();
    let _ = execute!(stderr, SetForegroundColor(Color::Red));
    eprint!("");
    let _ = execute!(stderr, ResetColor);
    eprintln!(" {}", message);
}

/// Print a warning message with yellow exclamation.
pub fn print_warning(message: &str) {
    let mut stdout = io::stdout();
    let _ = execute!(stdout, SetForegroundColor(Color::Yellow));
    print!("!");
    let _ = execute!(stdout, ResetColor);
    println!(" {}", message);
}

/// Print an info message with blue info icon.
pub fn print_info(message: &str) {
    let mut stdout = io::stdout();
    let _ = execute!(stdout, SetForegroundColor(Color::Blue));
    print!("");
    let _ = execute!(stdout, ResetColor);
    println!(" {}", message);
}

/// Read a simple line of input with a prompt.
pub fn input(prompt: &str) -> io::Result<String> {
    let mut stdout = io::stdout();
    print!("{}: ", prompt);
    stdout.flush()?;

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    Ok(input.trim().to_string())
}

/// Read an optional line of input (empty returns None).
pub fn optional_input(prompt: &str, hint: Option<&str>) -> io::Result<Option<String>> {
    let mut stdout = io::stdout();

    if let Some(h) = hint {
        print!("{} ({}): ", prompt, h);
    } else {
        print!("{}: ", prompt);
    }
    stdout.flush()?;

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let input = input.trim();

    if input.is_empty() {
        Ok(None)
    } else {
        Ok(Some(input.to_string()))
    }
}

#[cfg(test)]
mod tests {
    // Interactive tests are difficult to unit test, but we can test the non-interactive parts.

    #[test]
    fn test_header_length_calculation() {
        // Just verify it doesn't panic with various inputs
        super::print_header("Test");
        super::print_header("A longer header text");
        super::print_header("");
    }

    #[test]
    fn test_step_indicator() {
        super::print_step(1, 3, "Test Step");
        super::print_step(3, 3, "Final Step");
    }

    #[test]
    fn test_print_functions_do_not_panic() {
        super::print_success("operation completed");
        super::print_error("something went wrong");
        super::print_info("here is some information");
        // Also test with empty strings
        super::print_success("");
        super::print_error("");
        super::print_info("");
    }
}