|
| 1 | +use std::io; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use rustpython_vm::{scope::Scope, VirtualMachine}; |
| 5 | + |
| 6 | +type OtherError = Box<dyn std::error::Error>; |
| 7 | +type OtherResult<T> = Result<T, OtherError>; |
| 8 | + |
| 9 | +pub enum ReadlineResult { |
| 10 | + Line(String), |
| 11 | + EOF, |
| 12 | + Interrupt, |
| 13 | + IO(std::io::Error), |
| 14 | + EncodingError, |
| 15 | + Other(OtherError), |
| 16 | +} |
| 17 | + |
| 18 | +#[allow(unused)] |
| 19 | +mod basic_readline { |
| 20 | + use super::*; |
| 21 | + |
| 22 | + pub struct BasicReadline<'vm> { |
| 23 | + vm: &'vm VirtualMachine, |
| 24 | + } |
| 25 | + |
| 26 | + impl<'vm> BasicReadline<'vm> { |
| 27 | + pub fn new(vm: &'vm VirtualMachine, _scope: Scope) -> Self { |
| 28 | + BasicReadline { vm } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn load_history(&mut self, _path: &Path) -> OtherResult<()> { |
| 32 | + Ok(()) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn save_history(&mut self, _path: &Path) -> OtherResult<()> { |
| 36 | + Ok(()) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn add_history_entry(&mut self, _entry: &str) -> OtherResult<()> { |
| 40 | + Ok(()) |
| 41 | + } |
| 42 | + |
| 43 | + pub fn readline(&mut self, prompt: &str) -> ReadlineResult { |
| 44 | + use std::io::prelude::*; |
| 45 | + print!("{}", prompt); |
| 46 | + if let Err(e) = io::stdout().flush() { |
| 47 | + return ReadlineResult::IO(e); |
| 48 | + } |
| 49 | + |
| 50 | + match io::stdin().lock().lines().next() { |
| 51 | + Some(Ok(line)) => ReadlineResult::Line(line), |
| 52 | + None => ReadlineResult::EOF, |
| 53 | + Some(Err(e)) => match e.kind() { |
| 54 | + io::ErrorKind::Interrupted => ReadlineResult::Interrupt, |
| 55 | + io::ErrorKind::InvalidData => ReadlineResult::EncodingError, |
| 56 | + _ => ReadlineResult::IO(e), |
| 57 | + }, |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(not(target_os = "wasi"))] |
| 64 | +mod rustyline_readline { |
| 65 | + use super::{super::rustyline_helper::ShellHelper, *}; |
| 66 | + |
| 67 | + pub struct RustylineReadline<'vm> { |
| 68 | + repl: rustyline::Editor<ShellHelper<'vm>>, |
| 69 | + } |
| 70 | + |
| 71 | + impl<'vm> RustylineReadline<'vm> { |
| 72 | + pub fn new(vm: &'vm VirtualMachine, scope: Scope) -> Self { |
| 73 | + use rustyline::{At, Cmd, CompletionType, Config, Editor, KeyPress, Movement, Word}; |
| 74 | + let mut repl = Editor::with_config( |
| 75 | + Config::builder() |
| 76 | + .completion_type(CompletionType::List) |
| 77 | + .tab_stop(8) |
| 78 | + .build(), |
| 79 | + ); |
| 80 | + repl.bind_sequence( |
| 81 | + KeyPress::ControlLeft, |
| 82 | + Cmd::Move(Movement::BackwardWord(1, Word::Vi)), |
| 83 | + ); |
| 84 | + repl.bind_sequence( |
| 85 | + KeyPress::ControlRight, |
| 86 | + Cmd::Move(Movement::ForwardWord(1, At::AfterEnd, Word::Vi)), |
| 87 | + ); |
| 88 | + repl.set_helper(Some(ShellHelper::new(vm, scope))); |
| 89 | + RustylineReadline { repl } |
| 90 | + } |
| 91 | + |
| 92 | + pub fn load_history(&mut self, path: &Path) -> OtherResult<()> { |
| 93 | + self.repl.load_history(path)?; |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | + |
| 97 | + pub fn save_history(&mut self, path: &Path) -> OtherResult<()> { |
| 98 | + if !path.exists() { |
| 99 | + if let Some(parent) = path.parent() { |
| 100 | + std::fs::create_dir_all(parent)?; |
| 101 | + } |
| 102 | + } |
| 103 | + self.repl.save_history(path)?; |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | + |
| 107 | + pub fn add_history_entry(&mut self, entry: &str) -> OtherResult<()> { |
| 108 | + self.repl.add_history_entry(entry); |
| 109 | + Ok(()) |
| 110 | + } |
| 111 | + |
| 112 | + pub fn readline(&mut self, prompt: &str) -> ReadlineResult { |
| 113 | + use rustyline::error::ReadlineError; |
| 114 | + match self.repl.readline(prompt) { |
| 115 | + Ok(line) => ReadlineResult::Line(line), |
| 116 | + Err(ReadlineError::Interrupted) => ReadlineResult::Interrupt, |
| 117 | + Err(ReadlineError::Eof) => ReadlineResult::EOF, |
| 118 | + Err(ReadlineError::Io(e)) => ReadlineResult::IO(e), |
| 119 | + #[cfg(unix)] |
| 120 | + Err(ReadlineError::Utf8Error) => ReadlineResult::EncodingError, |
| 121 | + #[cfg(windows)] |
| 122 | + Err(ReadlineError::Decode(_)) => ReadlineResult::EncodingError, |
| 123 | + Err(e) => ReadlineResult::Other(e.into()), |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(target_os = "wasi")] |
| 130 | +type ReadlineInner<'vm> = basic_readline::BasicReadline<'vm>; |
| 131 | + |
| 132 | +#[cfg(not(target_os = "wasi"))] |
| 133 | +type ReadlineInner<'vm> = rustyline_readline::RustylineReadline<'vm>; |
| 134 | + |
| 135 | +pub struct Readline<'vm>(ReadlineInner<'vm>); |
| 136 | + |
| 137 | +impl<'vm> Readline<'vm> { |
| 138 | + pub fn new(vm: &'vm VirtualMachine, scope: Scope) -> Self { |
| 139 | + Readline(ReadlineInner::new(vm, scope)) |
| 140 | + } |
| 141 | + pub fn load_history(&mut self, path: &Path) -> OtherResult<()> { |
| 142 | + self.0.load_history(path) |
| 143 | + } |
| 144 | + pub fn save_history(&mut self, path: &Path) -> OtherResult<()> { |
| 145 | + self.0.save_history(path) |
| 146 | + } |
| 147 | + pub fn add_history_entry(&mut self, entry: &str) -> OtherResult<()> { |
| 148 | + self.0.add_history_entry(entry) |
| 149 | + } |
| 150 | + pub fn readline(&mut self, prompt: &str) -> ReadlineResult { |
| 151 | + self.0.readline(prompt) |
| 152 | + } |
| 153 | +} |
0 commit comments