forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.rs
72 lines (64 loc) · 1.73 KB
/
repl.rs
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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::deno_dir::DenoDir;
use crate::op_error::OpError;
use deno_core::ErrBox;
use rustyline::Editor;
use std::fs;
use std::path::PathBuf;
pub struct Repl {
editor: Editor<()>,
history_file: PathBuf,
}
impl Repl {
pub fn new(history_file: PathBuf) -> Self {
let mut repl = Self {
editor: Editor::<()>::new(),
history_file,
};
repl.load_history();
repl
}
fn load_history(&mut self) {
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) -> Result<(), ErrBox> {
fs::create_dir_all(self.history_file.parent().unwrap())?;
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);
ErrBox::from(e)
})
}
pub fn readline(&mut self, prompt: &str) -> Result<String, OpError> {
self
.editor
.readline(&prompt)
.map(|line| {
self.editor.add_history_entry(line.clone());
line
})
.map_err(OpError::from)
// Forward error to TS side for processing
}
}
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
}