Skip to content

Commit

Permalink
present statusline by left/right elements
Browse files Browse the repository at this point in the history
The previous logic to display the statusline did so by right aligning
only the last element. This isn't sufficient if more than one element is
to appear on the right (e.g., a cursor position indicator). So, this
commit replaces the logic to first render the right elements as
compactly as possible, then render the first left elements as compactly
as possible, but expand out the last left element to fill the remaining
space.

Behavior is identical; this is purely an API change.
  • Loading branch information
oldaccountdeadname committed Jan 23, 2022
1 parent add8bbc commit ff0dc68
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 132 deletions.
15 changes: 10 additions & 5 deletions src/presenters/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ pub fn display(workspace: &mut Workspace, view: &mut View, error: &Error) {
let _ = presenter.print_buffer(buffer, &data, None, None);
}

presenter.print_status_line(&[StatusLineData {
content: error.description().to_string(),
style: Style::Bold,
colors: Colors::Warning,
}]);
presenter.print_status_line(
&[
StatusLineData {
content: error.description().to_string(),
style: Style::Bold,
colors: Colors::Warning,
}
],
&[],
);

presenter.present();
}
17 changes: 10 additions & 7 deletions src/presenters/modes/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ pub fn display(workspace: &mut Workspace, view: &mut View) -> Result<()> {

// Draw the status line as a search prompt.
let confirmation = "Are you sure? (y/n)".to_string();
presenter.print_status_line(&[
StatusLineData {
content: confirmation,
style: Style::Bold,
colors: Colors::Warning,
}
]);
presenter.print_status_line(
&[
StatusLineData {
content: confirmation,
style: Style::Bold,
colors: Colors::Warning,
}
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ pub fn display(workspace: &mut Workspace, view: &mut View) -> Result<()> {
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, None, None)?;

presenter.print_status_line(&[
StatusLineData {
content: " INSERT ".to_string(),
style: Style::Default,
colors: Colors::Insert,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " INSERT ".to_string(),
style: Style::Default,
colors: Colors::Insert,
},
buffer_status
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ pub fn display(workspace: &mut Workspace, mode: &mut JumpMode, view: &mut View)
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, None, Some(mode))?;

presenter.print_status_line(&[
StatusLineData {
content: " JUMP ".to_string(),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " JUMP ".to_string(),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
],
&[],
);

// Don't display a cursor.
presenter.set_cursor(None);
Expand Down
17 changes: 10 additions & 7 deletions src/presenters/modes/line_jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ pub fn display(workspace: &mut Workspace, mode: &LineJumpMode, view: &mut View)
// Draw the status line as an input prompt.
let input_prompt = format!("Go to line: {}", mode.input);
let input_prompt_len = input_prompt.len();
presenter.print_status_line(&[
StatusLineData {
content: input_prompt,
style: Style::Default,
colors: Colors::Default,
}
]);
presenter.print_status_line(
&[
StatusLineData {
content: input_prompt,
style: Style::Default,
colors: Colors::Default,
}
],
&[],
);

// Move the cursor to the end of the search query input.
let cursor_line = presenter.height() - 1;
Expand Down
22 changes: 13 additions & 9 deletions src/presenters/modes/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ pub fn display(workspace: &mut Workspace, view: &mut View, repo: &Option<Reposit
};

// Build the status line mode and buffer title display.
presenter.print_status_line(&[
StatusLineData {
content: " NORMAL ".to_string(),
style: Style::Default,
colors,
},
buffer_status,
git_status_line_data(&repo, &buf.path)
]);
presenter.print_status_line(
&[
StatusLineData {
content: " NORMAL ".to_string(),
style: Style::Default,
colors,
},
buffer_status,
],
&[
git_status_line_data(&repo, &buf.path),
],
);

presenter.present();
} else {
Expand Down
27 changes: 15 additions & 12 deletions src/presenters/modes/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ pub fn display(workspace: &mut Workspace, mode: &PathMode, view: &mut View) -> R
mode_display.graphemes(true).count() +
search_input.graphemes(true).count();

presenter.print_status_line(&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::PathMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
]);
presenter.print_status_line(
&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::PathMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
],
&[],
);

// Move the cursor to the end of the search query input.
{
Expand Down
38 changes: 21 additions & 17 deletions src/presenters/modes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,27 @@ pub fn display(workspace: &mut Workspace, mode: &SearchMode, view: &mut View) ->
mode_display.graphemes(true).count() +
search_input.graphemes(true).count();

presenter.print_status_line(&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::SearchMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
StatusLineData {
content: result_display,
style: Style::Default,
colors: Colors::Focused,
},
]);
presenter.print_status_line(
&[
StatusLineData {
content: mode_display,
style: Style::Default,
colors: Colors::SearchMode,
},
StatusLineData {
content: search_input,
style: Style::Default,
colors: Colors::Focused,
},
],
&[
StatusLineData {
content: result_display,
style: Style::Default,
colors: Colors::Focused,
},
],
);

// Move the cursor to the end of the search query input.
if mode.insert {
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/search_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ pub fn display<T: Display>(workspace: &mut Workspace, mode: &mut dyn SearchSelec
data = buf.data();
presenter.print_buffer(buf, &data, None, None)?;

presenter.print_status_line(&[
StatusLineData {
content: format!(" {} ", mode),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: format!(" {} ", mode),
style: Style::Default,
colors: Colors::Inverted,
},
buffer_status
],
&[],
);
}

if let Some(message) = mode.message() {
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ pub fn display(workspace: &mut Workspace, mode: &SelectMode, view: &mut View) ->
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, Some(&[selected_range]), None)?;

presenter.print_status_line(&[
StatusLineData {
content: " SELECT ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " SELECT ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
19 changes: 11 additions & 8 deletions src/presenters/modes/select_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ pub fn display(workspace: &mut Workspace, mode: &SelectLineMode, view: &mut View
// Draw the visible set of tokens to the terminal.
presenter.print_buffer(buf, &data, Some(&[selected_range]), None)?;

presenter.print_status_line(&[
StatusLineData {
content: " SELECT LINE ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
]);
presenter.print_status_line(
&[
StatusLineData {
content: " SELECT LINE ".to_string(),
style: Style::Default,
colors: Colors::SelectMode,
},
buffer_status
],
&[],
);

// Render the changes to the screen.
presenter.present();
Expand Down
69 changes: 34 additions & 35 deletions src/view/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,46 +87,45 @@ impl<'p> Presenter<'p> {
Ok(())
}

pub fn print_status_line(&mut self, entries: &[StatusLineData]) {
pub fn print_status_line(
&mut self, left: &[StatusLineData], right: &[StatusLineData]
) {
let line = self.view.terminal.height() - 1;

entries.iter().enumerate().fold(0, |offset, (index, element)| {
let content = match entries.len() {
1 => {
// There's only one element; have it fill the line.
element.content.pad_to_width(self.view.terminal.width())
},
2 => {
if index == entries.len() - 1 {
// Expand the last element to fill the remaining width.
element.content.pad_to_width(self.view.terminal.width().saturating_sub(offset))
} else {
element.content.clone()
}
},
_ => {
if index == entries.len() - 2 {
// Before-last element extends to fill unused space.
let space = offset + entries[index+1].content.len();
element.content.pad_to_width(self.view.terminal.width().saturating_sub(space))
} else {
element.content.clone()
}
}
};

// Update the tracked offset.
let updated_offset = offset + content.len();

let mut left_end = self.view.terminal.width();
for entry in right.iter().rev() {
left_end = left_end.saturating_sub(entry.content.len());
self.print(
&Position{ line, offset },
element.style,
element.colors,
content
&Position { line, offset: left_end },
entry.style,
entry.colors,
entry.content.clone(),
);
}

updated_offset
});
let mut left_start = 0;

for (i, entry) in left.iter().enumerate() {
if i == left.len() - 1 {
// The last element should fill the remaining the width.
let len = left_end.saturating_sub(left_start);
self.print(
&Position { line, offset: left_start },
entry.style,
entry.colors,
entry.content.pad_to_width(len),
);
} else {
self.print(
&Position { line, offset: left_start },
entry.style,
entry.colors,
entry.content.clone(),
);

left_start += entry.content.len();
}
}
}

pub fn print<C>(&mut self, position: &Position, style: Style, colors: Colors, content: C)
Expand Down

0 comments on commit ff0dc68

Please sign in to comment.