зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-07 16:05:50 +03:00
simplify placeholder implementation
Этот коммит содержится в:
@@ -6,7 +6,7 @@ use crate::scroll::Scrolling;
|
|||||||
#[cfg(feature = "search")]
|
#[cfg(feature = "search")]
|
||||||
use crate::search::Search;
|
use crate::search::Search;
|
||||||
use crate::tui::layout::Alignment;
|
use crate::tui::layout::Alignment;
|
||||||
use crate::tui::style::{Modifier, Style};
|
use crate::tui::style::{Color, Modifier, Style};
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
feature = "ratatui-crossterm",
|
feature = "ratatui-crossterm",
|
||||||
feature = "ratatui-termion",
|
feature = "ratatui-termion",
|
||||||
@@ -62,7 +62,7 @@ pub struct TextArea<'a> {
|
|||||||
search: Search,
|
search: Search,
|
||||||
alignment: Alignment,
|
alignment: Alignment,
|
||||||
pub(crate) placeholder: Option<String>,
|
pub(crate) placeholder: Option<String>,
|
||||||
pub(crate) placeholder_style: Option<Style>,
|
pub(crate) placeholder_style: Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert any iterator whose elements can be converted into [`String`] into [`TextArea`]. Each [`String`] element is
|
/// Convert any iterator whose elements can be converted into [`String`] into [`TextArea`]. Each [`String`] element is
|
||||||
@@ -161,7 +161,7 @@ impl<'a> TextArea<'a> {
|
|||||||
search: Search::default(),
|
search: Search::default(),
|
||||||
alignment: Alignment::Left,
|
alignment: Alignment::Left,
|
||||||
placeholder: None,
|
placeholder: None,
|
||||||
placeholder_style: None,
|
placeholder_style: Style::default().fg(Color::DarkGray),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1244,7 +1244,7 @@ impl<'a> TextArea<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_placeholder_style(&mut self, style: Style) {
|
pub fn set_placeholder_style(&mut self, style: Style) {
|
||||||
self.placeholder_style = Some(style);
|
self.placeholder_style = style;
|
||||||
}
|
}
|
||||||
/// Set the style of cursor. By default, a cursor is rendered in the reversed color. Setting the same style as
|
/// 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.
|
/// cursor line hides a cursor.
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::textarea::TextArea;
|
use crate::textarea::TextArea;
|
||||||
use crate::tui::buffer::Buffer;
|
use crate::tui::buffer::Buffer;
|
||||||
use crate::tui::layout::Rect;
|
use crate::tui::layout::Rect;
|
||||||
use crate::tui::style::{Color, Style};
|
|
||||||
use crate::tui::text::Text;
|
use crate::tui::text::Text;
|
||||||
use crate::tui::widgets::{Paragraph, Widget};
|
use crate::tui::widgets::{Paragraph, Widget};
|
||||||
use crate::util::num_digits;
|
use crate::util::num_digits;
|
||||||
@@ -120,29 +119,15 @@ impl<'a> Widget for Renderer<'a> {
|
|||||||
let top_row = next_scroll_top(top_row, cursor.0 as u16, height);
|
let top_row = next_scroll_top(top_row, cursor.0 as u16, height);
|
||||||
let top_col = next_scroll_top(top_col, cursor.1 as u16, width);
|
let top_col = next_scroll_top(top_col, cursor.1 as u16, width);
|
||||||
|
|
||||||
let text = self.text(top_row as usize, height as usize);
|
let (text, style) = match &self.0.placeholder {
|
||||||
|
Some(text) if self.0.is_empty() => {
|
||||||
// check for placeholder text and style
|
(Text::from(text.as_str()), self.0.placeholder_style) // When a placeholder is set and no text is in textarea
|
||||||
let mut style = self.0.style();
|
|
||||||
let text = match &self.0.placeholder {
|
|
||||||
Some(ph) => {
|
|
||||||
if self.0.is_empty() {
|
|
||||||
// placeholder is defined and the box is empty
|
|
||||||
style = self
|
|
||||||
.0
|
|
||||||
.placeholder_style
|
|
||||||
// default to dark grey if the caller didnt specify
|
|
||||||
.unwrap_or(Style::default().fg(Color::DarkGray));
|
|
||||||
Text::from(ph.as_str())
|
|
||||||
} else {
|
|
||||||
text
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => text,
|
_ => (self.text(top_row as usize, height as usize), self.0.style()),
|
||||||
};
|
};
|
||||||
|
|
||||||
// to get fine control over the text color and the surrrounding block they have to be rendered separately
|
// To get fine control over the text color and the surrrounding block they have to be rendered separately
|
||||||
// see https://github.com/tui-rs-revival/ratatui/issues/144
|
// see https://github.com/ratatui-org/ratatui/issues/144
|
||||||
let mut text_area = area;
|
let mut text_area = area;
|
||||||
let mut inner = Paragraph::new(text)
|
let mut inner = Paragraph::new(text)
|
||||||
.style(style)
|
.style(style)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user