Skip to content

Commit

Permalink
Reintroduce StyledBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Aug 25, 2024
1 parent 6422e06 commit c9db271
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Highlighter for MyHelper {
&self,
line: &'l str,
pos: usize,
) -> impl ExactSizeIterator<Item = (::rustyline::highlight::AnsiStyle, &'l str)> {
) -> impl Iterator<Item = impl 'l + rustyline::highlight::StyledBlock> {
self.highlighter.highlight_line(line, pos)
}

Expand Down
4 changes: 2 additions & 2 deletions examples/read_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ impl Highlighter for MaskingHighlighter {
&self,
line: &'l str,
_pos: usize,
) -> impl ExactSizeIterator<Item = (rustyline::highlight::AnsiStyle, &'l str)> {
) -> impl Iterator<Item = impl 'l + rustyline::highlight::StyledBlock> {
use unicode_width::UnicodeWidthStr;
if self.masking {
[(rustyline::highlight::AnsiStyle::default(), " ")]
.repeat(line.width())
.into_iter()
} else {
vec![].into_iter()
vec![(rustyline::highlight::AnsiStyle::default(), line)].into_iter()
}
}

Expand Down
2 changes: 1 addition & 1 deletion rustyline-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn highlighter_macro_derive(input: TokenStream) -> TokenStream {
&self,
line: &'l str,
pos: usize,
) -> impl ExactSizeIterator<Item = (::rustyline::highlight::AnsiStyle, &'l str)> {
) -> impl Iterator<Item = impl 'l + ::rustyline::highlight::StyledBlock> {
::rustyline::highlight::Highlighter::highlight_line(&self.#field_name_or_index, line, pos)
}

Expand Down
34 changes: 15 additions & 19 deletions src/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::fmt::Display;
/// ANSI style
#[cfg(feature = "split-highlight")]
#[cfg_attr(docsrs, doc(cfg(feature = "split-highlight")))]
pub trait Style {
pub trait Style: Default {
/// Produce a ansi sequences which sets the graphic mode
fn start(&self) -> impl Display;
/// Produce a ansi sequences which ends the graphic mode
Expand All @@ -27,7 +27,7 @@ impl Style for () {
}
}

#[cfg(feature = "ansi-str")]
/*#[cfg(feature = "ansi-str")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi-str")))]
impl Style for ansi_str::Style {
fn start(&self) -> impl Display {
Expand All @@ -37,7 +37,7 @@ impl Style for ansi_str::Style {
fn end(&self) -> impl Display {
self.end()
}
}
}*/
#[cfg(feature = "anstyle")]
#[cfg_attr(docsrs, doc(cfg(feature = "anstyle")))]
impl Style for anstyle::Style {
Expand All @@ -58,7 +58,6 @@ pub type AnsiStyle = anstyle::Style;
#[cfg(not(feature = "anstyle"))]
pub type AnsiStyle = ();

/*
/// Styled text
#[cfg(feature = "split-highlight")]
#[cfg_attr(docsrs, doc(cfg(feature = "split-highlight")))]
Expand All @@ -74,7 +73,7 @@ pub trait StyledBlock {
where
Self: Sized;
}
#[cfg(feature = "ansi-str")]
/*#[cfg(feature = "ansi-str")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi-str")))]
impl StyledBlock for ansi_str::AnsiBlock<'_> {
type Style = ansi_str::Style;
Expand All @@ -86,11 +85,11 @@ impl StyledBlock for ansi_str::AnsiBlock<'_> {
fn style(&self) -> &Self::Style {
self.style()
}
}
#[cfg(feature = "anstyle")]
#[cfg_attr(docsrs, doc(cfg(feature = "anstyle")))]
impl StyledBlock for (anstyle::Style, &str) {
type Style = anstyle::Style;
}*/
#[cfg(feature = "split-highlight")]
#[cfg_attr(docsrs, doc(cfg(feature = "split-highlight")))]
impl StyledBlock for (AnsiStyle, &str) {
type Style = AnsiStyle;

fn text(&self) -> &str {
self.1
Expand All @@ -100,7 +99,6 @@ impl StyledBlock for (anstyle::Style, &str) {
&self.0
}
}
*/

/// Syntax highlighter with [ANSI color](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters).
/// Rustyline will try to handle escape sequence for ANSI color on windows
Expand All @@ -126,8 +124,6 @@ pub trait Highlighter {

/// Takes the currently edited `line` with the cursor `pos`ition and
/// returns the styled blocks.
///
/// Returns an empty vec when there is no highlighting.
#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
#[cfg_attr(
docsrs,
Expand All @@ -137,9 +133,9 @@ pub trait Highlighter {
&self,
line: &'l str,
pos: usize,
) -> impl ExactSizeIterator<Item = (AnsiStyle, &'l str)> {
) -> impl Iterator<Item = impl 'l + StyledBlock> {
let _ = (line, pos);
std::iter::empty()
vec![(AnsiStyle::default(), line)].into_iter()
}

/// Takes the `prompt` and
Expand Down Expand Up @@ -195,7 +191,7 @@ impl<'r, H: Highlighter> Highlighter for &'r H {
&self,
line: &'l str,
pos: usize,
) -> impl ExactSizeIterator<Item = (AnsiStyle, &'l str)> {
) -> impl Iterator<Item = impl 'l + StyledBlock> {
(**self).highlight_line(line, pos)
}

Expand Down Expand Up @@ -275,9 +271,9 @@ impl Highlighter for MatchingBracketHighlighter {
&self,
line: &'l str,
_pos: usize,
) -> impl ExactSizeIterator<Item = (AnsiStyle, &'l str)> {
) -> impl Iterator<Item = impl 'l + StyledBlock> {
if line.len() <= 1 {
return vec![].into_iter();
return vec![(AnsiStyle::default(), line)].into_iter();
}
if let Some((bracket, pos)) = self.bracket.get() {
if let Some((_, idx)) = find_matching_bracket(line, pos, bracket) {
Expand All @@ -289,7 +285,7 @@ impl Highlighter for MatchingBracketHighlighter {
.into_iter();
}
}
vec![].into_iter()
vec![(AnsiStyle::default(), line)].into_iter()
}

fn highlight_char(&self, line: &str, pos: usize, forced: bool) -> bool {
Expand Down
16 changes: 6 additions & 10 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,16 +1010,12 @@ impl Renderer for PosixRenderer {
self.buffer
.push_str(&highlighter.highlight(line, line.pos()));
} else {
use crate::highlight::Style;
let it = highlighter.highlight_line(line, line.pos());
if it.len() == 0 {
self.buffer.push_str(line);
} else {
for (style, block) in it {
write!(self.buffer, "{}", style.start())?;
self.buffer.push_str(&block);
write!(self.buffer, "{}", style.end())?;
}
use crate::highlight::{Style, StyledBlock};
for sb in highlighter.highlight_line(line, line.pos()) {
let style = sb.style();
write!(self.buffer, "{}", style.start())?;
self.buffer.push_str(sb.text());
write!(self.buffer, "{}", style.end())?;
}
}
}
Expand Down

0 comments on commit c9db271

Please sign in to comment.