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
use crate::repl::{settings::SessionSettings, Session};

use ansi_term::{Colour, Style};
use rustyline::error::ReadlineError;
use rustyline::Editor;
use std::io::{stdin, stdout, Write};

const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
const HISTORY_FILE: Option<&'static str> = option_env!("CLARITY_REPL_HISTORY_FILE");

enum Input<'a> {
    Incomplete(char),
    Complete(Vec<&'a str>),
}

fn complete_input(str: &str) -> Result<Input, (char, char)> {
    let mut forms: Vec<&str> = vec![];
    let mut paren_count = 0;
    let mut last_pos = 0;

    let mut brackets = vec![];
    let mut skip_next = false;
    let mut in_string = false;

    for (pos, character) in str.char_indices() {
        match character {
            '\\' => skip_next = true,
            '"' => {
                if skip_next {
                    skip_next = false
                } else {
                    in_string = !in_string
                }
            }
            '(' | '{' => {
                if !in_string {
                    brackets.push(character);
                    // skip whitespace between the previous form's
                    // closing paren (if there is one) and the current
                    // form's opening paren
                    match (character, paren_count) {
                        ('(', 0) => {
                            paren_count += 1;
                            last_pos = pos
                        }
                        ('(', _) => paren_count += 1,
                        _ => {}
                    }
                }
            }
            ')' | '}' => {
                if !in_string {
                    match (brackets.pop(), character) {
                        (Some('('), '}') => return Err((')', '}')),
                        (Some('{'), ')') => return Err(('}', ')')),
                        _ => {}
                    };
                    if character == ')' {
                        paren_count -= 1;
                        if paren_count == 0 {
                            forms.push(&str[last_pos..pos + 1]);
                        }
                    }
                }
            }
            _ => {}
        }
    }

    match brackets.last() {
        Some(char) => Ok(Input::Incomplete(*char)),
        _ => Ok(Input::Complete(forms)),
    }
}

pub struct Terminal {
    pub session: Session,
}

impl Terminal {
    pub fn new(session_settings: SessionSettings) -> Terminal {
        let mut session = Session::new(session_settings);
        session.is_interactive = true;
        Terminal { session }
    }

    pub fn load(mut session: Session) -> Terminal {
        session.is_interactive = true;
        Terminal { session }
    }

    pub fn start(&mut self) {
        println!("{}", green!(format!("clarity-repl v{}", VERSION.unwrap())));
        println!("{}", black!("Enter \"::help\" for usage hints."));
        println!("{}", black!("Connected to a transient in-memory database."));

        let output = match self.session.display_digest() {
            Ok(output) => output,
            Err(e) => {
                println!("{}", e);
                std::process::exit(1);
            }
        };
        println!("{}", output);
        let mut editor = Editor::<()>::new();
        let mut ctrl_c_acc = 0;
        let mut input_buffer = vec![];
        let mut prompt = String::from(">> ");

        editor
            .load_history(HISTORY_FILE.unwrap_or("history.txt"))
            .ok();
        loop {
            let readline = editor.readline(prompt.as_str());
            match readline {
                Ok(command) => {
                    ctrl_c_acc = 0;
                    input_buffer.push(command);
                    let input = input_buffer.join(" ");
                    match complete_input(&input) {
                        Ok(Input::Complete(forms)) => {
                            let output = self.session.handle_command(&input);
                            for line in output {
                                println!("{}", line);
                            }
                            prompt = String::from(">> ");
                            self.session.executed.push(input.to_string());
                            editor.add_history_entry(input);
                            input_buffer.clear();
                        }
                        Ok(Input::Incomplete(str)) => {
                            prompt = format!("{}.. ", str);
                        }
                        Err((expected, got)) => {
                            println!("Error: expected closing {}, got {}", expected, got);
                            prompt = String::from(">> ");
                            input_buffer.pop();
                        }
                    }
                }
                Err(ReadlineError::Interrupted) => {
                    ctrl_c_acc += 1;
                    if ctrl_c_acc == 2 {
                        break;
                    } else {
                        println!("{}", yellow!("Hit CTRL-C a second time to quit."));
                    }
                }
                Err(ReadlineError::Eof) => {
                    println!("CTRL-D");
                    break;
                }
                Err(err) => {
                    println!("Error: {:?}", err);
                    break;
                }
            }
        }
        editor
            .save_history(HISTORY_FILE.unwrap_or("history.txt"))
            .unwrap();
    }
}