diff --git a/src/ansi_escape.rs b/src/ansi_escape.rs index 533ffbd..6590293 100644 --- a/src/ansi_escape.rs +++ b/src/ansi_escape.rs @@ -20,8 +20,8 @@ pub(crate) const HIDE_CURSOR: &str = "\x1b[?25l"; /// DECTCTEM: Make the cursor visible pub(crate) const SHOW_CURSOR: &str = "\x1b[?25h"; -/// Clear line right of the current position of the cursor -pub(crate) const CLEAR_LINE_RIGHT_OF_CURSOR: &str = "\x1b[K"; +/// Clear screen from cursor down +pub(crate) const CLEAR_SCREEN_FROM_CURSOR_DOWN: &str = "\x1b[J"; /// Report the cursor position to the application. pub(crate) const DEVICE_STATUS_REPORT: &str = "\x1b[6n"; diff --git a/src/editor.rs b/src/editor.rs index 441ab08..49aaf34 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -24,7 +24,7 @@ const REMOVE_LINE: u8 = ctrl_key(b'R'); const BACKSPACE: u8 = 127; const HELP_MESSAGE: &str = - "^S save | ^Q quit | ^F find | ^G go to | ^D duplicate | ^E execute | ^C copy | ^X cut | ^V paste"; + "^S save | ^Q quit | ^F find | ^G go to | ^D duplicate | ^R remove | ^E execute | ^C copy | ^X cut | ^V paste"; /// `set_status!` sets a formatted status message for the editor. /// Example usage: `set_status!(editor, "{} written to {}", file_size, file_name)` @@ -173,7 +173,6 @@ impl Editor { print!("{USE_ALTERNATE_SCREEN}"); editor.update_window_size()?; - set_status!(editor, "{}", HELP_MESSAGE); Ok(editor) } @@ -290,7 +289,8 @@ impl Editor { fn update_window_size(&mut self) -> Result<(), Error> { let wsize = sys::get_window_size().or_else(|_| terminal::get_window_size_using_cursor())?; // Make room for the status bar and status message - (self.screen_rows, self.window_width) = (wsize.0.saturating_sub(2), wsize.1); + (self.screen_rows, self.window_width) = + (wsize.0.saturating_sub(1 + (self.status_msg().len() + wsize.1 - 1) / wsize.1), wsize.1); self.update_screen_cols(); Ok(()) } @@ -533,7 +533,7 @@ impl Editor { fn draw_rows(&self, buffer: &mut String) -> Result<(), Error> { let row_it = self.rows.iter().map(Some).chain(repeat(None)).enumerate(); for (i, row) in row_it.skip(self.cursor.roff).take(self.screen_rows) { - buffer.push_str(CLEAR_LINE_RIGHT_OF_CURSOR); + buffer.push_str(CLEAR_SCREEN_FROM_CURSOR_DOWN); if let Some(row) = row { // Draw a row of text self.draw_left_padding(buffer, i + 1)?; @@ -570,13 +570,16 @@ impl Editor { Ok(()) } + /// Returns the message for the message bar, or the help message if none is applicable. + fn status_msg(&self) -> &str { + let msg = self.status_msg.as_ref().filter(|sm| sm.time.elapsed() < self.config.message_dur); + msg.map_or(HELP_MESSAGE, |sm| sm.msg.as_str()) + } + /// Draw the message bar on the terminal, by adding characters to the buffer. fn draw_message_bar(&self, buffer: &mut String) { - buffer.push_str(CLEAR_LINE_RIGHT_OF_CURSOR); - let msg_duration = self.config.message_dur; - if let Some(sm) = self.status_msg.as_ref().filter(|sm| sm.time.elapsed() < msg_duration) { - buffer.push_str(&sm.msg[..sm.msg.len().min(self.window_width)]); - } + buffer.push_str(CLEAR_SCREEN_FROM_CURSOR_DOWN); + buffer.push_str(self.status_msg()); } /// Refresh the screen: update the offsets, draw the rows, the status bar, the message bar, and