Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add an option to show cursor and placeholder together #73

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
))]
Comment on lines +123 to +127
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tui-rs, Text does not have push_span method, so I did not support them.

When I did not put above cfg, I got this error.

error[E0599]: no method named `push_span` found for struct `tui::text::Text` in the current scope
   --> src/widget.rs:[12](https://github.com/kyu08/tui-textarea/actions/runs/10096172842/job/27918026333#step:18:13)9:22
    |
129 |                 text.push_span(self.0.placeholder.as_str());
    |                      ^^^^^^^^^ method not found in `Text<'_>`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `tui-textarea` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `tui-textarea` (lib test) due to 1 previous error

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())
Expand Down
Loading