Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustyline for the cli #492

Merged
merged 5 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ yarn-error.log

# tests/js/test.js is used for testing changes locally
tests/js/test.js
.boa_history

# Profiling
*.string_data
Expand Down
124 changes: 124 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2018"

[dependencies]
Boa = { path = "../boa", features = ["serde"] }
rustyline = "6.2.0"
structopt = "0.3.14"
serde_json = "1.0.53"

Expand Down
63 changes: 43 additions & 20 deletions boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ use boa::{
realm::Realm,
syntax::ast::{node::StatementList, token::Token},
};
use std::{
fs::read_to_string,
io::{self, Write},
path::PathBuf,
};
use rustyline::{config::Config, error::ReadlineError, EditMode, Editor};
use std::{fs::read_to_string, path::PathBuf};
use structopt::{clap::arg_enum, StructOpt};

#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))]
Expand All @@ -47,7 +44,8 @@ use structopt::{clap::arg_enum, StructOpt};
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

/// CLI configuration for Boa.
//
static CLI_HISTORY: &str = ".boa_history";

// Added #[allow(clippy::option_option)] because to StructOpt an Option<Option<T>>
// is an optional argument that optionally takes a value ([--opt=[val]]).
// https://docs.rs/structopt/0.3.11/structopt/#type-magic
Expand Down Expand Up @@ -79,6 +77,10 @@ struct Opt {
case_insensitive = true
)]
dump_ast: Option<Option<DumpFormat>>,

/// Use vi mode in the REPL
#[structopt(long = "vi")]
vi_mode: bool,
}

impl Opt {
Expand Down Expand Up @@ -196,26 +198,47 @@ pub fn main() -> Result<(), std::io::Error> {
}

if args.files.is_empty() {
loop {
let mut buffer = String::new();
let config = Config::builder()
.keyseq_timeout(1)
.edit_mode(if args.vi_mode {
EditMode::Vi
} else {
EditMode::Emacs
})
.build();

io::stdin().read_line(&mut buffer)?;
let mut editor = Editor::<()>::with_config(config);
let _ = editor.load_history(CLI_HISTORY);

if args.has_dump_flag() {
match dump(&buffer, &args) {
Ok(_) => {}
Err(e) => eprintln!("{}", e),
loop {
match editor.readline("> ") {
Ok(line) if line == ".exit" => break,
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,

Ok(line) => {
editor.add_history_entry(&line);

if args.has_dump_flag() {
match dump(&line, &args) {
Ok(_) => {}
Err(e) => eprintln!("{}", e),
}
Razican marked this conversation as resolved.
Show resolved Hide resolved
} else {
match forward_val(&mut engine, line.trim_end()) {
Ok(v) => println!("{}", v),
Err(v) => eprintln!("{}", v),
}
}
}
} else {
match forward_val(&mut engine, buffer.trim_end()) {
Ok(v) => println!("{}", v.to_string()),
Err(v) => eprintln!("{}", v.to_string()),

Err(err) => {
eprintln!("Unknown error: {:?}", err);
break;
}
}

// The flush is needed because where in a REPL and we do not want buffering.
std::io::stdout().flush().unwrap();
}

editor.save_history(CLI_HISTORY).unwrap();
}

Ok(())
Expand Down