about summary refs log tree commit diff
path: root/tvix/cli/src/main.rs
diff options
context:
space:
mode:
authorAspen Smith <root@gws.fyi>2024-05-27T18·43-0400
committerclbot <clbot@tvl.fyi>2024-06-01T09·04+0000
commit23589a1db34ce81db7ce5ca53e5ba0728a92c4e4 (patch)
tree1d45d4af90224a598608aa93ec3bfe63d352d559 /tvix/cli/src/main.rs
parent96ce9aea046b584c411afdc210e73764f27ff202 (diff)
refactor(tvix): Break out REPL into its own module r/8187
In preparation for adding some new functionality to the tvix REPL, break
it out into its own module.

Change-Id: I4fb78320e92562e3474a3724536cb22c1d893e57
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11733
Tested-by: BuildkiteCI
Autosubmit: aspen <root@gws.fyi>
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/cli/src/main.rs')
-rw-r--r--tvix/cli/src/main.rs104
1 files changed, 5 insertions, 99 deletions
diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs
index 20f6d8fe9774..486de4aca4a9 100644
--- a/tvix/cli/src/main.rs
+++ b/tvix/cli/src/main.rs
@@ -1,5 +1,7 @@
+mod repl;
+
 use clap::Parser;
-use rustyline::{error::ReadlineError, Editor};
+use repl::Repl;
 use std::rc::Rc;
 use std::{fs, path::PathBuf};
 use tracing::Level;
@@ -299,7 +301,8 @@ fn main() {
             std::process::exit(1);
         }
     } else {
-        run_prompt(io_handle, &args)
+        let mut repl = Repl::new();
+        repl.run(io_handle, &args)
     }
 }
 
@@ -335,100 +338,3 @@ fn println_result(result: &Value, raw: bool) {
         println!("=> {} :: {}", result, result.type_of())
     }
 }
-
-fn state_dir() -> Option<PathBuf> {
-    let mut path = dirs::data_dir();
-    if let Some(p) = path.as_mut() {
-        p.push("tvix")
-    }
-    path
-}
-
-fn run_prompt(io_handle: Rc<TvixStoreIO>, args: &Args) {
-    let mut rl = Editor::<()>::new().expect("should be able to launch rustyline");
-
-    if args.compile_only {
-        eprintln!("warning: `--compile-only` has no effect on REPL usage!");
-    }
-
-    let history_path = match state_dir() {
-        // Attempt to set up these paths, but do not hard fail if it
-        // doesn't work.
-        Some(mut path) => {
-            let _ = std::fs::create_dir_all(&path);
-            path.push("history.txt");
-            let _ = rl.load_history(&path);
-            Some(path)
-        }
-
-        None => None,
-    };
-
-    let mut multiline_input: Option<String> = None;
-    loop {
-        let prompt = if multiline_input.is_some() {
-            "         > "
-        } else {
-            "tvix-repl> "
-        };
-
-        let readline = rl.readline(prompt);
-        match readline {
-            Ok(line) => {
-                if line.is_empty() {
-                    continue;
-                }
-
-                let input = if let Some(mi) = &mut multiline_input {
-                    mi.push('\n');
-                    mi.push_str(&line);
-                    mi
-                } else {
-                    &line
-                };
-
-                let res = if let Some(without_prefix) = input.strip_prefix(":d ") {
-                    interpret(
-                        Rc::clone(&io_handle),
-                        without_prefix,
-                        None,
-                        args,
-                        true,
-                        AllowIncomplete::Allow,
-                    )
-                } else {
-                    interpret(
-                        Rc::clone(&io_handle),
-                        input,
-                        None,
-                        args,
-                        false,
-                        AllowIncomplete::Allow,
-                    )
-                };
-
-                match res {
-                    Ok(_) => {
-                        rl.add_history_entry(input);
-                        multiline_input = None;
-                    }
-                    Err(IncompleteInput) => {
-                        if multiline_input.is_none() {
-                            multiline_input = Some(line);
-                        }
-                    }
-                }
-            }
-            Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
-
-            Err(err) => {
-                eprintln!("error: {}", err);
-                break;
-            }
-        }
-    }
-
-    if let Some(path) = history_path {
-        rl.save_history(&path).unwrap();
-    }
-}