diff --git a/boa_cli/src/helper.rs b/boa_cli/src/helper.rs index 5cdb2e15783..dfd1301dc68 100644 --- a/boa_cli/src/helper.rs +++ b/boa_cli/src/helper.rs @@ -7,7 +7,7 @@ use rustyline::{ validate::{MatchingBracketValidator, ValidationContext, ValidationResult, Validator}, Completer, Helper, Hinter, }; -use std::borrow::Cow; +use std::borrow::Cow::{self, Borrowed}; const STRING_COLOR: Color = Color::Green; const KEYWORD_COLOR: Color = Color::Yellow; @@ -33,18 +33,22 @@ const IDENTIFIER_COLOR: Color = Color::TrueColor { b: 214, }; +const READLINE_COLOR: Color = Color::Cyan; + #[allow(clippy::upper_case_acronyms)] #[derive(Completer, Helper, Hinter)] pub(crate) struct RLHelper { highlighter: LineHighlighter, validator: MatchingBracketValidator, + colored_prompt: String, } impl RLHelper { - pub(crate) fn new() -> Self { + pub(crate) fn new(prompt: &str) -> Self { Self { highlighter: LineHighlighter, validator: MatchingBracketValidator::new(), + colored_prompt: prompt.color(READLINE_COLOR).bold().to_string(), } } } @@ -63,14 +67,28 @@ impl Validator for RLHelper { } impl Highlighter for RLHelper { - fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { - hint.into() - } - fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { self.highlighter.highlight(line, pos) } + // Must match signature of Highlighter::highlight_prompt, can't elide lifetimes. + #[allow(single_use_lifetimes)] + fn highlight_prompt<'b, 's: 'b, 'p: 'b>( + &'s self, + prompt: &'p str, + default: bool, + ) -> Cow<'b, str> { + if default { + Borrowed(&self.colored_prompt) + } else { + Borrowed(prompt) + } + } + + fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { + hint.into() + } + fn highlight_candidate<'c>( &self, candidate: &'c str, diff --git a/boa_cli/src/main.rs b/boa_cli/src/main.rs index 723d8c44f01..fc0e45db1d8 100644 --- a/boa_cli/src/main.rs +++ b/boa_cli/src/main.rs @@ -77,7 +77,7 @@ use boa_engine::{ }; use boa_runtime::Console; use clap::{Parser, ValueEnum, ValueHint}; -use colored::{Color, Colorize}; +use colored::Colorize; use debug::init_boa_debug_object; use rustyline::{config::Config, error::ReadlineError, EditMode, Editor}; use std::{ @@ -95,8 +95,6 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; /// CLI configuration for Boa. static CLI_HISTORY: &str = ".boa_history"; -const READLINE_COLOR: Color = Color::Cyan; - // Added #[allow(clippy::option_option)] because to StructOpt an Option> // is an optional argument that optionally takes a value ([--opt=[val]]). // https://docs.rs/structopt/0.3.11/structopt/#type-magic @@ -419,12 +417,11 @@ fn main() -> Result<(), io::Error> { ReadlineError::Io(e) => e, e => io::Error::new(io::ErrorKind::Other, e), })?; - editor.set_helper(Some(helper::RLHelper::new())); - - let readline = ">> ".color(READLINE_COLOR).bold().to_string(); + let readline = ">> "; + editor.set_helper(Some(helper::RLHelper::new(readline))); loop { - match editor.readline(&readline) { + match editor.readline(readline) { Ok(line) if line == ".exit" => break, Err(ReadlineError::Interrupted | ReadlineError::Eof) => break, diff --git a/boa_engine/src/optimizer/mod.rs b/boa_engine/src/optimizer/mod.rs index 9c8a6915c57..e675a72a718 100644 --- a/boa_engine/src/optimizer/mod.rs +++ b/boa_engine/src/optimizer/mod.rs @@ -16,7 +16,7 @@ bitflags! { /// Print statistics to `stdout`. const STATISTICS = 0b0000_0001; - /// Apply contant folding optimization. + /// Apply constant folding optimization. const CONSTANT_FOLDING = 0b0000_0010; /// Apply all optimizations.