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

pull/2434/head
etienne-k 3 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::{ use helix_view::{
document::{Mode, SCRATCH_BUFFER_NAME}, document::{Mode, SCRATCH_BUFFER_NAME},
graphics::Rect, graphics::Rect,
Document, Editor, View, theme::Style,
Document, Editor, Theme, View,
}; };
use crate::ui::ProgressSpinners; use crate::ui::ProgressSpinners;
use tui::buffer::Buffer as Surface; 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; pub struct StatusLine;
@ -21,33 +36,38 @@ impl StatusLine {
is_focused: bool, is_focused: bool,
spinners: &ProgressSpinners, spinners: &ProgressSpinners,
) { ) {
use tui::text::{Span, Spans};
//------------------------------- //-------------------------------
// Left side of the status line. // 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 { let base_style = if is_focused {
editor.theme.get("ui.statusline") editor.theme.get("ui.statusline")
} else { } else {
editor.theme.get("ui.statusline.inactive") editor.theme.get("ui.statusline.inactive")
}; };
// statusline
surface.set_style(viewport.with_height(1), base_style); surface.set_style(viewport.with_height(1), base_style);
if is_focused { 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. // Right side of the status line.
@ -68,33 +88,47 @@ impl StatusLine {
counts counts
}); });
let (warnings, errors) = diags; let (warnings, errors) = diags;
let warning_style = editor.theme.get("warning");
let error_style = editor.theme.get("error");
for i in 0..2 { for i in 0..2 {
let (count, style) = match i { let (count, state_element, count_element) = match i {
0 => (warnings, warning_style), 0 => (
1 => (errors, error_style), 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!(), _ => 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 // 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);
right_side_text.0.push(Span::styled( right_side_text.0.push(Span::styled(
format!( selections_element.text,
" {} sel{} ", selections_element
sels_count, .style
if sels_count == 1 { "" } else { "s" } .map_or(base_style, |s| base_style.clone().patch(s)),
),
base_style,
)); ));
// Position // Position
@ -104,24 +138,35 @@ impl StatusLine {
.primary() .primary()
.cursor(doc.text().slice(..)), .cursor(doc.text().slice(..)),
); );
let position_element = Self::render_position(&pos);
right_side_text.0.push(Span::styled( right_side_text.0.push(Span::styled(
format!(" {}:{} ", pos.row + 1, pos.col + 1), // Convert to 1-indexing. position_element.text,
base_style, position_element
.style
.map_or(base_style, |s| base_style.clone().patch(s)),
)); ));
// Encoding // Encoding
let enc = doc.encoding(); let enc = doc.encoding();
if enc != encoding::UTF_8 { if enc != encoding::UTF_8 {
right_side_text let encoding_element = Self::render_encoding(enc);
.0 right_side_text.0.push(Span::styled(
.push(Span::styled(format!(" {} ", enc.name()), base_style)); 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 = doc.language_id().unwrap_or("text");
right_side_text let file_type_element = Self::render_file_type(file_type);
.0 right_side_text.0.push(Span::styled(
.push(Span::styled(format!(" {} ", file_type), base_style)); 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(
@ -137,26 +182,125 @@ impl StatusLine {
//------------------------------- //-------------------------------
// Middle / File path / Title // Middle / File path / Title
//------------------------------- //-------------------------------
let title = { let title_element = Self::render_file_name(doc);
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 { "" })
};
surface.set_string_truncated( surface.set_string_truncated(
viewport.x + 8, // 8: 1 space + 3 char mode string + 1 space + 1 spinner + 1 space viewport.x + 8, // 8: 1 space + 3 char mode string + 1 space + 1 spinner + 1 space
viewport.y, viewport.y,
&title, title_element.text.as_str(),
viewport viewport
.width .width
.saturating_sub(6) .saturating_sub(6)
.saturating_sub(right_side_text.width() as u16 + 1) as usize, // "+ 1": a space between the title and the selection info .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,
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