From bac44ae105f9c30baa148599bc6188faf2b71b4d Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 10 Jun 2022 00:07:35 +0900 Subject: [PATCH] add cursor line highlight --- src/textarea.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/textarea.rs b/src/textarea.rs index 5fa0b06..4dbce34 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -13,6 +13,7 @@ pub struct TextArea<'a> { cursor: (usize, usize), // 0-base tab: &'a str, history: EditHistory, + cursor_line_style: Style, } impl<'a> Default for TextArea<'a> { @@ -24,6 +25,7 @@ impl<'a> Default for TextArea<'a> { cursor: (0, 0), tab: " ", history: EditHistory::new(50), + cursor_line_style: Style::default().add_modifier(Modifier::UNDERLINED), } } } @@ -194,9 +196,9 @@ impl<'a> TextArea<'a> { .unwrap_or((l.len() - 1, ' ')); let j = i + c.len_utf8(); lines.push(Spans::from(vec![ - Span::from(&l[..i]), + Span::styled(&l[..i], self.cursor_line_style), Span::styled(&l[i..j], Style::default().add_modifier(Modifier::REVERSED)), - Span::from(&l[j..]), + Span::styled(&l[j..], self.cursor_line_style), ])); } else { lines.push(Spans::from(l.as_str())); @@ -234,6 +236,10 @@ impl<'a> TextArea<'a> { self.history = EditHistory::new(max); } + pub fn set_cursor_line_style(&mut self, style: Style) { + self.cursor_line_style = style; + } + pub fn lines(&'a self) -> impl Iterator { self.lines.iter().map(|l| &l[..l.len() - 1]) // Trim last whitespace }