1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-11 10:07:54 -05:00

remove dotcommands

This commit is contained in:
Bartek Iwańczuk 2022-11-26 01:05:15 +01:00
parent 43f1f303e6
commit 295bab82e5
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
2 changed files with 50 additions and 53 deletions

View file

@ -75,18 +75,6 @@ async fn read_eval_file(
Ok((*file.source).to_string()) 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( async fn create_repl_session(
ps: &ProcState, ps: &ProcState,
module_url: ModuleSpecifier, module_url: ModuleSpecifier,
@ -133,6 +121,19 @@ async fn create_repl_session(
Ok(repl_session) Ok(repl_session)
} }
fn save_session_to_file(
maybe_filename: Option<String>,
) -> 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( pub async fn run(
ps: &ProcState, ps: &ProcState,
module_url: ModuleSpecifier, module_url: ModuleSpecifier,
@ -159,7 +160,8 @@ pub async fn run(
let editor = ReplEditor::new(helper, history_file_path)?; let editor = ReplEditor::new(helper, history_file_path)?;
println!("Deno {}", crate::version::deno()); 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<String> = vec![]; let mut session_history: Vec<String> = vec![];
@ -175,38 +177,8 @@ pub async fn run(
should_exit_on_interrupt = false; should_exit_on_interrupt = false;
editor.update_history(line.clone()); 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()); session_history.push(line.to_string());
let output = let output = repl_session.evaluate_line_and_get_output(line).await?;
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 // 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(). // consistent behavior in when the user evaluates a call to close().
@ -216,8 +188,6 @@ pub async fn run(
println!("{}", output); println!("{}", output);
} }
}
}
Err(ReadlineError::Interrupted) => { Err(ReadlineError::Interrupted) => {
if should_exit_on_interrupt { if should_exit_on_interrupt {
break; break;

View file

@ -43,6 +43,33 @@ Object.defineProperty(globalThis, "_error", {
}); });
globalThis.clear = console.clear.bind(console); 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 { pub enum EvaluationOutput {