]> Repositorios git - scryer-prolog.git/commitdiff
Add --no-add-history flag
authorWilliam Kral <[email protected]>
Fri, 11 Mar 2022 05:48:41 +0000 (21:48 -0800)
committerWilliam Kral <[email protected]>
Fri, 11 Mar 2022 05:48:41 +0000 (21:48 -0800)
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 [new file with mode: 0644]
src/machine/mod.rs
src/machine/streams.rs
src/machine/system_calls.rs
src/read.rs
src/toplevel.pl
tests/scryer/helper.rs

diff --git a/src/machine/args.rs b/src/machine/args.rs
new file mode 100644 (file)
index 0000000..40c355a
--- /dev/null
@@ -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<String> = env::args().collect();
+        Self {
+            add_history: !args.contains("--no-add-history"),
+        }
+    }
+}
index 19e6d5a1cffc607b5f03d6c83e3472af3cf119d4..f5b26e4ab1930e2c1a71bc522537d1f3bcc1fe53 100644 (file)
@@ -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);
 
index 9d76c898c9917d8ddcf626287072c4a74b7d71f6..8808313a9537a1b15673bbccf065dd51f658ece6 100644 (file)
@@ -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
         ))
     }
index 5e23e19ec0e0158217a2096ab882489fd2034301..07647ce22782d21d02a0f01a6a8a32f85a2ff991 100644 (file)
@@ -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);
             }
         }
index 72dc9900f6df8e8f15a2cef0d000a7a6069d332f..256d805b221332ac52e75d7c6fd936dd69a5010c 100644 (file)
@@ -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<String>,
+    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() {
index 90e0a3a8bda6864013aecdb9105710a6de50661f..e9f45e0e7ac74d8466c3b3258e2dc58e8241ccc2 100644 (file)
@@ -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(_)).
index 9cb2f0a373158cfb1bbfd7b49c3c231ade66756a..243952f2a9806c43b809ee9a5042316ac013a384 100644 (file)
@@ -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()