From 4a489d930cbe727c869b3074b45e1346bf28e64c Mon Sep 17 00:00:00 2001 From: Aspen Smith Date: Mon, 27 May 2024 16:21:23 -0400 Subject: feat(tvix/repl): Add a help command Add a command to display help for the REPL, which can be either :? or :h. Change-Id: Ifdfd8c31130ca5afcde05a4c4276b768eb54c06f Reviewed-on: https://cl.tvl.fyi/c/depot/+/11737 Autosubmit: aspen Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/cli/src/repl.rs | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tvix/cli/src/repl.rs b/tvix/cli/src/repl.rs index 50c1779b0b68..a970aa1ba75d 100644 --- a/tvix/cli/src/repl.rs +++ b/tvix/cli/src/repl.rs @@ -19,17 +19,36 @@ pub enum ReplCommand<'a> { Expr(&'a str), Explain(&'a str), Quit, + Help, } impl<'a> ReplCommand<'a> { + const HELP: &'static str = " +Welcome to the Tvix REPL! + +The following commands are supported: + + Evaluate a Nix language expression and print the result, along with its inferred type + :d Evaluate a Nix language expression and print a detailed description of the result + :q Exit the REPL + :?, :h Display this help text +"; + pub fn parse(input: &'a str) -> Self { - if let Some(without_prefix) = input.strip_prefix(":d ") { - Self::Explain(without_prefix) - } else if input.trim_end() == ":q" { - Self::Quit - } else { - Self::Expr(input) + if input.starts_with(':') { + if let Some(without_prefix) = input.strip_prefix(":d ") { + return Self::Explain(without_prefix); + } + + let input = input.trim_end(); + match input { + ":q" => return Self::Quit, + ":h" | ":?" => return Self::Help, + _ => {} + } } + + Self::Expr(input) } } @@ -91,6 +110,10 @@ impl Repl { let res = match ReplCommand::parse(input) { ReplCommand::Quit => break, + ReplCommand::Help => { + println!("{}", ReplCommand::HELP); + Ok(false) + } ReplCommand::Expr(input) => interpret( Rc::clone(&io_handle), input, -- cgit 1.4.1