use reedline::{DefaultPrompt, Reedline, Signal};

pub fn new_repl(_engine: ()) -> color_eyre::Result<()> {
    let mut line_editor = Reedline::create();
    let prompt = DefaultPrompt::default();

    loop {
        let sig = line_editor.read_line(&prompt);
        match sig {
            Ok(Signal::Success(buffer)) => {
                // Process
                let lexer = magus::lexer::lexer(&buffer);
                for (token, span) in lexer.spanned() {
                    match token {
                        Ok(tok) => {
                            println!(
                                "[{}:{}] {:?} `{}`",
                                span.start,
                                span.end,
                                tok,
                                &buffer[span.start..span.end]
                            )
                        }
                        Err(e) => eprintln!(
                            "[{}:{}] !!{}!! `{}`",
                            span.start,
                            span.end,
                            e,
                            &buffer[span.start..span.end]
                        ),
                    }
                }
            }
            Ok(Signal::CtrlD) | Ok(Signal::CtrlC) => {
                println!("\nAborted!");
                return Ok(());
            }
            x => {
                println!("Event: {:?}", x)
            }
        }
    }
}