From: Adrián Arroyo Calle Date: Sun, 6 Dec 2020 22:16:25 +0000 (+0100) Subject: History file support (Rustyline 7.0) X-Git-Tag: v0.9.0~168^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=e2796dd351ebf8cd102de3072ae1980c0b656a4c;p=scryer-prolog.git History file support (Rustyline 7.0) --- diff --git a/Cargo.lock b/Cargo.lock index 3254bf74..a4ee6d4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.11" @@ -211,7 +217,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg 1.0.0", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] @@ -265,17 +271,25 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", + "dirs-sys", +] + +[[package]] +name = "dirs" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff" +dependencies = [ "dirs-sys", ] [[package]] name = "dirs-sys" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b" +checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" dependencies = [ - "cfg-if", "libc", "redox_users", "winapi 0.3.8", @@ -308,6 +322,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "fs2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" +dependencies = [ + "libc", + "winapi 0.3.8", +] + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -355,7 +379,7 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "wasi", ] @@ -478,7 +502,7 @@ version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e0d09e60c187a6d0a3fa418aec8587c6a4ae9de872f6126f2134f319b5ed10d" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "lexical-core", "rustc_version", ] @@ -490,7 +514,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14" dependencies = [ "arrayvec 0.4.12", - "cfg-if", + "cfg-if 0.1.10", "rustc_version", "ryu", "static_assertions", @@ -498,9 +522,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.69" +version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" +checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" [[package]] name = "libsodium-sys" @@ -528,7 +552,7 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -578,7 +602,7 @@ version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "fuchsia-zircon", "fuchsia-zircon-sys", "iovec", @@ -627,7 +651,7 @@ version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "winapi 0.3.8", ] @@ -646,22 +670,21 @@ checksum = "3b2e0b4f3320ed72aaedb9a5ac838690a8047c7b275da22711fddff4f8a14229" dependencies = [ "bitflags", "cc", - "cfg-if", + "cfg-if 0.1.10", "libc", "void", ] [[package]] name = "nix" -version = "0.17.0" +version = "0.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2" dependencies = [ "bitflags", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", - "void", ] [[package]] @@ -753,7 +776,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cee6d85f4cb4c4f59a6a85d5b68a233d280c82e29e822913b9c8b129fbf20bdd" dependencies = [ "bitflags", - "cfg-if", + "cfg-if 0.1.10", "foreign-types", "lazy_static", "libc", @@ -815,7 +838,7 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "cloudabi", "libc", "redox_syscall", @@ -1191,16 +1214,18 @@ dependencies = [ [[package]] name = "rustyline" -version = "6.1.2" +version = "7.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd20b28d972040c627e209eb29f19c24a71a19d661cc5a220089176e20ee202" +checksum = "1a5f54deba50e65ee4cf786dbc37e8b3c63bdccccbcf9d3a8a9fd0c1bb7e1984" dependencies = [ - "cfg-if", - "dirs", + "bitflags", + "cfg-if 1.0.0", + "dirs 3.0.1", + "fs2", "libc", "log", "memchr", - "nix 0.17.0", + "nix 0.19.1", "scopeguard", "unicode-segmentation", "unicode-width", @@ -1239,7 +1264,7 @@ dependencies = [ "chrono", "cpu-time", "crossterm", - "dirs", + "dirs 2.0.2", "divrem", "downcast", "git-version", @@ -1493,7 +1518,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "rand 0.7.3", "redox_syscall", @@ -1613,7 +1638,7 @@ version = "0.2.62" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3c7d40d09cdbf0f4895ae58cf57d92e1e57a9dd8ed2e8390514b54a47cc5551" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "wasm-bindgen-macro", ] diff --git a/Cargo.toml b/Cargo.toml index 02e4ecd7..2b834096 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ ordered-float = "0.5.0" prolog_parser = { version = "0.8.68", default-features = false } ref_thread_local = "0.0.0" rug = { version = "1.4.0", optional = true } -rustyline = "6.0.0" +rustyline = "7.0.0" unicode_reader = "1.0.0" ring = "0.16.13" ripemd160 = "0.8.0" diff --git a/src/read.rs b/src/read.rs index e468dfbd..f879e511 100644 --- a/src/read.rs +++ b/src/read.rs @@ -17,11 +17,13 @@ pub type PrologStream = ParsingStream; pub mod readline { use crate::machine::streams::Stream; use crate::rustyline::error::ReadlineError; - use crate::rustyline::{Cmd, Editor, KeyPress}; + use crate::rustyline::{Cmd, Editor, KeyEvent}; use std::io::{Cursor, Error, ErrorKind, Read}; static mut PROMPT: bool = false; + const HISTORY_FILE: &'static str = ".scryer_history"; + pub fn set_prompt(value: bool) { unsafe { PROMPT = value; @@ -45,7 +47,16 @@ pub mod readline { #[inline] pub fn new(pending_input: String) -> Self { let mut rl = Editor::<()>::new(); - rl.bind_sequence(KeyPress::Tab, Cmd::Insert(1, "\t".to_string())); + if let Some(mut path) = dirs::home_dir() { + path.push(HISTORY_FILE); + if path.exists() { + if rl.load_history(&path).is_err() { + println!("Warning: loading history failed"); + } + } + } + + rl.bind_sequence(KeyEvent::from('\t'), Cmd::Insert(1, "\t".to_string())); ReadlineStream { rl, pending_input: Cursor::new(pending_input) } } @@ -63,6 +74,7 @@ pub mod readline { unsafe { if PROMPT { self.rl.history_mut().add(self.pending_input.get_ref()); + self.save_history(); PROMPT = false; } } @@ -82,6 +94,21 @@ pub mod readline { } } + fn save_history(&mut self) { + if let Some(mut path) = dirs::home_dir() { + path.push(HISTORY_FILE); + if path.exists() { + if self.rl.append_history(&path).is_err() { + println!("Warning: couldn't append history (existing file)"); + } + } else { + if self.rl.save_history(&path).is_err() { + println!("Warning: couldn't save history (new file)"); + } + } + } + } + pub fn peek_byte(&mut self) -> std::io::Result { set_prompt(false);