about summary refs log tree commit diff
path: root/tvix/cli
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
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')
-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();
+        }
+    }
+}