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
use ansi_term::{Color, Style}; use rustyline::completion::{Completer, FilenameCompleter, Pair}; use rustyline::error::ReadlineError; use rustyline::highlight::{Highlighter, MatchingBracketHighlighter}; use rustyline::hint::{Hinter, HistoryHinter}; use rustyline::validate::Validator; use rustyline::validate::{self, MatchingBracketValidator}; use rustyline::Editor; use rustyline::{CompletionType, Config, Context}; use rustyline_derive::Helper; use std::borrow::Cow::{self, Borrowed, Owned}; use crate::Locker; use crate::{parse, CallSnapshot, Environment}; #[derive(Helper)] struct ReplHelper { highlighter: MatchingBracketHighlighter, validator: MatchingBracketValidator, hinter: HistoryHinter, colored_prompt: String, completer: FilenameCompleter, } impl Completer for ReplHelper { type Candidate = Pair; fn complete( &self, line: &str, pos: usize, ctx: &Context<'_>, ) -> Result<(usize, Vec<Pair>), ReadlineError> { self.completer.complete(line, pos, ctx) } } impl Hinter for ReplHelper { fn hint(&self, line: &str, pos: usize, ctx: &Context<'_>) -> Option<String> { self.hinter.hint(line, pos, ctx) } } impl Highlighter for ReplHelper { fn highlight_prompt<'b, 's: 'b, 'p: 'b>( &'s self, prompt: &'p str, default: bool, ) -> Cow<'b, str> { if default { Borrowed(&self.colored_prompt) } else { Borrowed(prompt) } } fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { Owned(Style::new().dimmed().paint(hint).to_string()) } fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { self.highlighter.highlight(line, pos) } fn highlight_char(&self, line: &str, pos: usize) -> bool { self.highlighter.highlight_char(line, pos) } } impl Validator for ReplHelper { fn validate( &self, ctx: &mut validate::ValidationContext, ) -> rustyline::Result<validate::ValidationResult> { self.validator.validate(ctx) } fn validate_while_typing(&self) -> bool { self.validator.validate_while_typing() } } pub fn spawn(env: Locker<Environment>) { let config = Config::builder() .history_ignore_space(true) .completion_type(CompletionType::List) .build(); let h = ReplHelper { completer: FilenameCompleter::new(), highlighter: MatchingBracketHighlighter::new(), hinter: HistoryHinter {}, colored_prompt: "".to_owned(), validator: MatchingBracketValidator::new(), }; let mut rl = Editor::with_config(config); rl.set_helper(Some(h)); if rl.load_history(".turtle_history.txt").is_err() { println!("It looks like this is your first time running Turtle from this directory; no history was loaded.") } loop { let p = "🐢 > ".to_string(); rl.helper_mut().expect("No helper").colored_prompt = Color::Green.bold().paint(&p).to_string(); let line = rl.readline(&p); match line { Ok(line) => { rl.add_history_entry(line.as_str()); match parse(line.as_str(), "<stdin>") { Ok(values) => { for value in values { let snapshot = CallSnapshot::root(&value.clone()); match value .eval_async(snapshot, env.clone()) .unwrap() .recv() .unwrap() { Ok(result) => println!( " {} {}", Color::Blue.bold().paint("="), Style::default().bold().paint(format!("{}", result)) ), Err(error) => eprintln!("{}", error), } } } Err(err) => eprintln!("{:#}", err), } } Err(ReadlineError::Interrupted) => break, Err(ReadlineError::Eof) => break, Err(err) => { eprintln!("Error: {:?}", err); break; } } rl.save_history(".turtle_history.txt").unwrap(); } }