From e3f81e8d3b318ab7c1dbaa535e5c5426467615d7 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 19 Dec 2022 12:58:39 +0300 Subject: feat(tvix/cli): add `:d` REPL-prefix to print explanations of values Change-Id: I1dd8de8d996e07840f9b0aaebf932b812103a43a Reviewed-on: https://cl.tvl.fyi/c/depot/+/7593 Reviewed-by: grfn Tested-by: BuildkiteCI --- tvix/cli/src/main.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'tvix/cli') diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs index 3228489e718a..cef51ed19a16 100644 --- a/tvix/cli/src/main.rs +++ b/tvix/cli/src/main.rs @@ -39,7 +39,7 @@ struct Args { /// Interprets the given code snippet, printing out warnings, errors /// and the result itself. The return value indicates whether /// evaluation succeeded. -fn interpret(code: &str, path: Option, args: &Args) -> bool { +fn interpret(code: &str, path: Option, args: &Args, explain: bool) -> bool { let mut eval = tvix_eval::Evaluation::new(code, path); eval.io_handle = Box::new(nix_compat::NixCompatIO::new()); eval.nix_path = args.nix_search_path.clone(); @@ -75,7 +75,11 @@ fn interpret(code: &str, path: Option, args: &Args) -> bool { } if let Some(value) = result.value.as_ref() { - println_result(value, args.raw); + if explain { + println!("=> {}", value.explain()); + } else { + println_result(value, args.raw); + } } // inform the caller about any errors @@ -88,7 +92,7 @@ fn main() { if let Some(file) = &args.script { run_file(file.clone(), &args) } else if let Some(expr) = &args.expr { - if !interpret(expr, None, &args) { + if !interpret(expr, None, &args, false) { std::process::exit(1); } } else { @@ -102,7 +106,7 @@ fn run_file(mut path: PathBuf, args: &Args) { } let contents = fs::read_to_string(&path).expect("failed to read the input file"); - if !interpret(&contents, Some(path), args) { + if !interpret(&contents, Some(path), args, false) { std::process::exit(1); } } @@ -148,7 +152,12 @@ fn run_prompt(args: &Args) { } rl.add_history_entry(&line); - interpret(&line, None, args); + + if let Some(without_prefix) = line.strip_prefix(":d ") { + interpret(without_prefix, None, args, true); + } else { + interpret(&line, None, args, false); + } } Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break, -- cgit 1.4.1