Skip to content

Commit

Permalink
show the cursor even if placeholder text is rendered by default
Browse files Browse the repository at this point in the history
related to #73
  • Loading branch information
rhysd committed Jul 31, 2024
1 parent cc016f9 commit 00f0010
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
18 changes: 1 addition & 17 deletions src/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 13 additions & 21 deletions src/widget.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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> {
Expand All @@ -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())
};
Expand Down

0 comments on commit 00f0010

Please sign in to comment.