From 295bab82e5f99a47a9d36be491ea766c346d4f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 26 Nov 2022 01:05:15 +0100 Subject: [PATCH] remove dotcommands --- cli/tools/repl/mod.rs | 76 ++++++++++++--------------------------- cli/tools/repl/session.rs | 27 ++++++++++++++ 2 files changed, 50 insertions(+), 53 deletions(-) diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs index 8467e398a3..175f7ded60 100644 --- a/cli/tools/repl/mod.rs +++ b/cli/tools/repl/mod.rs @@ -75,18 +75,6 @@ async fn read_eval_file( Ok((*file.source).to_string()) } -// TODO(bartlomieju): add .save command -fn print_help() { - let help_text = r#"Available commands: -.help Print this message -.restart Create a new session without exiting the REPL -.exit Exit the REPL -.save Save the current session to a file" -"#; - - print!("{}", help_text); -} - async fn create_repl_session( ps: &ProcState, module_url: ModuleSpecifier, @@ -133,6 +121,19 @@ async fn create_repl_session( Ok(repl_session) } +fn save_session_to_file( + maybe_filename: Option, +) -> Result<(), AnyError> { + // TODO(bartlomieju): make date shorter + let filename = maybe_filename.unwrap_or_else(|| { + format!("./repl-{}.ts", chrono::Local::now().to_rfc3339()) + }); + std::fs::write(&filename, session_history.join("\n")) + .context("Unable to save session file")?; + println!("Saved session to {}", filename); + Ok(()) +} + pub async fn run( ps: &ProcState, module_url: ModuleSpecifier, @@ -159,7 +160,8 @@ pub async fn run( let editor = ReplEditor::new(helper, history_file_path)?; println!("Deno {}", crate::version::deno()); - println!("exit using ctrl+d, ctrl+c, or close()"); + println!("Run repl.help() to see help"); + println!("Exit using ctrl+d, ctrl+c, or close()"); let mut session_history: Vec = vec![]; @@ -175,48 +177,16 @@ pub async fn run( should_exit_on_interrupt = false; editor.update_history(line.clone()); - match line.as_str() { - ".restart" => { - println!( - "Started a new REPL session. Global scope has been reset." - ); - repl_session = create_repl_session( - ps, - module_url.clone(), - maybe_eval_files.clone(), - maybe_eval.clone(), - ) - .await?; - continue; - } - ".help" => { - print_help(); - continue; - } - ".exit" => { - break; - } - ".save" => { - let filename = - format!("./repl-{}.ts", chrono::Local::now().to_rfc3339()); - std::fs::write(&filename, session_history.join("\n")) - .context("Unable to save session file")?; - println!("Saved session to {}", filename); - } - line => { - session_history.push(line.to_string()); - let output = - repl_session.evaluate_line_and_get_output(line).await?; + session_history.push(line.to_string()); + let output = repl_session.evaluate_line_and_get_output(line).await?; - // We check for close and break here instead of making it a loop condition to get - // consistent behavior in when the user evaluates a call to close(). - if repl_session.closing().await? { - break; - } - - println!("{}", output); - } + // We check for close and break here instead of making it a loop condition to get + // consistent behavior in when the user evaluates a call to close(). + if repl_session.closing().await? { + break; } + + println!("{}", output); } Err(ReadlineError::Interrupted) => { if should_exit_on_interrupt { diff --git a/cli/tools/repl/session.rs b/cli/tools/repl/session.rs index f2cdfe568b..472b1313eb 100644 --- a/cli/tools/repl/session.rs +++ b/cli/tools/repl/session.rs @@ -43,6 +43,33 @@ Object.defineProperty(globalThis, "_error", { }); globalThis.clear = console.clear.bind(console); + +globalThis.repl = { + help: () => { + console.log("%cAvailable commands:", "font-style: bold"); + console.log("%crepl.help()", "color: green;", " Print this message"); + console.log("%crepl.reload()", "color: green;", " Create a new session without exiting the REPL"); + console.log("%crepl.exit()", "color: green;", " Exit the REPL"); + console.log("%crepl.save(maybeFilename)", "color: green;", " Save the current session to a file"); + console.log("%cclear()", "color: green;", " Clear screen"); + console.log(""); + console.log("%cAvailable variables:", "font-style: bold"); + console.log("%c_", "color: green;", " Last evaluation result"); + console.log("%c_error", "color: green;", " Last thrown error"); + }, + + reload: () => { + Deno.core.ops.op_repl_reload(); + }, + + exit: () => { + close(); + }, + + save: (maybeFilename) => { + Deno.core.ops.op_repl_save(maybeFilename); + } +} "#; pub enum EvaluationOutput {