Skip to content

Commit

Permalink
Fix missing newlines at end of prompts
Browse files Browse the repository at this point in the history
Fixes #233
  • Loading branch information
mikaelmello committed Mar 17, 2024
1 parent 8eb6046 commit c1c6671
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions inquire/src/prompts/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
if last_handle.needs_redraw() {
backend.frame_setup()?;
self.render(backend)?;
backend.frame_finish()?;
backend.frame_finish(false)?;
last_handle = ActionResult::Clean;
}

Expand All @@ -144,7 +144,7 @@ where
if pre_cancel_result {
backend.frame_setup()?;
backend.render_canceled_prompt(self.message())?;
backend.frame_finish()?;
backend.frame_finish(true)?;
return Err(InquireError::OperationCanceled);
}

Expand All @@ -160,7 +160,7 @@ where

backend.frame_setup()?;
backend.render_prompt_with_answer(self.message(), &formatted)?;
backend.frame_finish()?;
backend.frame_finish(true)?;

Ok(final_answer)
}
Expand Down
13 changes: 9 additions & 4 deletions inquire/src/ui/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::{frame_renderer::FrameRenderer, InputReader};

pub trait CommonBackend: InputReader {
fn frame_setup(&mut self) -> Result<()>;
fn frame_finish(&mut self) -> Result<()>;
fn frame_finish(&mut self, is_last_frame: bool) -> Result<()>;

fn render_canceled_prompt(&mut self, prompt: &str) -> Result<()>;
fn render_prompt_with_answer(&mut self, prompt: &str, answer: &str) -> Result<()>;
Expand Down Expand Up @@ -247,8 +247,8 @@ where
self.frame_renderer.start_frame()
}

fn frame_finish(&mut self) -> Result<()> {
self.frame_renderer.finish_current_frame()
fn frame_finish(&mut self, is_last_frame: bool) -> Result<()> {
self.frame_renderer.finish_current_frame(is_last_frame)
}

fn render_canceled_prompt(&mut self, prompt: &str) -> Result<()> {
Expand Down Expand Up @@ -689,6 +689,7 @@ pub(crate) mod test {
min_date: Option<NaiveDate>,
max_date: Option<NaiveDate>,
},
PromptEnd,
}

#[derive(Default, Debug, Clone)]
Expand Down Expand Up @@ -751,7 +752,11 @@ pub(crate) mod test {
Ok(())
}

fn frame_finish(&mut self) -> std::io::Result<()> {
fn frame_finish(&mut self, is_last_frame: bool) -> std::io::Result<()> {
if is_last_frame {
self.push_token(Token::PromptEnd);
}

if let Some(frame) = self.cur_frame.take() {
self.frames.push(frame);
} else {
Expand Down
9 changes: 7 additions & 2 deletions inquire/src/ui/frame_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ where
Ok(())
}

pub fn finish_current_frame(&mut self) -> io::Result<()> {
pub fn finish_current_frame(&mut self, add_empty_line: bool) -> io::Result<()> {
let (last_rendered_frame, mut current_frame) = match std::mem::take(&mut self.state) {
RenderState::Rendered(_) | RenderState::Initial => {
return Ok(());
Expand All @@ -280,7 +280,7 @@ where
current_frame.frame_size.height(),
);

self.terminal.cursor_hide()?;
//self.terminal.cursor_hide()?;
self.move_cursor_to(Position { row: 0, col: 0 })?;

for i in 0..rows_to_iterate {
Expand Down Expand Up @@ -321,6 +321,11 @@ where
}
}

if add_empty_line {
self.terminal.write("\n")?;
self.cursor_position.row += 1;
}

if let Some(expected_cursor_position) = current_frame.expected_cursor_position {
self.move_cursor_to(expected_cursor_position)?;
}
Expand Down

0 comments on commit c1c6671

Please sign in to comment.