Skip to content

Commit

Permalink
Fix prompt on windows (#2986)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ShaneEverittM authored and Razican committed Jun 26, 2023
1 parent 8cbeb00 commit 4c8bb84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
30 changes: 24 additions & 6 deletions boa_cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
}
}
}
Expand All @@ -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,
Expand Down
11 changes: 4 additions & 7 deletions boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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
Expand Down Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4c8bb84

Please sign in to comment.