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

show the cursor even if placeholder text is rendered by default

related to #73
Этот коммит содержится в:
rhysd
2024-07-31 21:55:36 +09:00
родитель cc016f982c
Коммит 00f0010393
2 изменённых файлов: 14 добавлений и 38 удалений

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

@@ -87,14 +87,13 @@ pub struct TextArea<'a> {
cursor_line_style: Style,
line_number_style: Option<Style>,
pub(crate) viewport: Viewport,
cursor_style: Style,
pub(crate) cursor_style: Style,
yank: YankText,
#[cfg(feature = "search")]
search: Search,
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,
@@ -200,7 +199,6 @@ 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),
@@ -1854,20 +1852,6 @@ 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;

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

@@ -1,11 +1,15 @@
use crate::ratatui::buffer::Buffer;
use crate::ratatui::layout::Rect;
use crate::ratatui::text::Text;
use crate::ratatui::text::{Span, Text};
use crate::ratatui::widgets::{Paragraph, Widget};
use crate::textarea::TextArea;
use crate::util::num_digits;
#[cfg(feature = "ratatui")]
use ratatui::text::Line;
use std::cmp;
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(feature = "tuirs")]
use tui::text::Spans as Line;
// &mut 'a (u16, u16, u16, u16) is not available since Renderer instance totally takes over the ownership of TextArea
// instance. In the case, the TextArea instance cannot be accessed from any other objects since it is mutablly
@@ -94,6 +98,13 @@ impl<'a> Renderer<'a> {
}
Text::from(lines)
}
#[inline]
fn placeholder_text(&self) -> Text<'a> {
let cursor = Span::styled(" ", self.0.cursor_style);
let text = Span::raw(self.0.placeholder.as_str());
Text::from(Line::from(vec![cursor, text]))
}
}
impl<'a> Widget for Renderer<'a> {
@@ -120,26 +131,7 @@ 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)
(self.placeholder_text(), self.0.placeholder_style)
} else {
(self.text(top_row as usize, height as usize), self.0.style())
};