Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor highlighting #795

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ jobs:
run: cargo check --workspace --no-default-features
env:
RUSTFLAGS: "-D warnings"
- name: Check split-highlight feature
run: |
cargo check --workspace --all-targets --features 'split-highlight'
cargo check --workspace --all-targets --features 'split-highlight ansi-str'
cargo check --workspace --all-targets --features 'split-highlight anstyle'
cargo check --workspace --all-targets --features 'split-highlight ansi-str derive'
cargo check --workspace --all-targets --features 'split-highlight anstyle derive'

direct-minimal-versions:
name: Test min versions
Expand Down
21 changes: 16 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ keywords = ["readline"]
license = "MIT"
categories = ["command-line-interface"]

exclude = [
"/.github/*",
"/rustfmt.toml",
]
exclude = ["/.github/*", "/rustfmt.toml"]

[badges]
maintenance = { status = "actively-developed" }
Expand All @@ -23,6 +20,10 @@ maintenance = { status = "actively-developed" }
members = ["rustyline-derive"]

[dependencies]
# For convenience / compatibilty, you can highlight the whole input buffer, ansi-str helps to split
ansi-str = { version = "0.8.0", optional = true }
# ansi_str::Style is immutable so we use anstyle::Style instead
anstyle = { version = "1.0.8", optional = true }
bitflags = "2.6"
cfg-if = "1.0"
# For file completion
Expand Down Expand Up @@ -69,6 +70,8 @@ with-file-history = ["fd-lock"]
with-sqlite-history = ["rusqlite"]
with-fuzzy = ["skim"]
case_insensitive_history_search = ["regex"]
# For continuation prompt, indentation, scrolling
split-highlight = []

[[example]]
name = "custom_key_bindings"
Expand Down Expand Up @@ -96,7 +99,15 @@ name = "sqlite_history"
required-features = ["with-sqlite-history"]

[package.metadata.docs.rs]
features = ["custom-bindings", "derive", "with-dirs", "with-file-history", "with-fuzzy"]
features = [
"custom-bindings",
"derive",
"with-dirs",
"with-file-history",
"with-fuzzy",
"split-highlight",
"anstyle",
]
all-features = false
no-default-features = true
default-target = "x86_64-unknown-linux-gnu"
Expand Down
10 changes: 10 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ impl Highlighter for MyHelper {
Owned("\x1b[1m".to_owned() + hint + "\x1b[m")
}

#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))]
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
self.highlighter.highlight(line, pos)
}

#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
fn highlight_line<'l>(
&self,
line: &'l str,
pos: usize,
) -> impl Iterator<Item = impl 'l + rustyline::highlight::StyledBlock> {
self.highlighter.highlight_line(line, pos)
}

fn highlight_char(&self, line: &str, pos: usize, kind: CmdKind) -> bool {
self.highlighter.highlight_char(line, pos, kind)
}
Expand Down
27 changes: 22 additions & 5 deletions examples/read_password.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::borrow::Cow::{self, Borrowed, Owned};

use rustyline::config::Configurer;
use rustyline::highlight::{CmdKind, Highlighter};
use rustyline::{ColorMode, Editor, Result};
Expand All @@ -11,12 +9,31 @@ struct MaskingHighlighter {
}

impl Highlighter for MaskingHighlighter {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))]
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> std::borrow::Cow<'l, str> {
use unicode_width::UnicodeWidthStr;
if self.masking {
std::borrow::Cow::Owned(" ".repeat(line.width()))
} else {
std::borrow::Cow::Borrowed(line)
}
}

#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
fn highlight_line<'l>(
&self,
line: &'l str,
_pos: usize,
) -> impl Iterator<Item = impl 'l + rustyline::highlight::StyledBlock> {
use unicode_width::UnicodeWidthStr;
if self.masking {
Owned(" ".repeat(line.width()))
vec![(
rustyline::highlight::AnsiStyle::default(),
" ".repeat(line.width()),
)]
.into_iter()
} else {
Borrowed(line)
vec![(rustyline::highlight::AnsiStyle::default(), line.to_owned())].into_iter()
}
}

Expand Down
10 changes: 10 additions & 0 deletions rustyline-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,20 @@ pub fn highlighter_macro_derive(input: TokenStream) -> TokenStream {
quote! {
#[automatically_derived]
impl #impl_generics ::rustyline::highlight::Highlighter for #name #ty_generics #where_clause {
#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))]
fn highlight<'l>(&self, line: &'l str, pos: usize) -> ::std::borrow::Cow<'l, str> {
::rustyline::highlight::Highlighter::highlight(&self.#field_name_or_index, line, pos)
}

#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
fn highlight_line<'l>(
&self,
line: &'l str,
pos: usize,
) -> impl Iterator<Item = impl 'l + ::rustyline::highlight::StyledBlock> {
::rustyline::highlight::Highlighter::highlight_line(&self.#field_name_or_index, line, pos)
}

fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
&'s self,
prompt: &'p str,
Expand Down
8 changes: 4 additions & 4 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use unicode_width::UnicodeWidthChar;

use super::{Context, Helper, Result};
use crate::error::ReadlineError;
use crate::highlight::{CmdKind, Highlighter};
use crate::highlight::CmdKind;
use crate::hint::Hint;
use crate::history::SearchDirection;
use crate::keymap::{Anchor, At, CharSearch, Cmd, Movement, RepeatCount, Word};
Expand Down Expand Up @@ -68,9 +68,9 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
}
}

pub fn highlighter(&self) -> Option<&dyn Highlighter> {
pub fn highlighter(&self) -> Option<&H> {
if self.out.colors_enabled() {
self.helper.map(|h| h as &dyn Highlighter)
self.helper
} else {
None
}
Expand Down Expand Up @@ -167,7 +167,7 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
Info::Msg(msg) => msg,
};
let highlighter = if self.out.colors_enabled() {
self.helper.map(|h| h as &dyn Highlighter)
self.helper
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl From<std::string::FromUtf8Error> for ReadlineError {
}
}

#[cfg(unix)]
#[cfg(any(unix, all(feature = "split-highlight", not(feature = "ansi-str"))))]
impl From<fmt::Error> for ReadlineError {
fn from(err: fmt::Error) -> Self {
Self::Io(io::Error::new(io::ErrorKind::Other, err))
Expand Down
Loading