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
use std::io; use std::path::Path; use rustpython_vm::{scope::Scope, VirtualMachine}; type OtherError = Box<dyn std::error::Error>; type OtherResult<T> = Result<T, OtherError>; pub enum ReadlineResult { Line(String), EOF, Interrupt, IO(std::io::Error), EncodingError, Other(OtherError), } #[allow(unused)] mod basic_readline { use super::*; pub struct BasicReadline<'vm> { vm: &'vm VirtualMachine, } impl<'vm> BasicReadline<'vm> { pub fn new(vm: &'vm VirtualMachine, _scope: Scope) -> Self { BasicReadline { vm } } pub fn load_history(&mut self, _path: &Path) -> OtherResult<()> { Ok(()) } pub fn save_history(&mut self, _path: &Path) -> OtherResult<()> { Ok(()) } pub fn add_history_entry(&mut self, _entry: &str) -> OtherResult<()> { Ok(()) } pub fn readline(&mut self, prompt: &str) -> ReadlineResult { use std::io::prelude::*; print!("{}", prompt); if let Err(e) = io::stdout().flush() { return ReadlineResult::IO(e); } match io::stdin().lock().lines().next() { Some(Ok(line)) => ReadlineResult::Line(line), None => ReadlineResult::EOF, Some(Err(e)) => match e.kind() { io::ErrorKind::Interrupted => ReadlineResult::Interrupt, io::ErrorKind::InvalidData => ReadlineResult::EncodingError, _ => ReadlineResult::IO(e), }, } } } } #[cfg(not(target_os = "wasi"))] mod rustyline_readline { use super::{super::rustyline_helper::ShellHelper, *}; pub struct RustylineReadline<'vm> { repl: rustyline::Editor<ShellHelper<'vm>>, } impl<'vm> RustylineReadline<'vm> { pub fn new(vm: &'vm VirtualMachine, scope: Scope) -> Self { use rustyline::{At, Cmd, CompletionType, Config, Editor, KeyPress, Movement, Word}; let mut repl = Editor::with_config( Config::builder() .completion_type(CompletionType::List) .tab_stop(8) .build(), ); repl.bind_sequence( KeyPress::ControlLeft, Cmd::Move(Movement::BackwardWord(1, Word::Vi)), ); repl.bind_sequence( KeyPress::ControlRight, Cmd::Move(Movement::ForwardWord(1, At::AfterEnd, Word::Vi)), ); repl.set_helper(Some(ShellHelper::new(vm, scope))); RustylineReadline { repl } } pub fn load_history(&mut self, path: &Path) -> OtherResult<()> { self.repl.load_history(path)?; Ok(()) } pub fn save_history(&mut self, path: &Path) -> OtherResult<()> { if !path.exists() { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } } self.repl.save_history(path)?; Ok(()) } pub fn add_history_entry(&mut self, entry: &str) -> OtherResult<()> { self.repl.add_history_entry(entry); Ok(()) } pub fn readline(&mut self, prompt: &str) -> ReadlineResult { use rustyline::error::ReadlineError; match self.repl.readline(prompt) { Ok(line) => ReadlineResult::Line(line), Err(ReadlineError::Interrupted) => ReadlineResult::Interrupt, Err(ReadlineError::Eof) => ReadlineResult::EOF, Err(ReadlineError::Io(e)) => ReadlineResult::IO(e), #[cfg(unix)] Err(ReadlineError::Utf8Error) => ReadlineResult::EncodingError, #[cfg(windows)] Err(ReadlineError::Decode(_)) => ReadlineResult::EncodingError, Err(e) => ReadlineResult::Other(e.into()), } } } } #[cfg(target_os = "wasi")] type ReadlineInner<'vm> = basic_readline::BasicReadline<'vm>; #[cfg(not(target_os = "wasi"))] type ReadlineInner<'vm> = rustyline_readline::RustylineReadline<'vm>; pub struct Readline<'vm>(ReadlineInner<'vm>); impl<'vm> Readline<'vm> { pub fn new(vm: &'vm VirtualMachine, scope: Scope) -> Self { Readline(ReadlineInner::new(vm, scope)) } pub fn load_history(&mut self, path: &Path) -> OtherResult<()> { self.0.load_history(path) } pub fn save_history(&mut self, path: &Path) -> OtherResult<()> { self.0.save_history(path) } pub fn add_history_entry(&mut self, entry: &str) -> OtherResult<()> { self.0.add_history_entry(entry) } pub fn readline(&mut self, prompt: &str) -> ReadlineResult { self.0.readline(prompt) } }