Browse Source

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.
pull/2989/head
Shane Murphy 1 year ago committed by GitHub
parent
commit
eeeaaeebcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      boa_cli/src/helper.rs
  2. 11
      boa_cli/src/main.rs
  3. 2
      boa_engine/src/optimizer/mod.rs

30
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,

11
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<Option<T>>
// 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,

2
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.

Loading…
Cancel
Save