2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-14 01:30:38 -05:00
|
|
|
use rustyline;
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::msg::ErrorKind;
|
2018-11-05 12:55:59 -05:00
|
|
|
use std::error::Error;
|
|
|
|
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::deno_dir::DenoDir;
|
|
|
|
use crate::errors::new as deno_error;
|
|
|
|
use crate::errors::DenoResult;
|
2018-11-05 12:55:59 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
use rustyline::Editor;
|
|
|
|
|
|
|
|
// Work around the issue that on Windows, `struct Editor` does not implement the
|
|
|
|
// `Send` trait, because it embeds a windows HANDLE which is a type alias for
|
|
|
|
// *mut c_void. This value isn't actually a pointer and there's nothing that
|
|
|
|
// can be mutated through it, so hack around it. TODO: a prettier solution.
|
|
|
|
#[cfg(windows)]
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
struct Editor<T: rustyline::Helper> {
|
|
|
|
inner: rustyline::Editor<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe impl<T: rustyline::Helper> Send for Editor<T> {}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl<T: rustyline::Helper> Editor<T> {
|
|
|
|
pub fn new() -> Editor<T> {
|
|
|
|
Editor {
|
|
|
|
inner: rustyline::Editor::<T>::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl<T: rustyline::Helper> Deref for Editor<T> {
|
|
|
|
type Target = rustyline::Editor<T>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &rustyline::Editor<T> {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl<T: rustyline::Helper> DerefMut for Editor<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut rustyline::Editor<T> {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Repl {
|
|
|
|
editor: Editor<()>,
|
|
|
|
history_file: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repl {
|
2018-11-05 01:21:21 -05:00
|
|
|
pub fn new(history_file: PathBuf) -> Self {
|
|
|
|
let mut repl = Self {
|
2018-11-05 12:55:59 -05:00
|
|
|
editor: Editor::<()>::new(),
|
|
|
|
history_file,
|
|
|
|
};
|
|
|
|
|
|
|
|
repl.load_history();
|
|
|
|
repl
|
|
|
|
}
|
|
|
|
|
2019-01-15 07:06:25 -05:00
|
|
|
fn load_history(&mut self) {
|
2018-11-05 12:55:59 -05:00
|
|
|
debug!("Loading REPL history: {:?}", self.history_file);
|
|
|
|
self
|
|
|
|
.editor
|
|
|
|
.load_history(&self.history_file.to_str().unwrap())
|
|
|
|
.map_err(|e| debug!("Unable to load history file: {:?} {}", self.history_file, e))
|
|
|
|
// ignore this error (e.g. it occurs on first load)
|
|
|
|
.unwrap_or(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_history(&mut self) -> DenoResult<()> {
|
|
|
|
self
|
|
|
|
.editor
|
|
|
|
.save_history(&self.history_file.to_str().unwrap())
|
|
|
|
.map(|_| debug!("Saved REPL history to: {:?}", self.history_file))
|
|
|
|
.map_err(|e| {
|
|
|
|
eprintln!("Unable to save REPL history: {:?} {}", self.history_file, e);
|
|
|
|
deno_error(ErrorKind::Other, e.description().to_string())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn readline(&mut self, prompt: &str) -> DenoResult<String> {
|
|
|
|
self
|
|
|
|
.editor
|
|
|
|
.readline(&prompt)
|
|
|
|
.map(|line| {
|
|
|
|
self.editor.add_history_entry(line.as_ref());
|
|
|
|
line
|
2019-02-11 14:01:28 -05:00
|
|
|
}).map_err(|e| deno_error(ErrorKind::Other, e.description().to_string()))
|
|
|
|
// Forward error to TS side for processing
|
2018-11-05 12:55:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Repl {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.save_history().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn history_path(dir: &DenoDir, history_file: &str) -> PathBuf {
|
|
|
|
let mut p: PathBuf = dir.root.clone();
|
|
|
|
p.push(history_file);
|
|
|
|
p
|
|
|
|
}
|