shannonshell 0.1.1

An AI-first shell with seamless access to bash, nushell, and any other shell
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
use std::io;
use std::io::Write;
use std::process::Command;

use chrono::Utc;
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use crossterm::terminal;
use reedline::{
    default_vi_insert_keybindings, default_vi_normal_keybindings, ColumnarMenu, Completer,
    DefaultHinter, EditCommand, HistorySessionId, MenuBuilder, Reedline, ReedlineEvent,
    ReedlineMenu, Signal, Span, SqliteBackedHistory, Suggestion, Vi,
};

use crate::ai::session::Session;
use crate::ai::translate::translate_command;
use crate::completer::ShannonCompleter;
use crate::config::{AiConfig, ShellConfig};
use crate::executor::execute_command;
use crate::highlighter::TreeSitterHighlighter;
use crate::nushell_engine::NushellEngine;
use crate::prompt::{tilde_contract, ShannonPrompt};
use crate::shell::{self, ShellState};
use crate::theme::Theme;

const SWITCH_COMMAND: &str = "__shannon_switch";

pub fn shell_available(binary: &str) -> bool {
    Command::new(binary)
        .arg("--version")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok()
}

/// Completer that returns available shells for the Ctrl+Tab picker menu.
struct ShellSwitchCompleter {
    shells: Vec<String>,
}

impl Completer for ShellSwitchCompleter {
    fn complete(&mut self, _line: &str, _pos: usize) -> Vec<Suggestion> {
        self.shells
            .iter()
            .map(|name| Suggestion {
                value: format!("/switch {name}"),
                display_override: Some(name.clone()),
                description: None,
                style: None,
                extra: None,
                span: Span::new(0, 0),
                append_whitespace: false,
                match_indices: None,
            })
            .collect()
    }
}

fn build_editor(
    shell_config: &ShellConfig,
    session_id: Option<HistorySessionId>,
    ai_mode: bool,
    theme: &Theme,
    shell_names: &[String],
) -> Reedline {
    let mut insert_keybindings = default_vi_insert_keybindings();
    let mut normal_keybindings = default_vi_normal_keybindings();

    for kb in [&mut insert_keybindings, &mut normal_keybindings] {
        kb.add_binding(
            KeyModifiers::SHIFT,
            KeyCode::BackTab,
            ReedlineEvent::ExecuteHostCommand(SWITCH_COMMAND.into()),
        );
        kb.add_binding(
            KeyModifiers::NONE,
            KeyCode::Tab,
            ReedlineEvent::UntilFound(vec![
                ReedlineEvent::Menu("completion_menu".to_string()),
                ReedlineEvent::MenuNext,
            ]),
        );
        kb.add_binding(
            KeyModifiers::CONTROL,
            KeyCode::Char('s'),
            ReedlineEvent::Menu("shell_menu".to_string()),
        );
    }

    insert_keybindings.add_binding(
        KeyModifiers::NONE,
        KeyCode::Right,
        ReedlineEvent::UntilFound(vec![
            ReedlineEvent::HistoryHintComplete,
            ReedlineEvent::Edit(vec![EditCommand::MoveRight { select: false }]),
        ]),
    );

    let edit_mode = Box::new(Vi::new(insert_keybindings, normal_keybindings));

    let history_db = shell::history_db();
    let history = SqliteBackedHistory::with_file(history_db, session_id, Some(Utc::now()))
        .expect("failed to create history database");

    let highlighter = if ai_mode {
        TreeSitterHighlighter::new(None, theme)
    } else {
        TreeSitterHighlighter::new(shell_config.highlighter.as_deref(), theme)
    };

    let hinter = DefaultHinter::default().with_style(theme.hint);

    let completer = Box::new(ShannonCompleter::new());
    let completion_menu = Box::new(ColumnarMenu::default().with_name("completion_menu"));

    let shell_menu = ReedlineMenu::WithCompleter {
        menu: Box::new(reedline::ListMenu::default().with_name("shell_menu")),
        completer: Box::new(ShellSwitchCompleter {
            shells: shell_names.to_vec(),
        }),
    };

    Reedline::create()
        .with_edit_mode(edit_mode)
        .with_history(Box::new(history))
        .with_history_session_id(session_id)
        .with_highlighter(Box::new(highlighter))
        .with_hinter(Box::new(hinter))
        .with_completer(completer)
        .with_menu(ReedlineMenu::EngineCompleter(completion_menu))
        .with_menu(shell_menu)
        .use_bracketed_paste(true)
}

