From eeeaaeebccca04bfd256ca1fc61f979e39fcb758 Mon Sep 17 00:00:00 2001 From: Shane Murphy Date: Mon, 5 Jun 2023 03:23:40 -0700 Subject: [PATCH] Fix prompt on windows (#2986) * Fix typo * Refactor prompt highlighting into RLHelper By moving the prompt coloring to be done in Highlighter::highlight_prompt, we sidestep the issue on Windows where the prompt width is calculated post-coloring AND without ignoring escape codes. By including it in the implementation of Highlighter, Editor::readline now operates on a plain-text prompt, so width calculation is correct. This commit also re-arranges the trait impl order to match the definition. --- boa_cli/src/helper.rs | 30 ++++++++++++++++++++++++------ boa_cli/src/main.rs | 11 ++++------- boa_engine/src/optimizer/mod.rs | 2 +- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/boa_cli/src/helper.rs b/boa_cli/src/helper.rs index 5cdb2e1578..dfd1301dc6 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 723d8c44f0..fc0e45db1d 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 9c8a6915c5..e675a72a71 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.