simple_repl/
simple_repl.rs

1// Simple REPL example that echoes what you type with "typed: " prefix
2
3use editline::terminals::StdioTerminal;
4use editline::LineEditor;
5
6fn main() {
7    println!("Simple REPL - Type something and press Enter");
8    println!("Type 'exit' or press Ctrl-D to quit");
9    println!("Features: line editing, history (up/down), word navigation (Ctrl+arrows)");
10    println!("Press Ctrl-C to cancel current line");
11    println!();
12
13    let mut editor = LineEditor::new(1024, 50);
14    let mut terminal = StdioTerminal::new();
15
16    loop {
17        print!("> ");
18        std::io::Write::flush(&mut std::io::stdout()).unwrap();
19
20        match editor.read_line(&mut terminal) {
21            Ok(line) => {
22                if line == "exit" {
23                    println!("Goodbye!");
24                    break;
25                } else if !line.is_empty() {
26                    println!("typed: {}", line);
27                }
28            }
29            Err(e) => {
30                // Handle Ctrl-C and Ctrl-D
31                match e {
32                    editline::Error::Eof => {
33                        // Ctrl-D pressed - exit gracefully
34                        println!("\nGoodbye!");
35                        break;
36                    }
37                    editline::Error::Interrupted => {
38                        // Ctrl-C pressed - show message and continue
39                        println!("\nInterrupted. Type 'exit' or press Ctrl-D to quit.");
40                        continue;
41                    }
42                    _ => {
43                        eprintln!("\nError reading input: {}", e);
44                        break;
45                    }
46                }
47            }
48        }
49    }
50}