about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-12-19T09·58+0300
committertazjin <tazjin@tvl.su>2022-12-22T19·09+0000
commite3f81e8d3b318ab7c1dbaa535e5c5426467615d7 (patch)
tree38711e9cd1e6b3849282d4afe52646c074cf7a4f
parente306d1d1a1cf5068277b774f586fc22e05dd671f (diff)
feat(tvix/cli): add `:d` REPL-prefix to print explanations of values r/5476
Change-Id: I1dd8de8d996e07840f9b0aaebf932b812103a43a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7593
Reviewed-by: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
-rw-r--r--tvix/cli/src/main.rs19
1 files changed, 14 insertions, 5 deletions
diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs
index 3228489e71..cef51ed19a 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<PathBuf>, args: &Args) -> bool {
+fn interpret(code: &str, path: Option<PathBuf>, 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<PathBuf>, 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,