refactor(tvix): Break out REPL into its own module

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>
This commit is contained in:
Aspen Smith 2024-05-27 14:43:06 -04:00 committed by clbot
parent 96ce9aea04
commit 23589a1db3
2 changed files with 122 additions and 99 deletions

View file

@ -1,5 +1,7 @@
mod repl;
use clap::Parser; use clap::Parser;
use rustyline::{error::ReadlineError, Editor}; use repl::Repl;
use std::rc::Rc; use std::rc::Rc;
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use tracing::Level; use tracing::Level;
@ -299,7 +301,8 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
} else { } 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()) 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();
}
}

117
tvix/cli/src/repl.rs Normal file
View file

@ -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();
}
}
}