/// Read a single keypress from the terminal (Enter or Esc).
fn read_confirmation() -> io::Result<KeyCode> {
    terminal::enable_raw_mode()?;
    let result = loop {
        if let Event::Key(key_event) = event::read()? {
            match key_event.code {
                KeyCode::Enter | KeyCode::Esc | KeyCode::Char('e') => {
                    break Ok(key_event.code);
                }
                _ => continue,
            }
        }
    };
    terminal::disable_raw_mode()?;
    result
}

/// Emit OSC 7 to report the current working directory to the terminal.
fn emit_osc7(cwd: &std::path::Path) {
    let hostname = std::env::var("HOSTNAME").unwrap_or_else(|_| "localhost".to_string());
    let path = cwd.to_string_lossy();
    let encoded: String = path
        .chars()
        .map(|c| {
            if c.is_ascii_control() || c == ' ' {
                format!("%{:02X}", c as u32)
            } else {
                c.to_string()
            }
        })
        .collect();
    eprint!(
        "\x1b]7;file://{}{}{}\x1b\\",
        hostname,
        if encoded.starts_with('/') { "" } else { "/" },
        encoded
    );
}

/// Emit OSC 2 to set the terminal title (idle — showing shell + cwd).
fn emit_osc2_idle(shell_name: &str, cwd: &std::path::PathBuf) {
    let path = tilde_contract(cwd);
    eprint!("\x1b]2;[{}] {}\x07", shell_name, path);
}

/// Emit OSC 2 to set the terminal title (running a command).
fn emit_osc2_command(shell_name: &str, cwd: &std::path::PathBuf, command: &str) {
    let path = tilde_contract(cwd);
    let binary = command.split_whitespace().next().unwrap_or(command);
    eprint!("\x1b]2;[{}] {}> {}\x07", shell_name, path, binary);
}

/// Handle a `/` meta-command. Returns true if handled, false if the shell should run it.
fn handle_meta_command(
    line: &str,
    shells: &[(String, ShellConfig)],
    active_idx: &mut usize,
    editor: &mut Reedline,
    session_id: Option<HistorySessionId>,
    ai_mode: bool,
    theme: &Theme,
    shell_names: &[String],
) -> bool {
    let parts: Vec<&str> = line.splitn(2, ' ').collect();
    let cmd = parts[0];
    let arg = parts.get(1).map(|s| s.trim()).unwrap_or("");

    // If a file with this path exists, let the shell handle it
    if std::path::Path::new(cmd).exists() {
        return false;
    }

    match cmd {
        "/switch" => {
            if let Some(idx) = shells.iter().position(|(n, _)| n == arg) {
                *active_idx = idx;
                *editor = build_editor(
                    &shells[*active_idx].1,
                    session_id,
                    ai_mode,
                    theme,
                    shell_names,
                );
            } else if !arg.is_empty() {
                eprintln!("shannon: unknown shell: {arg}");
            } else {
                let names: Vec<&str> = shells.iter().map(|(n, _)| n.as_str()).collect();
                eprintln!("Available shells: {}", names.join(", "));
            }
            true
        }
        "/help" => {
            eprintln!("Shannon commands:");
            eprintln!("  /switch <shell>  — switch to a shell");
            eprintln!("  /help            — show this help");
            eprintln!("  Shift+Tab        — cycle to next shell");
            eprintln!("  Ctrl+S           — shell picker menu");
            eprintln!("  Enter (empty)    — toggle AI mode");
            true
        }
        _ => false,
    }
}

/// Execute a command using the nushell engine (for "nu") or wrapper (for others).
fn run_command(
    shell: &(String, ShellConfig),
    command: &str,
    state: &mut ShellState,
    nushell_engine: &mut Option<NushellEngine>,
) {
    if shell.0 == "nu" {
        if let Some(ref mut engine) = nushell_engine {
            engine.inject_state(state);
            *state = engine.execute(command);
            return;
        }
    }
    // Wrapper path for bash/fish/zsh (and fallback for nu without engine)
    match execute_command(&shell.1, command, state) {
        Ok(new_state) => {
            *state = new_state;
        }
        Err(e) => {
            eprintln!("shannon: {e}");
            state.last_exit_code = 1;
        }
    }
}

