refactor(statusline): Append elements using a consistent API

This is a preparation for the configurability which is about to be
implemented.
pull/2434/head
etienne-k 3 years ago
parent 73d6f34861
commit 1b50fbb1eb

@ -1,8 +1,4 @@
use helix_core::{ use helix_core::{coords_at_pos, encoding};
coords_at_pos,
encoding::{self, Encoding},
Position,
};
use helix_view::{ use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME}, document::{Mode, SCRATCH_BUFFER_NAME},
graphics::Rect, graphics::Rect,
@ -15,6 +11,7 @@ use crate::ui::ProgressSpinners;
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
use tui::text::{Span, Spans}; use tui::text::{Span, Spans};
/// A status line element contains the information about a component which can be displayed in the status line.
struct StatusLineElement { struct StatusLineElement {
/// The element /// The element
pub text: String, pub text: String,
@ -24,6 +21,22 @@ struct StatusLineElement {
pub style: Option<Style>, pub style: Option<Style>,
} }
struct RenderBuffer<'a> {
pub left: Spans<'a>,
pub center: Spans<'a>,
pub right: Spans<'a>,
}
impl<'a> RenderBuffer<'a> {
pub fn new() -> Self {
return Self {
left: Spans::default(),
center: Spans::default(),
right: Spans::default(),
};
}
}
pub struct StatusLine; pub struct StatusLine;
impl StatusLine { impl StatusLine {
@ -36,9 +49,9 @@ impl StatusLine {
is_focused: bool, is_focused: bool,
spinners: &ProgressSpinners, spinners: &ProgressSpinners,
) { ) {
//------------------------------- let mut buffer = RenderBuffer::new();
// Left side of the status line. // Left side of the status line.
//-------------------------------
let base_style = if is_focused { let base_style = if is_focused {
editor.theme.get("ui.statusline") editor.theme.get("ui.statusline")
@ -48,34 +61,21 @@ impl StatusLine {
surface.set_style(viewport.with_height(1), base_style); surface.set_style(viewport.with_height(1), base_style);
if is_focused { let mode_element = Self::render_mode(doc, is_focused);
let mode = Self::render_mode(doc); Self::append_left(&mut buffer, &base_style, mode_element);
surface.set_string(
viewport.x + 1,
viewport.y,
mode.text,
mode.style
.map_or(base_style, |s| base_style.clone().patch(s)),
);
}
let spinner = Self::render_lsp_spinner(doc, spinners); let spinner_element = Self::render_lsp_spinner(doc, spinners);
surface.set_string( Self::append_left(&mut buffer, &base_style, spinner_element);
viewport.x + 5,
// TODO: why x+1?
surface.set_spans(
viewport.x + 1,
viewport.y, viewport.y,
spinner.text, &buffer.left,
spinner buffer.left.width() as u16,
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
); );
//-------------------------------
// Right side of the status line. // Right side of the status line.
//-------------------------------
let mut right_side_text = Spans::default();
// Compute the individual info strings and add them to `right_side_text`.
// Diagnostics // Diagnostics
let diags = doc.diagnostics().iter().fold((0, 0), |mut counts, diag| { let diags = doc.diagnostics().iter().fold((0, 0), |mut counts, diag| {
@ -105,111 +105,96 @@ impl StatusLine {
}; };
if count > 0 { if count > 0 {
right_side_text.0.push(Span::styled( Self::append_right(&mut buffer, &base_style, state_element);
state_element.text, Self::append_right(&mut buffer, &base_style, count_element);
state_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
right_side_text.0.push(Span::styled(
count_element.text,
count_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
} }
} }
// Selections // Selections
let sels_count = doc.selection(view.id).len(); let sels_count = doc.selection(view.id).len();
let selections_element = Self::render_selections(sels_count); let selections_element = Self::render_selections(sels_count);
right_side_text.0.push(Span::styled( Self::append_right(&mut buffer, &base_style, selections_element);
selections_element.text,
selections_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
// Position // Position
let pos = coords_at_pos( let position_element = Self::render_position(doc, view);
doc.text().slice(..), Self::append_right(&mut buffer, &base_style, position_element);
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
);
let position_element = Self::render_position(&pos);
right_side_text.0.push(Span::styled(
position_element.text,
position_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
// Encoding // Encoding
let enc = doc.encoding(); if let Some(encoding_element) = Self::render_encoding(doc) {
if enc != encoding::UTF_8 { Self::append_right(&mut buffer, &base_style, encoding_element);
let encoding_element = Self::render_encoding(enc);
right_side_text.0.push(Span::styled(
encoding_element.text,
encoding_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
} }
// File type // File type
let file_type = doc.language_id().unwrap_or("text"); let file_type_element = Self::render_file_type(doc);
let file_type_element = Self::render_file_type(file_type); Self::append_right(&mut buffer, &base_style, file_type_element);
right_side_text.0.push(Span::styled(
file_type_element.text,
file_type_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
// Render to the statusline. // Render to the statusline.
surface.set_spans( surface.set_spans(
viewport.x viewport.x + viewport.width.saturating_sub(buffer.right.width() as u16),
+ viewport
.width
.saturating_sub(right_side_text.width() as u16),
viewport.y, viewport.y,
&right_side_text, &buffer.right,
right_side_text.width() as u16, buffer.right.width() as u16,
); );
//------------------------------- // Center of the status line.
// Middle / File path / Title
//-------------------------------
let title_element = Self::render_file_name(doc); let title_element = Self::render_file_name(doc);
Self::append_center(&mut buffer, &base_style, title_element);
// Width of the empty space between the left and center area and between the center and right area.
let spacing = 1u16;
surface.set_string_truncated( let edge_width = buffer.left.width().max(buffer.right.width()) as u16;
viewport.x + 8, // 8: 1 space + 3 char mode string + 1 space + 1 spinner + 1 space //let center_width = viewport.width
// - (buffer.left.width() as u16 + buffer.right.width() as u16 + 2 * spacing);
let center_max_width = viewport.width - (2 * edge_width + 2 * spacing);
let center_width = center_max_width.min(buffer.center.width() as u16);
surface.set_spans(
viewport.x + viewport.width / 2 - center_width / 2,
viewport.y, viewport.y,
title_element.text.as_str(), &buffer.center,
viewport center_width,
.width
.saturating_sub(6)
.saturating_sub(right_side_text.width() as u16 + 1) as usize, // "+ 1": a space between the title and the selection info
|_| {
title_element
.style
.map_or(base_style, |s| base_style.clone().patch(s))
},
true,
true,
); );
} }
fn render_mode(doc: &Document) -> StatusLineElement { fn append_left(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) {
buffer.left.0.push(Span::styled(
element.text,
element
.style
.map_or(*base_style, |s| base_style.clone().patch(s)),
));
}
fn append_center(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) {
buffer.center.0.push(Span::styled(
element.text,
element
.style
.map_or(*base_style, |s| base_style.clone().patch(s)),
));
}
fn append_right(buffer: &mut RenderBuffer, base_style: &Style, element: StatusLineElement) {
buffer.right.0.push(Span::styled(
element.text,
element
.style
.map_or(*base_style, |s| base_style.clone().patch(s)),
));
}
fn render_mode(doc: &Document, is_focused: bool) -> StatusLineElement {
return StatusLineElement { return StatusLineElement {
text: format!( text: format!(
"{}", "{}",
match doc.mode() { match doc.mode() {
Mode::Insert => "INS", Mode::Insert if is_focused => "INS",
Mode::Select => "SEL", Mode::Select if is_focused => "SEL",
Mode::Normal => "NOR", Mode::Normal if is_focused => "NOR",
// If not focused, explicitly leave an empty space instead of returning None.
_ => " ",
} }
), ),
style: None, style: None,
@ -222,6 +207,7 @@ impl StatusLine {
" {} ", " {} ",
doc.language_server() doc.language_server()
.and_then(|srv| spinners.get(srv.id()).and_then(|spinner| spinner.frame())) .and_then(|srv| spinners.get(srv.id()).and_then(|spinner| spinner.frame()))
// Even if there's no spinner; reserve its space to avoid elements frequently shifting.
.unwrap_or(" ") .unwrap_or(" ")
), ),
style: None, style: None,
@ -267,21 +253,36 @@ impl StatusLine {
}; };
} }
fn render_position(position: &Position) -> StatusLineElement { fn render_position(doc: &Document, view: &View) -> StatusLineElement {
let position = coords_at_pos(
doc.text().slice(..),
doc.selection(view.id)
.primary()
.cursor(doc.text().slice(..)),
);
return StatusLineElement { return StatusLineElement {
text: format!(" {}:{} ", position.row + 1, position.col + 1), text: format!(" {}:{} ", position.row + 1, position.col + 1),
style: None, style: None,
}; };
} }
fn render_encoding(encoding: &'static Encoding) -> StatusLineElement { fn render_encoding(doc: &Document) -> Option<StatusLineElement> {
return StatusLineElement { let enc = doc.encoding();
text: format!(" {} ", encoding.name()),
if enc != encoding::UTF_8 {
return Some(StatusLineElement {
text: format!(" {} ", enc.name()),
style: None, style: None,
}; });
} else {
return None;
}
} }
fn render_file_type(file_type: &str) -> StatusLineElement { fn render_file_type(doc: &Document) -> StatusLineElement {
let file_type = doc.language_id().unwrap_or("text");
return StatusLineElement { return StatusLineElement {
text: format!(" {} ", file_type), text: format!(" {} ", file_type),
style: None, style: None,

Loading…
Cancel
Save