rust_bf/commands/
ide.rs

1use std::io::{self, Write};
2use std::path::PathBuf;
3use clap::Args;
4use crate::ide::run_with_options;
5
6#[derive(Args, Debug)]
7#[command(disable_help_flag = true)]
8pub struct IdeArgs {
9    /// Accept a file name to load on startup
10    #[arg(short = 'f', long = "file", value_name = "PATH")]
11    pub filename: Option<String>,
12
13    /// Enable Vi mode (default is Emacs mode)
14    #[arg(short = 'v', long = "vi", action = clap::ArgAction::SetTrue)]
15    pub vi_mode: bool,
16
17    /// Show this help
18    #[arg(short = 'h', long = "help", action = clap::ArgAction::SetTrue)]
19    pub help: bool,
20}
21
22
23// Public entry point for the TUI from main.rs
24pub fn run(program: &str, help: bool, filename: Option<PathBuf>, vi_mode: bool) -> i32 {
25    if help {
26        usage_and_exit(program, 0);
27    } else {
28        let _ = run_with_options(filename, vi_mode);
29    }
30    0
31}
32
33fn usage_and_exit(program: &str, code: i32) -> ! {
34    eprintln!(
35        r#"Usage:
36  {0} ide   # Start a Brainfuck Terminal IDE (read-eval-print loop)
37
38Options:
39  --help,   -h        Show this help
40  --file,   -f        Optional file to load on startup
41  --vi,     -v        Enable Vi mode (default is Emacs mode)
42
43Description:
44  Starts a terminal IDE where you can enter Brainfuck code and execute it live.
45
46Notes:
47    - Non-Brainfuck characters are ignored; only valid instructions are executed.
48    - Ctrl+R executes the current buffer
49    - Ctrl+S saves the current buffer to a file
50    - Ctrl+O opens a file into the current buffer
51    - Ctrl+L toggles line numbers on/off (on by default)
52    - Ctrl+N creates a new empty buffer
53    - Ctrl+P jumps to matching bracket
54    - Ctrl+Q exits the IDE; if there are unsaved changes, you will be asked to confirm.
55"#,
56        program
57    );
58    let _ = io::stderr().flush();
59    std::process::exit(code);
60}