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

Render the help message as the default no-content status message. #222

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions src/ansi_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
21 changes: 12 additions & 9 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down Expand Up @@ -173,7 +173,6 @@ impl Editor {
print!("{USE_ALTERNATE_SCREEN}");

editor.update_window_size()?;
set_status!(editor, "{}", HELP_MESSAGE);

Ok(editor)
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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
Expand Down
Loading