Skip to content

Commit aadc190

Browse files
committed
feat: complete repl
1 parent 2b5fdc4 commit aadc190

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ members = ["nova_cli", "nova_vm", "common", "small_string", "tests"]
44

55
[workspace.dependencies]
66
clap = { version = "4.5.9", features = ["derive"] }
7+
cliclack = "0.3.2"
8+
console = "0.15.8"
79
fast-float = "0.2.0"
810
miette = { version = "7.2.0", features = ["fancy"] }
911
num-bigint = "0.4.4"

nova_cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ default-run = "nova_cli"
66

77
[dependencies]
88
clap = { workspace = true}
9+
cliclack = { workspace = true }
10+
console = { workspace = true }
911
miette = { workspace = true }
1012
nova_vm = { path = "../nova_vm" }
1113
oxc_ast = { workspace = true }

nova_cli/src/main.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
mod helper;
5+
mod theme;
56

67
use std::{cell::RefCell, collections::VecDeque, fmt::Debug};
78

89
use clap::{Parser as ClapParser, Subcommand};
10+
use cliclack::{input, intro, set_theme};
911
use helper::{exit_with_parse_errors, initialize_global_object};
1012
use nova_vm::ecmascript::{
1113
execution::{
@@ -17,6 +19,7 @@ use nova_vm::ecmascript::{
1719
};
1820
use oxc_parser::Parser;
1921
use oxc_span::SourceType;
22+
use theme::DefaultTheme;
2023

2124
/// A JavaScript engine
2225
#[derive(Debug, ClapParser)] // requires `derive` feature
@@ -49,10 +52,7 @@ enum Command {
4952
},
5053

5154
/// Runs the REPL
52-
Repl {
53-
#[arg(short, long)]
54-
verbose: bool,
55-
},
55+
Repl {},
5656
}
5757

5858
#[derive(Default)]
@@ -165,13 +165,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
165165
}
166166
}
167167
}
168-
Command::Repl { verbose } => {
168+
Command::Repl {} => {
169169
let allocator = Default::default();
170170
let host_hooks: &CliHostHooks = &*Box::leak(Box::default());
171171
let mut agent = Agent::new(
172172
Options {
173173
disable_gc: false,
174-
print_internals: verbose,
174+
print_internals: true,
175175
},
176176
host_hooks,
177177
);
@@ -187,9 +187,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
187187
}
188188
let realm = agent.current_realm_id();
189189

190+
set_theme(DefaultTheme);
191+
println!("\n\n");
192+
let mut placeholder = "Enter a line of Javascript".to_string();
193+
190194
loop {
191-
let mut input = String::new();
192-
std::io::stdin().read_line(&mut input)?;
195+
intro("Nova Repl (type exit or ctrl+c to exit)")?;
196+
let input: String = input("").placeholder(&placeholder).interact()?;
197+
198+
if input.matches("exit").count() == 1 {
199+
std::process::exit(0);
200+
}
201+
placeholder = input.to_string();
193202
let script = match parse_script(&allocator, input.into(), realm, true, None) {
194203
Ok(script) => script,
195204
Err((file, errors)) => {
@@ -199,9 +208,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
199208
let result = script_evaluation(&mut agent, script);
200209
match result {
201210
Ok(result) => {
202-
if verbose {
203-
println!("{:?}", result);
204-
}
211+
println!("{:?}\n", result);
205212
}
206213
Err(error) => {
207214
eprintln!(

nova_cli/src/theme.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use cliclack::{Theme, ThemeState};
2+
use console::Style;
3+
4+
pub struct DefaultTheme;
5+
6+
impl Theme for DefaultTheme {
7+
fn bar_color(&self, _: &ThemeState) -> Style {
8+
Style::new().dim().bold()
9+
}
10+
11+
fn state_symbol_color(&self, _state: &ThemeState) -> Style {
12+
Style::new().cyan()
13+
}
14+
15+
fn info_symbol(&self) -> String {
16+
"⚙".into()
17+
}
18+
}

0 commit comments

Comments
 (0)