1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-09-07 16:05:50 +03:00
Этот коммит содержится в:
rhysd
2022-06-16 18:30:25 +09:00
родитель b7facac135
Коммит 2f38bc46a5
2 изменённых файлов: 36 добавлений и 5 удалений

Просмотреть файл

@@ -200,6 +200,5 @@ impl<'a> Drop for Editor<'a> {
} }
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let mut editor = Editor::new(env::args_os().skip(1))?; Editor::new(env::args_os().skip(1))?.run()
editor.run()
} }

Просмотреть файл

@@ -44,6 +44,7 @@ pub struct TextArea<'a> {
cursor_line_style: Style, cursor_line_style: Style,
line_number_style: Option<Style>, line_number_style: Option<Style>,
scroll_top: (AtomicU16, AtomicU16), scroll_top: (AtomicU16, AtomicU16),
cursor_style: Style,
yank: String, yank: String,
} }
@@ -136,6 +137,7 @@ impl<'a> TextArea<'a> {
cursor_line_style: Style::default().add_modifier(Modifier::UNDERLINED), cursor_line_style: Style::default().add_modifier(Modifier::UNDERLINED),
line_number_style: None, line_number_style: None,
scroll_top: (AtomicU16::new(0), AtomicU16::new(0)), scroll_top: (AtomicU16::new(0), AtomicU16::new(0)),
cursor_style: Style::default().add_modifier(Modifier::REVERSED),
yank: String::new(), yank: String::new(),
} }
} }
@@ -935,19 +937,18 @@ impl<'a> TextArea<'a> {
} }
if i == self.cursor.0 { if i == self.cursor.0 {
let cursor_style = Style::default().add_modifier(Modifier::REVERSED);
if let Some((i, c)) = l.char_indices().nth(self.cursor.1) { if let Some((i, c)) = l.char_indices().nth(self.cursor.1) {
let j = i + c.len_utf8(); let j = i + c.len_utf8();
spans.extend_from_slice(&[ spans.extend_from_slice(&[
Span::styled(&l[..i], self.cursor_line_style), Span::styled(&l[..i], self.cursor_line_style),
Span::styled(&l[i..j], cursor_style), Span::styled(&l[i..j], self.cursor_style),
Span::styled(&l[j..], self.cursor_line_style), Span::styled(&l[j..], self.cursor_line_style),
]); ]);
} else { } else {
// When cursor is at the end of line // When cursor is at the end of line
spans.extend_from_slice(&[ spans.extend_from_slice(&[
Span::styled(l.as_str(), self.cursor_line_style), Span::styled(l.as_str(), self.cursor_line_style),
Span::styled(" ", cursor_style), Span::styled(" ", self.cursor_style),
]); ]);
} }
} else { } else {
@@ -1098,6 +1099,16 @@ impl<'a> TextArea<'a> {
/// Remove the style of line number which was set by [`TextArea::set_line_number_style`]. After calling this /// Remove the style of line number which was set by [`TextArea::set_line_number_style`]. After calling this
/// method, Line numbers will no longer be shown. /// method, Line numbers will no longer be shown.
/// ```
/// use tui::style::{Style, Color};
/// use tui_textarea::TextArea;
///
/// let mut textarea = TextArea::default();
///
/// textarea.set_line_number_style(Style::default().bg(Color::DarkGray));
/// textarea.remove_line_number();
/// assert_eq!(textarea.line_number_style(), None);
/// ```
pub fn remove_line_number(&mut self) { pub fn remove_line_number(&mut self) {
self.line_number_style = None; self.line_number_style = None;
} }
@@ -1107,6 +1118,27 @@ impl<'a> TextArea<'a> {
self.line_number_style self.line_number_style
} }
/// Set the style of cursor. By default, a cursor is rendered in the reversed color. Setting the same style as
/// cursor line hides a cursor.
/// ```
/// use tui::style::{Style, Color};
/// use tui_textarea::TextArea;
///
/// let mut textarea = TextArea::default();
///
/// let style = Style::default().bg(Color::Red);
/// textarea.set_cursor_style(style);
/// assert_eq!(textarea.cursor_style(), style);
/// ```
pub fn set_cursor_style(&mut self, style: Style) {
self.cursor_style = style;
}
/// Get the style of cursor.
pub fn cursor_style(&self) -> Style {
self.cursor_style
}
/// Get slice of line texts. This method borrows the content, but not moves. Note that the returned slice will /// Get slice of line texts. This method borrows the content, but not moves. Note that the returned slice will
/// never be empty because an empty text means a slice containing one empty line. This is correct since any text /// never be empty because an empty text means a slice containing one empty line. This is correct since any text
/// file must end with a newline. /// file must end with a newline.