Merge pull request #1342 from wkral/no-add-history

Add --no-add-history flag
This commit is contained in:
Mark Thom
2022-03-11 18:25:47 -07:00
committed by GitHub
7 changed files with 36 additions and 13 deletions

16
src/machine/args.rs Normal file
View File

@@ -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"),
}
}
}

View File

@@ -1,3 +1,4 @@
pub mod args;
pub mod arithmetic_ops; pub mod arithmetic_ops;
pub mod attributed_variables; pub mod attributed_variables;
pub mod code_walker; pub mod code_walker;
@@ -25,6 +26,7 @@ use crate::arithmetic::*;
use crate::atom_table::*; use crate::atom_table::*;
use crate::forms::*; use crate::forms::*;
use crate::instructions::*; use crate::instructions::*;
use crate::machine::args::*;
use crate::machine::compile::*; use crate::machine::compile::*;
use crate::machine::copier::*; use crate::machine::copier::*;
use crate::machine::heap::*; use crate::machine::heap::*;
@@ -396,9 +398,10 @@ impl Machine {
pub fn new() -> Self { pub fn new() -> Self {
use ref_thread_local::RefThreadLocal; use ref_thread_local::RefThreadLocal;
let args = MachineArgs::new();
let mut machine_st = MachineState::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_output = Stream::stdout(&mut machine_st.arena);
let user_error = Stream::stderr(&mut machine_st.arena); let user_error = Stream::stderr(&mut machine_st.arena);

View File

@@ -426,9 +426,9 @@ impl Stream {
} }
#[inline] #[inline]
pub fn stdin(arena: &mut Arena) -> Stream { pub fn stdin(arena: &mut Arena, add_history: bool) -> Stream {
Stream::Readline(arena_alloc!( Stream::Readline(arena_alloc!(
StreamLayout::new(ReadlineStream::new("")), StreamLayout::new(ReadlineStream::new("", add_history)),
arena arena
)) ))
} }

View File

@@ -4094,7 +4094,7 @@ impl Machine {
match result { match result {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(e) => { Err(e) => {
self.user_input = input_stream(&mut self.machine_st.arena); self.user_input.reset();
return Err(e); return Err(e);
} }
} }

View File

@@ -1,7 +1,6 @@
use crate::parser::ast::*; use crate::parser::ast::*;
use crate::parser::parser::*; use crate::parser::parser::*;
use crate::arena::*;
use crate::atom_table::*; use crate::atom_table::*;
use crate::forms::*; use crate::forms::*;
use crate::iterators::*; 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)] #[derive(Debug)]
pub struct ReadlineStream { pub struct ReadlineStream {
rl: Editor<()>, rl: Editor<()>,
pending_input: Cursor<String>, pending_input: Cursor<String>,
add_history: bool,
} }
impl ReadlineStream { impl ReadlineStream {
#[inline] #[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 config = Config::builder().check_cursor_position(true).build();
let mut rl = Editor::<()>::with_config(config); let mut rl = Editor::<()>::with_config(config);
@@ -108,6 +102,7 @@ impl ReadlineStream {
ReadlineStream { ReadlineStream {
rl, rl,
pending_input: Cursor::new(pending_input.to_owned()), pending_input: Cursor::new(pending_input.to_owned()),
add_history: add_history,
} }
} }
@@ -143,6 +138,9 @@ impl ReadlineStream {
} }
fn save_history(&mut self) { fn save_history(&mut self) {
if !self.add_history {
return;
};
if let Some(mut path) = dirs_next::home_dir() { if let Some(mut path) = dirs_next::home_dir() {
path.push(HISTORY_FILE); path.push(HISTORY_FILE);
if path.exists() { if path.exists() {

View File

@@ -57,6 +57,7 @@ delegate_task([Arg0|Args], Goals0) :-
; member(Arg0, ["-v", "--version"]) -> print_version ; member(Arg0, ["-v", "--version"]) -> print_version
; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0)
; member(Arg0, ["-f"]) -> disable_init_file ; member(Arg0, ["-f"]) -> disable_init_file
; member(Arg0, ["--no-add-history"]) -> ignore_machine_arg
; atom_chars(Mod, Arg0), ; atom_chars(Mod, Arg0),
catch(consult(Mod), E, print_exception(E)) catch(consult(Mod), E, print_exception(E))
), ),
@@ -73,7 +74,9 @@ print_help :-
write(' -g, --goal GOAL '), write(' -g, --goal GOAL '),
write('Run the query GOAL'), nl, write('Run the query GOAL'), nl,
write(' -f '), 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(' '), % write(' '),
halt. halt.
@@ -94,6 +97,8 @@ gather_goal(Type, Args0, Goals) :-
disable_init_file :- disable_init_file :-
asserta('disabled_init_file'). asserta('disabled_init_file').
ignore_machine_arg.
arg_type(g). arg_type(g).
arg_type(t). arg_type(t).
arg_type(g(_)). arg_type(g(_)).

View File

@@ -65,6 +65,7 @@ pub fn run_top_level_test_with_args<
Command::cargo_bin(SCRYER_PROLOG) Command::cargo_bin(SCRYER_PROLOG)
.unwrap() .unwrap()
.arg("-f") .arg("-f")
.arg("--no-add-history")
.args(args) .args(args)
.write_stdin(stdin) .write_stdin(stdin)
.assert() .assert()