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
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"); pub struct Terminal { session: Session, } impl Terminal { pub fn new(session_settings: SessionSettings) -> Terminal { Terminal { session: Session::new(session_settings), } } 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 res = self.session.start(); println!("{}", res); let mut editor = Editor::<()>::new(); let mut ctrl_c_acc = 0; loop { let readline = editor.readline(">> "); match readline { Ok(command) => { let output = self.session.handle_command(&command); for line in output { println!("{}", line); } ctrl_c_acc = 0; editor.add_history_entry(command.as_str()); } 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.txt").unwrap(); } }