about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--tvix/cli/src/main.rs104
-rw-r--r--tvix/cli/src/repl.rs117
2 files changed, 122 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();
-    }
-}
diff --git a/tvix/cli/src/repl.rs b/tvix/cli/src/repl.rs
new file mode 100644
index 000000000000..8dde9d9bfdc0
--- /dev/null
+++ b/tvix/cli/src/repl.rs
@@ -0,0 +1,117 @@
+use std::{path::PathBuf, rc::Rc};
+
+use rustyline::{error::ReadlineError, Editor};
+use tvix_glue::tvix_store_io::TvixStoreIO;
+
+use crate::{interpret, AllowIncomplete, Args, IncompleteInput};
+
+fn state_dir() -> Option<PathBuf> {
+    let mut path = dirs::data_dir();
+    if let Some(p) = path.as_mut() {
+        p.push("tvix")
+    }
+    path
+}
+
+#[derive(Debug)]
+pub struct Repl {
+    /// In-progress multiline input, when the input so far doesn't parse as a complete expression
+    multiline_input: Option<String>,
+    rl: Editor<()>,
+}
+
+impl Repl {
+    pub fn new() -> Self {
+        let rl = Editor::<()>::new().expect("should be able to launch rustyline");
+        Self {
+            multiline_input: None,
+            rl,
+        }
+    }
+
+    pub fn run(&mut self, io_handle: Rc<TvixStoreIO>, args: &Args) {
+        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 _ = self.rl.load_history(&path);
+                Some(path)
+            }
+
+            None => None,
+        };
+
+        loop {
+            let prompt = if self.multiline_input.is_some() {
+                "         > "
+            } else {
+                "tvix-repl> "
+            };
+
+            let readline = self.rl.readline(prompt);
+            match readline {
+                Ok(line) => {
+                    if line.is_empty() {
+                        continue;
+                    }
+
+                    let input = if let Some(mi) = &mut self.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(_) => {
+                            self.rl.add_history_entry(input);
+                            self.multiline_input = None;
+                        }
+                        Err(IncompleteInput) => {
+                            if self.multiline_input.is_none() {
+                                self.multiline_input = Some(line);
+                            }
+                        }
+                    }
+                }
+                Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,
+
+                Err(err) => {
+                    eprintln!("error: {}", err);
+                    break;
+                }
+            }
+        }
+
+        if let Some(path) = history_path {
+            self.rl.save_history(&path).unwrap();
+        }
+    }
+}