From 5f1f07e5a117b159576968b411050ea177e1e1ad Mon Sep 17 00:00:00 2001 From: William Kral Date: Thu, 10 Mar 2022 21:48:41 -0800 Subject: [PATCH] Add --no-add-history flag Flag prevents the input stream from saving terms to ~/.scryer_history when set. Use the flag when running tests to increase test isolation. --- src/machine/args.rs | 16 ++++++++++++++++ src/machine/mod.rs | 5 ++++- src/machine/streams.rs | 4 ++-- src/machine/system_calls.rs | 2 +- src/read.rs | 14 ++++++-------- src/toplevel.pl | 7 ++++++- tests/scryer/helper.rs | 1 + 7 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 src/machine/args.rs diff --git a/src/machine/args.rs b/src/machine/args.rs new file mode 100644 index 00000000..40c355a5 --- /dev/null +++ b/src/machine/args.rs @@ -0,0 +1,16 @@ +use std::collections::BTreeSet; +use std::env; + +#[derive(Debug)] +pub struct MachineArgs { + pub add_history: bool, +} + +impl MachineArgs { + pub fn new() -> Self { + let args: BTreeSet = env::args().collect(); + Self { + add_history: !args.contains("--no-add-history"), + } + } +} diff --git a/src/machine/mod.rs b/src/machine/mod.rs index 19e6d5a1..f5b26e4a 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -1,3 +1,4 @@ +pub mod args; pub mod arithmetic_ops; pub mod attributed_variables; pub mod code_walker; @@ -25,6 +26,7 @@ use crate::arithmetic::*; use crate::atom_table::*; use crate::forms::*; use crate::instructions::*; +use crate::machine::args::*; use crate::machine::compile::*; use crate::machine::copier::*; use crate::machine::heap::*; @@ -396,9 +398,10 @@ impl Machine { pub fn new() -> Self { use ref_thread_local::RefThreadLocal; + let args = MachineArgs::new(); let mut machine_st = MachineState::new(); - let user_input = Stream::stdin(&mut machine_st.arena); + let user_input = Stream::stdin(&mut machine_st.arena, args.add_history); let user_output = Stream::stdout(&mut machine_st.arena); let user_error = Stream::stderr(&mut machine_st.arena); diff --git a/src/machine/streams.rs b/src/machine/streams.rs index 9d76c898..8808313a 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -426,9 +426,9 @@ impl Stream { } #[inline] - pub fn stdin(arena: &mut Arena) -> Stream { + pub fn stdin(arena: &mut Arena, add_history: bool) -> Stream { Stream::Readline(arena_alloc!( - StreamLayout::new(ReadlineStream::new("")), + StreamLayout::new(ReadlineStream::new("", add_history)), arena )) } diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 5e23e19e..07647ce2 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -4094,7 +4094,7 @@ impl Machine { match result { Ok(()) => Ok(()), Err(e) => { - self.user_input = input_stream(&mut self.machine_st.arena); + self.user_input.reset(); return Err(e); } } diff --git a/src/read.rs b/src/read.rs index 72dc9900..256d805b 100644 --- a/src/read.rs +++ b/src/read.rs @@ -1,7 +1,6 @@ use crate::parser::ast::*; use crate::parser::parser::*; -use crate::arena::*; use crate::atom_table::*; use crate::forms::*; use crate::iterators::*; @@ -78,21 +77,16 @@ fn get_prompt() -> &'static str { } } -#[inline] -pub fn input_stream(arena: &mut Arena) -> Stream { - let input_stream = ReadlineStream::new(""); - Stream::from_readline_stream(input_stream, arena) -} - #[derive(Debug)] pub struct ReadlineStream { rl: Editor<()>, pending_input: Cursor, + add_history: bool, } impl ReadlineStream { #[inline] - pub fn new(pending_input: &str) -> Self { + pub fn new(pending_input: &str, add_history: bool) -> Self { let config = Config::builder().check_cursor_position(true).build(); let mut rl = Editor::<()>::with_config(config); @@ -108,6 +102,7 @@ impl ReadlineStream { ReadlineStream { rl, pending_input: Cursor::new(pending_input.to_owned()), + add_history: add_history, } } @@ -143,6 +138,9 @@ impl ReadlineStream { } fn save_history(&mut self) { + if !self.add_history { + return; + }; if let Some(mut path) = dirs_next::home_dir() { path.push(HISTORY_FILE); if path.exists() { diff --git a/src/toplevel.pl b/src/toplevel.pl index 90e0a3a8..e9f45e0e 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -57,6 +57,7 @@ delegate_task([Arg0|Args], Goals0) :- ; member(Arg0, ["-v", "--version"]) -> print_version ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) ; member(Arg0, ["-f"]) -> disable_init_file + ; member(Arg0, ["--no-add-history"]) -> ignore_machine_arg ; atom_chars(Mod, Arg0), catch(consult(Mod), E, print_exception(E)) ), @@ -73,7 +74,9 @@ print_help :- write(' -g, --goal GOAL '), write('Run the query GOAL'), nl, write(' -f '), - write('Fast startup. Do not load initialization file (~/.scryerrc)'),nl, + write('Fast startup. Do not load initialization file (~/.scryerrc)'), nl, + write(' --no-add-history '), + write('Prevent adding input to history file (~/.scryer_history)'), nl, % write(' '), halt. @@ -94,6 +97,8 @@ gather_goal(Type, Args0, Goals) :- disable_init_file :- asserta('disabled_init_file'). +ignore_machine_arg. + arg_type(g). arg_type(t). arg_type(g(_)). diff --git a/tests/scryer/helper.rs b/tests/scryer/helper.rs index 9cb2f0a3..243952f2 100644 --- a/tests/scryer/helper.rs +++ b/tests/scryer/helper.rs @@ -65,6 +65,7 @@ pub fn run_top_level_test_with_args< Command::cargo_bin(SCRYER_PROLOG) .unwrap() .arg("-f") + .arg("--no-add-history") .args(args) .write_stdin(stdin) .assert() -- 2.54.0