/// Run the main read-eval-print loop.
pub fn run(
    shells: Vec<(String, ShellConfig)>,
    ai_config: AiConfig,
    mut state: ShellState,
    depth: u32,
    mut nushell_engine: Option<NushellEngine>,
    theme: Theme,
) -> io::Result<()> {
    let session_id = Reedline::create_history_session_id();
    let shell_names: Vec<String> = shells.iter().map(|(n, _)| n.clone()).collect();

    let mut active_idx = 0;
    let mut ai_mode = false;
    let mut editor = build_editor(&shells[active_idx].1, session_id, ai_mode, &theme, &shell_names);
    let mut ai_session: Option<Session> = None;

    loop {
        // Report cwd and title to terminal
        emit_osc7(&state.cwd);
        emit_osc2_idle(&shells[active_idx].0, &state.cwd);

        let prompt = ShannonPrompt {
            shell_name: shells[active_idx].0.clone(),
            cwd: state.cwd.clone(),
            last_exit_code: state.last_exit_code,
            depth,
            ai_mode,
            prompt_color: theme.prompt,
            indicator_color: theme.prompt_indicator,
            error_color: theme.prompt_error,
        };

        match editor.read_line(&prompt) {
            Ok(Signal::Success(line)) => {
                // Shift+Tab: cycle to next shell
                if line == SWITCH_COMMAND {
                    active_idx = (active_idx + 1) % shells.len();
                    editor = build_editor(&shells[active_idx].1, session_id, ai_mode, &theme, &shell_names);
                    continue;
                }

                let line = line.trim();

                // Meta-commands: /switch, /help, etc.
                if line.starts_with('/') {
                    if handle_meta_command(
                        line,
                        &shells,
                        &mut active_idx,
                        &mut editor,
                        session_id,
                        ai_mode,
                        &theme,
                        &shell_names,
                    ) {
                        continue;
                    }
                    // Not a known meta-command — fall through to shell
                }

                // Empty line toggles AI mode
                if line.is_empty() {
                    if ai_mode {
                        ai_mode = false;
                        ai_session = None;
                    } else {
                        ai_mode = true;
                        ai_session = Some(Session::new());
                    }
                    // Rebuild editor to toggle highlighting
                    editor = build_editor(&shells[active_idx].1, session_id, ai_mode, &theme, &shell_names);
                    continue;
                }

                if line == "exit" {
                    break;
                }

                if ai_mode {
                    // AI mode: translate natural language to command
                    eprint!("  Thinking...");
                    io::stderr().flush().ok();

                    let session = ai_session.as_mut().unwrap();
                    let cwd = state.cwd.to_string_lossy().to_string();
                    let shell_name = &shells[active_idx].0;

                    match translate_command(&ai_config, session, shell_name, &cwd, line) {
                        Ok(command) => {
                            // Clear "Thinking..." and show the command
                            eprint!("\r\x1b[K");
                            eprintln!("  \x1b[36m→\x1b[0m {command}");
                            eprintln!("  \x1b[90m[Enter] run  [Esc] cancel\x1b[0m");

                            match read_confirmation()? {
                                KeyCode::Enter => {
                                    eprintln!(); // newline after confirmation
                                    // Run the command through the active shell
                                    emit_osc2_command(&shells[active_idx].0, &state.cwd, &command);
                                    run_command(
                                        &shells[active_idx],
                                        &command,
                                        &mut state,
                                        &mut nushell_engine,
                                    );
                                    emit_osc7(&state.cwd);

                                    // Exit AI mode after execution
                                    ai_mode = false;
                                    ai_session = None;
                                    editor = build_editor(
                                        &shells[active_idx].1,
                                        session_id,
                                        ai_mode,
                                        &theme,
                                        &shell_names,
                                    );
                                }
                                KeyCode::Esc => {
                                    // Cancel — stay in AI mode
                                }
                                _ => {}
                            }
                        }
                        Err(e) => {
                            eprint!("\r\x1b[K");
                            eprintln!("  shannon: {e}");
                        }
                    }
                } else {
                    // Normal mode: execute command directly
                    let cwd = state.cwd.to_string_lossy().to_string();
                    let _ = editor.update_last_command_context(&|mut c| {
                        c.start_timestamp = Some(Utc::now());
                        c.cwd = Some(cwd.clone());
                        c
                    });

                    emit_osc2_command(&shells[active_idx].0, &state.cwd, line);
                    run_command(
                        &shells[active_idx],
                        line,
                        &mut state,
                        &mut nushell_engine,
                    );
                    emit_osc7(&state.cwd);
                }
            }
            Ok(Signal::CtrlD) => break,
            Ok(Signal::CtrlC) => {
                if ai_mode {
                    ai_mode = false;
                    ai_session = None;
                }
                continue;
            }
            Err(e) => {
                eprintln!("shannon: {e}");
                break;
            }
        }
    }

    Ok(())
}