summaryrefslogtreecommitdiff
path: root/matrix-bin/src/repl.rs
blob: 1b5addca3d3d50df32a0f6f23e73cf8b1ef0caca (plain)
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
use std::{io::Write, sync::atomic::Ordering};

use matrix::{value::Value, vm::Interupt};
use rustyline::Config;

use crate::State;

pub struct Repl<'a> {
    state: State<'a>
}

impl<'a> Repl<'a> {

    pub fn new(state: State<'a>) -> Self {
        Self { state }
    }

    pub fn run(&mut self)  {

        let interupt = self.state.vm.interupt();
        ctrlc::set_handler(move || {
            interupt.store(Interupt::KeyboardInterupt as usize, Ordering::SeqCst);
        }).unwrap();

        let config = Config::builder()
            .check_cursor_position(true)
            .build();
        let mut rl = rustyline::DefaultEditor::with_config(config).unwrap();
        loop {
            let Ok(line) = rl.readline(">> ") else {
                break;
            };
            match self.state.execute(line) {
                Err(err) => crate::error(err),
                Ok(val) => {
                    if val != Value::Nil {
                        println!("{val}");
                    }
                }
            }
            let _ = std::io::stdout().flush();
        }
    }

}