Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Expose cursor position of InputField #64

Merged
merged 3 commits into from
Oct 10, 2024
Merged
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
26 changes: 21 additions & 5 deletions src/views/input_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,13 @@ impl InputField {
/// All rendering must be explicitely called, no rendering is
/// done on functions changing the state.
///
/// This function also returns the cursor position in the form
/// `Ok(Some((left, top)))` if the terminal cursor is rendered.
/// Otherwise, this function returns `Ok(None)`.
///
/// w is typically either stderr or stdout. This function doesn't
/// flush by itself (useful to avoid flickering)
pub fn display_on<W: Write>(&self, w: &mut W) -> Result<(), Error> {
pub fn display_on<W: Write>(&self, w: &mut W) -> Result<Option<(u16, u16)>, Error> {
let normal_style = if self.focused {
&self.focused_style
} else {
Expand All @@ -554,6 +558,7 @@ impl InputField {

let mut width = self.area.width as usize;
let pos = self.content.cursor_pos();
let mut terminal_cursor_pos = None;
let scrollbar = self
.area
.scrollbar(self.scroll.y as u16, self.content.line_count() as u16);
Expand Down Expand Up @@ -609,6 +614,11 @@ impl InputField {
if !is_last || displayed_width + char_width > width {
if self.focused && selection.contains(i, y) {
self.cursor_style.queue(w, fit::ELLIPSIS)?;
// set terminal cursor position
terminal_cursor_pos = Some((
self.area.left + displayed_width as u16,
self.area.top + j,
));
} else {
normal_style.queue(w, fit::ELLIPSIS)?;
}
Expand All @@ -618,6 +628,9 @@ impl InputField {
}
if self.focused && selection.contains(i, y) {
self.cursor_style.queue(w, c)?;
// set terminal cursor position
terminal_cursor_pos =
Some((self.area.left + displayed_width as u16, self.area.top + j));
} else {
normal_style.queue(w, c)?;
}
Expand All @@ -628,6 +641,9 @@ impl InputField {
}
if displayed_width < width && cursor_at_end {
self.cursor_style.queue(w, ' ')?;
// set terminal cursor position
terminal_cursor_pos =
Some((self.area.left + displayed_width as u16, self.area.top + j));
displayed_width += 1;
}
while displayed_width < width {
Expand All @@ -646,14 +662,14 @@ impl InputField {
}
}
}
Ok(())
Ok(terminal_cursor_pos)
}

/// render the input field on stdout
pub fn display(&self) -> Result<(), Error> {
pub fn display(&self) -> Result<Option<(u16, u16)>, Error> {
let mut w = std::io::stdout();
self.display_on(&mut w)?;
let pos = self.display_on(&mut w)?;
w.flush()?;
Ok(())
Ok(pos)
}
}