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

rename set_placeholder to set_placeholder_text and add more APIs

Этот коммит содержится в:
rhysd
2023-10-01 23:10:23 +09:00
родитель 2031e5b20c
Коммит 92c107c939
4 изменённых файлов: 63 добавлений и 5 удалений

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

@@ -38,7 +38,7 @@ fn main() -> io::Result<()> {
// set placeholder
textarea.set_placeholder_style(Style::default());
textarea.set_placeholder("prompt message");
textarea.set_placeholder_text("prompt message");
loop {
term.draw(|f| {
f.render_widget(textarea.widget(), area);

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

@@ -36,7 +36,7 @@ fn main() -> io::Result<()> {
};
textarea.set_style(Style::default().fg(Color::Yellow));
textarea.set_placeholder_style(Style::default());
textarea.set_placeholder("prompt message");
textarea.set_placeholder_text("prompt message");
loop {
term.draw(|f| {
f.render_widget(textarea.widget(), area);

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

@@ -37,7 +37,7 @@ fn main() -> io::Result<()> {
let mut textarea = TextArea::default();
textarea.set_cursor_line_style(Style::default());
textarea.set_placeholder("enter a valid float, eg 1.56".to_string());
textarea.set_placeholder_text("Enter a valid float (e.g. 1.56)");
let layout =
Layout::default().constraints([Constraint::Length(3), Constraint::Min(1)].as_slice());
let mut is_valid = validate(&mut textarea);

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

@@ -1238,14 +1238,72 @@ impl<'a> TextArea<'a> {
self.line_number_style
}
/// sets the placeholder text
pub fn set_placeholder(&mut self, placeholder: impl Into<String>) {
/// Set the placeholder text. The text is set in the textarea when no text is input. Setting a non-empty string `""`
/// enables the placeholder. The default value is an empty string so the placeholder is disabled by default.
/// To customize the text style, see [`TextArea::set_placeholder_style`].
/// ```
/// use tui_textarea::TextArea;
///
/// let mut textarea = TextArea::default();
/// assert_eq!(textarea.placeholder_text(), "");
/// assert!(textarea.placeholder_style().is_none());
///
/// textarea.set_placeholder_text("Hello");
/// assert_eq!(textarea.placeholder_text(), "Hello");
/// assert!(textarea.placeholder_style().is_some());
/// ```
pub fn set_placeholder_text(&mut self, placeholder: impl Into<String>) {
self.placeholder = placeholder.into();
}
/// Set the style of the placeholder text. The default style is a dark gray text.
/// ```
/// use tui::style::{Style, Color};
/// use tui_textarea::TextArea;
///
/// let mut textarea = TextArea::default();
/// assert_eq!(textarea.placeholder_style(), None); // When the placeholder is disabled
///
/// textarea.set_placeholder_text("Enter your message"); // Enable placeholder by setting non-empty text
///
/// let style = Style::default().bg(Color::Blue);
/// textarea.set_placeholder_style(style);
/// assert_eq!(textarea.placeholder_style(), Some(style));
/// ```
pub fn set_placeholder_style(&mut self, style: Style) {
self.placeholder_style = style;
}
/// Get the placeholder text. An empty string means the placeholder is disabled. The default value is an empty string.
/// ```
/// use tui_textarea::TextArea;
///
/// let textarea = TextArea::default();
/// assert_eq!(textarea.placeholder_text(), "");
/// ```
pub fn placeholder_text(&self) -> &'_ str {
self.placeholder.as_str()
}
/// Get the placeholder style. When the placeholder text is empty, it returns `None` since the placeholder is disabled.
/// The default style is a dark gray text.
/// ```
/// use tui_textarea::TextArea;
///
/// let mut textarea = TextArea::default();
/// assert_eq!(textarea.placeholder_style(), None);
///
/// textarea.set_placeholder_text("hello");
/// assert!(textarea.placeholder_style().is_some());
/// ```
pub fn placeholder_style(&self) -> Option<Style> {
if self.placeholder.is_empty() {
None
} else {
Some(self.placeholder_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.
/// ```