1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-09-03 14:26:17 +03:00

Merge pull request #73 from kyu08/placeholder-cursor

add an option to show cursor and placeholder together
Этот коммит содержится в:
Linda_pp
2024-07-31 21:54:41 +09:00
коммит произвёл GitHub
родитель 533443c3f2 a1ed9c68b3
Коммит cc016f982c
2 изменённых файлов: 34 добавлений и 0 удалений

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

@@ -94,6 +94,7 @@ pub struct TextArea<'a> {
alignment: Alignment,
pub(crate) placeholder: String,
pub(crate) placeholder_style: Style,
pub(crate) show_placeholder_with_cursor: bool, // not supported for tui-rs
mask: Option<char>,
selection_start: Option<(usize, usize)>,
select_style: Style,
@@ -199,6 +200,7 @@ impl<'a> TextArea<'a> {
alignment: Alignment::Left,
placeholder: String::new(),
placeholder_style: Style::default().fg(Color::DarkGray),
show_placeholder_with_cursor: false,
mask: None,
selection_start: None,
select_style: Style::default().bg(Color::LightBlue),
@@ -1852,6 +1854,20 @@ impl<'a> TextArea<'a> {
self.placeholder_style = style;
}
/// Set if cursor and placeholder are shown together. The default value is `false`.
/// ```
/// use tui_textarea::TextArea;
///
/// let mut textarea = TextArea::default();
/// assert_eq!(textarea.show_placeholder_with_cursor, false);
///
/// textarea.set_show_placeholder_with_cursor(true);
/// assert_eq!(textarea.show_placeholder_with_cursor, true);
/// ```
pub fn set_show_placeholder_with_cursor(&mut self, enabled: bool) {
self.show_placeholder_with_cursor = enabled;
}
/// Get the placeholder text. An empty string means the placeholder is disabled. The default value is an empty string.
/// ```
/// use tui_textarea::TextArea;

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

@@ -120,7 +120,25 @@ impl<'a> Widget for Renderer<'a> {
let top_col = next_scroll_top(top_col, cursor.1 as u16, width);
let (text, style) = if !self.0.placeholder.is_empty() && self.0.is_empty() {
#[cfg(any(
feature = "tuirs-crossterm",
feature = "tuirs-termion",
feature = "tuirs-no-backend",
))]
let text = Text::from(self.0.placeholder.as_str());
#[cfg(not(any(
feature = "tuirs-crossterm",
feature = "tuirs-termion",
feature = "tuirs-no-backend",
)))]
let text = if self.0.show_placeholder_with_cursor {
let mut text = self.text(top_row as usize, height as usize);
text.push_span(self.0.placeholder.as_str());
text
} else {
Text::from(self.0.placeholder.as_str())
};
(text, self.0.placeholder_style)
} else {
(self.text(top_row as usize, height as usize), self.0.style())