refactor(statusline): split the statusline implementation into different functions

pull/2434/head
etienne-k 2 years ago
parent bf691a1a02
commit 73d6f34861

@ -1,13 +1,28 @@
use helix_core::{coords_at_pos, encoding};
use helix_core::{
coords_at_pos,
encoding::{self, Encoding},
Position,
};
use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME},
graphics::Rect,
Document, Editor, View,
theme::Style,
Document, Editor, Theme, View,
};
use crate::ui::ProgressSpinners;
use tui::buffer::Buffer as Surface;
use tui::text::{Span, Spans};
struct StatusLineElement {
/// The element
pub text: String,
/// The style to be used to render the element (this style will be merged with the base style).
/// If not set, a default base style will be used.
pub style: Option<Style>,
}
pub struct StatusLine;
@ -21,33 +36,38 @@ impl StatusLine {
is_focused: bool,
spinners: &ProgressSpinners,
) {
use tui::text::{Span, Spans};
//-------------------------------
// Left side of the status line.
//-------------------------------
let mode = match doc.mode() {
Mode::Insert => "INS",
Mode::Select => "SEL",
Mode::Normal => "NOR",
};
let progress = doc
.language_server()
.and_then(|srv| spinners.get(srv.id()).and_then(|spinner| spinner.frame()))
.unwrap_or("");
let base_style = if is_focused {
editor.theme.get("ui.statusline")
} else {
editor.theme.get("ui.statusline.inactive")
};
// statusline
surface.set_style(viewport.with_height(1), base_style);
if is_focused {
surface.set_string(viewport.x + 1, viewport.y, mode, base_style);
let mode = Self::render_mode(doc);
surface.set_string(
viewport.x + 1,
viewport.y,
mode.text,
mode.style
.map_or(base_style, |s| base_style.clone().patch(s)),
);
}
surface.set_string(viewport.x + 5, viewport.y, progress, base_style);
let spinner = Self::render_lsp_spinner(doc, spinners);
surface.set_string(
viewport.x + 5,
viewport.y,
spinner.text,
spinner
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
);
//-------------------------------
// Right side of the status line.
@ -68,33 +88,47 @@ impl StatusLine {
counts
});
let (warnings, errors) = diags;
let warning_style = editor.theme.get("warning");
let error_style = editor.theme.get("error");
for i in 0..2 {
let (count, style) = match i {
0 => (warnings, warning_style),
1 => (errors, error_style),
let (count, state_element, count_element) = match i {
0 => (
warnings,
Self::render_diagnostics_warning_state(&editor.theme),
Self::render_diagnostics_warning_count(warnings),
),
1 => (
errors,
Self::render_diagnostics_error_state(&editor.theme),
Self::render_diagnostics_error_count(errors),
),
_ => unreachable!(),
};
if count == 0 {
continue;
if count > 0 {
right_side_text.0.push(Span::styled(
state_element.text,
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)),
));
}
let style = base_style.patch(style);
right_side_text.0.push(Span::styled("●", style));
right_side_text
.0
.push(Span::styled(format!(" {} ", count), base_style));
}
// Selections
let sels_count = doc.selection(view.id).len();
let selections_element = Self::render_selections(sels_count);
right_side_text.0.push(Span::styled(
format!(
" {} sel{} ",
sels_count,
if sels_count == 1 { "" } else { "s" }
),
base_style,
selections_element.text,
selections_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
// Position
@ -104,24 +138,35 @@ impl StatusLine {
.primary()
.cursor(doc.text().slice(..)),
);
let position_element = Self::render_position(&pos);
right_side_text.0.push(Span::styled(
format!(" {}:{} ", pos.row + 1, pos.col + 1), // Convert to 1-indexing.
base_style,
position_element.text,
position_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
));
// Encoding
let enc = doc.encoding();
if enc != encoding::UTF_8 {
right_side_text
.0
.push(Span::styled(format!(" {} ", enc.name()), base_style));
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
let file_type = doc.language_id().unwrap_or("text");
right_side_text
.0
.push(Span::styled(format!(" {} ", file_type), base_style));
let file_type_element = Self::render_file_type(file_type);
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.
surface.set_spans(
@ -137,26 +182,125 @@ impl StatusLine {
//-------------------------------
// Middle / File path / Title
//-------------------------------
let title = {
let rel_path = doc.relative_path();
let path = rel_path
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" })
};
let title_element = Self::render_file_name(doc);
surface.set_string_truncated(
viewport.x + 8, // 8: 1 space + 3 char mode string + 1 space + 1 spinner + 1 space
viewport.y,
&title,
title_element.text.as_str(),
viewport
.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
|_| base_style,
|_| {
title_element
.style
.map_or(base_style, |s| base_style.clone().patch(s))
},
true,
true,
);
}
fn render_mode(doc: &Document) -> StatusLineElement {
return StatusLineElement {
text: format!(
"{}",
match doc.mode() {
Mode::Insert => "INS",
Mode::Select => "SEL",
Mode::Normal => "NOR",
}
),
style: None,
};
}
fn render_lsp_spinner(doc: &Document, spinners: &ProgressSpinners) -> StatusLineElement {
return StatusLineElement {
text: format!(
"{}",
doc.language_server()
.and_then(|srv| spinners.get(srv.id()).and_then(|spinner| spinner.frame()))
.unwrap_or("")
),
style: None,
};
}
fn render_diagnostics_warning_state(theme: &Theme) -> StatusLineElement {
return StatusLineElement {
text: format!("●"),
style: Some(theme.get("warning")),
};
}
fn render_diagnostics_warning_count(warnings: usize) -> StatusLineElement {
return StatusLineElement {
text: format!(" {} ", warnings),
style: None,
};
}
fn render_diagnostics_error_state(theme: &Theme) -> StatusLineElement {
return StatusLineElement {
text: format!("●"),
style: Some(theme.get("error")),
};
}
fn render_diagnostics_error_count(errors: usize) -> StatusLineElement {
return StatusLineElement {
text: format!(" {} ", errors),
style: None,
};
}
fn render_selections(selections: usize) -> StatusLineElement {
return StatusLineElement {
text: format!(
" {} sel{} ",
&selections,
if selections == 1 { "" } else { "s" }
),
style: None,
};
}
fn render_position(position: &Position) -> StatusLineElement {
return StatusLineElement {
text: format!(" {}:{} ", position.row + 1, position.col + 1),
style: None,
};
}
fn render_encoding(encoding: &'static Encoding) -> StatusLineElement {
return StatusLineElement {
text: format!(" {} ", encoding.name()),
style: None,
};
}
fn render_file_type(file_type: &str) -> StatusLineElement {
return StatusLineElement {
text: format!(" {} ", file_type),
style: None,
};
}
fn render_file_name(doc: &Document) -> StatusLineElement {
let title = {
let rel_path = doc.relative_path();
let path = rel_path
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" })
};
return StatusLineElement {
text: title,
style: None,
};
}
}

Loading…
Cancel
Save