mirror of https://github.com/helix-editor/helix
feat(statusline): make the status line configurable
parent
c01e5aa822
commit
90db789577
|
@ -8,6 +8,7 @@ use helix_view::{
|
||||||
|
|
||||||
use crate::ui::ProgressSpinners;
|
use crate::ui::ProgressSpinners;
|
||||||
|
|
||||||
|
use helix_view::editor::StatusLineElement as StatusLineElementID;
|
||||||
use tui::buffer::Buffer as Surface;
|
use tui::buffer::Buffer as Surface;
|
||||||
use tui::text::{Span, Spans};
|
use tui::text::{Span, Spans};
|
||||||
|
|
||||||
|
@ -21,6 +22,14 @@ struct StatusLineElement {
|
||||||
pub style: Option<Style>,
|
pub style: Option<Style>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RenderContext<'a> {
|
||||||
|
pub document: &'a Document,
|
||||||
|
pub view: &'a View,
|
||||||
|
pub theme: &'a Theme,
|
||||||
|
pub focused: bool,
|
||||||
|
pub spinners: &'a ProgressSpinners,
|
||||||
|
}
|
||||||
|
|
||||||
struct RenderBuffer<'a> {
|
struct RenderBuffer<'a> {
|
||||||
pub left: Spans<'a>,
|
pub left: Spans<'a>,
|
||||||
pub center: Spans<'a>,
|
pub center: Spans<'a>,
|
||||||
|
@ -49,10 +58,15 @@ impl StatusLine {
|
||||||
is_focused: bool,
|
is_focused: bool,
|
||||||
spinners: &ProgressSpinners,
|
spinners: &ProgressSpinners,
|
||||||
) {
|
) {
|
||||||
|
let context = RenderContext {
|
||||||
|
document: doc,
|
||||||
|
view: view,
|
||||||
|
theme: &editor.theme,
|
||||||
|
focused: is_focused,
|
||||||
|
spinners: spinners,
|
||||||
|
};
|
||||||
let mut buffer = RenderBuffer::new();
|
let mut buffer = RenderBuffer::new();
|
||||||
|
|
||||||
// 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")
|
||||||
} else {
|
} else {
|
||||||
|
@ -61,15 +75,17 @@ impl StatusLine {
|
||||||
|
|
||||||
surface.set_style(viewport.with_height(1), base_style);
|
surface.set_style(viewport.with_height(1), base_style);
|
||||||
|
|
||||||
let mode_element = Self::render_mode(doc, is_focused);
|
// Left side of the status line.
|
||||||
Self::append_left(&mut buffer, &base_style, mode_element);
|
|
||||||
|
|
||||||
let spinner_element = Self::render_lsp_spinner(doc, spinners);
|
for element_id in &editor.config().status_line.left {
|
||||||
Self::append_left(&mut buffer, &base_style, spinner_element);
|
let elements = Self::render_element(&context, *element_id);
|
||||||
|
for element in elements {
|
||||||
|
Self::append_left(&mut buffer, &base_style, element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: why x+1?
|
|
||||||
surface.set_spans(
|
surface.set_spans(
|
||||||
viewport.x + 1,
|
viewport.x,
|
||||||
viewport.y,
|
viewport.y,
|
||||||
&buffer.left,
|
&buffer.left,
|
||||||
buffer.left.width() as u16,
|
buffer.left.width() as u16,
|
||||||
|
@ -77,30 +93,13 @@ impl StatusLine {
|
||||||
|
|
||||||
// Right side of the status line.
|
// Right side of the status line.
|
||||||
|
|
||||||
// Diagnostics
|
for element_id in &editor.config().status_line.right {
|
||||||
for diagnostic_element in Self::render_diagnostics(doc, &editor.theme) {
|
let elements = Self::render_element(&context, *element_id);
|
||||||
Self::append_right(&mut buffer, &base_style, diagnostic_element);
|
for element in elements {
|
||||||
|
Self::append_right(&mut buffer, &base_style, element);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Selections
|
|
||||||
let sels_count = doc.selection(view.id).len();
|
|
||||||
let selections_element = Self::render_selections(sels_count);
|
|
||||||
Self::append_right(&mut buffer, &base_style, selections_element);
|
|
||||||
|
|
||||||
// Position
|
|
||||||
let position_element = Self::render_position(doc, view);
|
|
||||||
Self::append_right(&mut buffer, &base_style, position_element);
|
|
||||||
|
|
||||||
// Encoding
|
|
||||||
if let Some(encoding_element) = Self::render_encoding(doc) {
|
|
||||||
Self::append_right(&mut buffer, &base_style, encoding_element);
|
|
||||||
}
|
|
||||||
|
|
||||||
// File type
|
|
||||||
let file_type_element = Self::render_file_type(doc);
|
|
||||||
Self::append_right(&mut buffer, &base_style, file_type_element);
|
|
||||||
|
|
||||||
// Render to the statusline.
|
|
||||||
surface.set_spans(
|
surface.set_spans(
|
||||||
viewport.x + viewport.width.saturating_sub(buffer.right.width() as u16),
|
viewport.x + viewport.width.saturating_sub(buffer.right.width() as u16),
|
||||||
viewport.y,
|
viewport.y,
|
||||||
|
@ -110,15 +109,17 @@ impl StatusLine {
|
||||||
|
|
||||||
// Center of the status line.
|
// Center of the status line.
|
||||||
|
|
||||||
let title_element = Self::render_file_name(doc);
|
for element_id in &editor.config().status_line.center {
|
||||||
Self::append_center(&mut buffer, &base_style, title_element);
|
let elements = Self::render_element(&context, *element_id);
|
||||||
|
for element in elements {
|
||||||
|
Self::append_center(&mut buffer, &base_style, element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Width of the empty space between the left and center area and between the center and right area.
|
// Width of the empty space between the left and center area and between the center and right area.
|
||||||
let spacing = 1u16;
|
let spacing = 1u16;
|
||||||
|
|
||||||
let edge_width = buffer.left.width().max(buffer.right.width()) as u16;
|
let edge_width = buffer.left.width().max(buffer.right.width()) as u16;
|
||||||
//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_max_width = viewport.width - (2 * edge_width + 2 * spacing);
|
||||||
let center_width = center_max_width.min(buffer.center.width() as u16);
|
let center_width = center_max_width.min(buffer.center.width() as u16);
|
||||||
|
|
||||||
|
@ -157,14 +158,41 @@ impl StatusLine {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_mode(doc: &Document, is_focused: bool) -> StatusLineElement {
|
fn render_element(
|
||||||
|
context: &RenderContext,
|
||||||
|
element_id: StatusLineElementID,
|
||||||
|
) -> Vec<StatusLineElement> {
|
||||||
|
return match element_id {
|
||||||
|
helix_view::editor::StatusLineElement::Mode => vec![Self::render_mode(context)],
|
||||||
|
helix_view::editor::StatusLineElement::Spinner => {
|
||||||
|
vec![Self::render_lsp_spinner(context)]
|
||||||
|
}
|
||||||
|
helix_view::editor::StatusLineElement::FileName => {
|
||||||
|
vec![Self::render_file_name(context)]
|
||||||
|
}
|
||||||
|
helix_view::editor::StatusLineElement::FileEncoding => {
|
||||||
|
Self::render_file_encoding(context).map_or(Vec::with_capacity(0), |e| vec![e])
|
||||||
|
}
|
||||||
|
helix_view::editor::StatusLineElement::FileType => {
|
||||||
|
vec![Self::render_file_type(context)]
|
||||||
|
}
|
||||||
|
helix_view::editor::StatusLineElement::Diagnostics => Self::render_diagnostics(context),
|
||||||
|
helix_view::editor::StatusLineElement::Selections => {
|
||||||
|
vec![Self::render_selections(context)]
|
||||||
|
}
|
||||||
|
helix_view::editor::StatusLineElement::Position => vec![Self::render_position(context)],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_mode(context: &RenderContext) -> StatusLineElement {
|
||||||
|
let visible = context.focused;
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!(
|
text: format!(
|
||||||
"{}",
|
" {} ",
|
||||||
match doc.mode() {
|
match context.document.mode() {
|
||||||
Mode::Insert if is_focused => "INS",
|
Mode::Insert if visible => "INS",
|
||||||
Mode::Select if is_focused => "SEL",
|
Mode::Select if visible => "SEL",
|
||||||
Mode::Normal if is_focused => "NOR",
|
Mode::Normal if visible => "NOR",
|
||||||
// If not focused, explicitly leave an empty space instead of returning None.
|
// If not focused, explicitly leave an empty space instead of returning None.
|
||||||
_ => " ",
|
_ => " ",
|
||||||
}
|
}
|
||||||
|
@ -173,12 +201,17 @@ impl StatusLine {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_lsp_spinner(doc: &Document, spinners: &ProgressSpinners) -> StatusLineElement {
|
fn render_lsp_spinner(context: &RenderContext) -> StatusLineElement {
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!(
|
text: format!(
|
||||||
" {} ",
|
" {} ",
|
||||||
doc.language_server()
|
context
|
||||||
.and_then(|srv| spinners.get(srv.id()).and_then(|spinner| spinner.frame()))
|
.document
|
||||||
|
.language_server()
|
||||||
|
.and_then(|srv| context
|
||||||
|
.spinners
|
||||||
|
.get(srv.id())
|
||||||
|
.and_then(|spinner| spinner.frame()))
|
||||||
// Even if there's no spinner; reserve its space to avoid elements frequently shifting.
|
// Even if there's no spinner; reserve its space to avoid elements frequently shifting.
|
||||||
.unwrap_or(" ")
|
.unwrap_or(" ")
|
||||||
),
|
),
|
||||||
|
@ -186,11 +219,15 @@ impl StatusLine {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_diagnostics(doc: &Document, theme: &Theme) -> Vec<StatusLineElement> {
|
fn render_diagnostics(context: &RenderContext) -> Vec<StatusLineElement> {
|
||||||
// 2 diagnostics types, each consisting of 2 elements (state + count)
|
// 2 diagnostics types, each consisting of 2 elements (state + count)
|
||||||
let mut elements: Vec<StatusLineElement> = Vec::with_capacity(4);
|
let mut elements: Vec<StatusLineElement> = Vec::with_capacity(4);
|
||||||
|
|
||||||
let diags = doc.diagnostics().iter().fold((0, 0), |mut counts, diag| {
|
let diags = context
|
||||||
|
.document
|
||||||
|
.diagnostics()
|
||||||
|
.iter()
|
||||||
|
.fold((0, 0), |mut counts, diag| {
|
||||||
use helix_core::diagnostic::Severity;
|
use helix_core::diagnostic::Severity;
|
||||||
match diag.severity {
|
match diag.severity {
|
||||||
Some(Severity::Warning) => counts.0 += 1,
|
Some(Severity::Warning) => counts.0 += 1,
|
||||||
|
@ -205,13 +242,13 @@ impl StatusLine {
|
||||||
let (count, state_element, count_element) = match i {
|
let (count, state_element, count_element) = match i {
|
||||||
0 => (
|
0 => (
|
||||||
warnings,
|
warnings,
|
||||||
Self::render_diagnostics_warning_state(theme),
|
Self::render_diagnostics_warning_state(context),
|
||||||
Self::render_diagnostics_warning_count(warnings),
|
Self::render_diagnostics_warning_count(context, warnings),
|
||||||
),
|
),
|
||||||
1 => (
|
1 => (
|
||||||
errors,
|
errors,
|
||||||
Self::render_diagnostics_error_state(theme),
|
Self::render_diagnostics_error_state(context),
|
||||||
Self::render_diagnostics_error_count(errors),
|
Self::render_diagnostics_error_count(context, errors),
|
||||||
),
|
),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
@ -225,51 +262,56 @@ impl StatusLine {
|
||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_diagnostics_warning_state(theme: &Theme) -> StatusLineElement {
|
fn render_diagnostics_warning_state(context: &RenderContext) -> StatusLineElement {
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!("●"),
|
text: format!("●"),
|
||||||
style: Some(theme.get("warning")),
|
style: Some(context.theme.get("warning")),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_diagnostics_warning_count(warnings: usize) -> StatusLineElement {
|
fn render_diagnostics_warning_count(
|
||||||
|
_context: &RenderContext,
|
||||||
|
warning_count: usize,
|
||||||
|
) -> StatusLineElement {
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!(" {} ", warnings),
|
text: format!(" {} ", warning_count),
|
||||||
style: None,
|
style: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_diagnostics_error_state(theme: &Theme) -> StatusLineElement {
|
fn render_diagnostics_error_state(context: &RenderContext) -> StatusLineElement {
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!("●"),
|
text: format!("●"),
|
||||||
style: Some(theme.get("error")),
|
style: Some(context.theme.get("error")),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_diagnostics_error_count(errors: usize) -> StatusLineElement {
|
fn render_diagnostics_error_count(
|
||||||
|
_context: &RenderContext,
|
||||||
|
error_count: usize,
|
||||||
|
) -> StatusLineElement {
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!(" {} ", errors),
|
text: format!(" {} ", error_count),
|
||||||
style: None,
|
style: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_selections(selections: usize) -> StatusLineElement {
|
fn render_selections(context: &RenderContext) -> StatusLineElement {
|
||||||
|
let count = context.document.selection(context.view.id).len();
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!(
|
text: format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }),
|
||||||
" {} sel{} ",
|
|
||||||
&selections,
|
|
||||||
if selections == 1 { "" } else { "s" }
|
|
||||||
),
|
|
||||||
style: None,
|
style: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_position(doc: &Document, view: &View) -> StatusLineElement {
|
fn render_position(context: &RenderContext) -> StatusLineElement {
|
||||||
let position = coords_at_pos(
|
let position = coords_at_pos(
|
||||||
doc.text().slice(..),
|
context.document.text().slice(..),
|
||||||
doc.selection(view.id)
|
context
|
||||||
|
.document
|
||||||
|
.selection(context.view.id)
|
||||||
.primary()
|
.primary()
|
||||||
.cursor(doc.text().slice(..)),
|
.cursor(context.document.text().slice(..)),
|
||||||
);
|
);
|
||||||
|
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
|
@ -278,8 +320,8 @@ impl StatusLine {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_encoding(doc: &Document) -> Option<StatusLineElement> {
|
fn render_file_encoding(context: &RenderContext) -> Option<StatusLineElement> {
|
||||||
let enc = doc.encoding();
|
let enc = context.document.encoding();
|
||||||
|
|
||||||
if enc != encoding::UTF_8 {
|
if enc != encoding::UTF_8 {
|
||||||
return Some(StatusLineElement {
|
return Some(StatusLineElement {
|
||||||
|
@ -291,8 +333,8 @@ impl StatusLine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_file_type(doc: &Document) -> StatusLineElement {
|
fn render_file_type(context: &RenderContext) -> StatusLineElement {
|
||||||
let file_type = doc.language_id().unwrap_or("text");
|
let file_type = context.document.language_id().unwrap_or("text");
|
||||||
|
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
text: format!(" {} ", file_type),
|
text: format!(" {} ", file_type),
|
||||||
|
@ -300,14 +342,22 @@ impl StatusLine {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_file_name(doc: &Document) -> StatusLineElement {
|
fn render_file_name(context: &RenderContext) -> StatusLineElement {
|
||||||
let title = {
|
let title = {
|
||||||
let rel_path = doc.relative_path();
|
let rel_path = context.document.relative_path();
|
||||||
let path = rel_path
|
let path = rel_path
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|p| p.to_string_lossy())
|
.map(|p| p.to_string_lossy())
|
||||||
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
|
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
|
||||||
format!("{}{}", path, if doc.is_modified() { "[+]" } else { "" })
|
format!(
|
||||||
|
" {}{} ",
|
||||||
|
path,
|
||||||
|
if context.document.is_modified() {
|
||||||
|
"[+]"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
return StatusLineElement {
|
return StatusLineElement {
|
||||||
|
|
|
@ -148,6 +148,7 @@ pub struct Config {
|
||||||
pub auto_info: bool,
|
pub auto_info: bool,
|
||||||
pub file_picker: FilePickerConfig,
|
pub file_picker: FilePickerConfig,
|
||||||
/// Shape for cursor in each mode
|
/// Shape for cursor in each mode
|
||||||
|
pub status_line: StatusLineConfig,
|
||||||
pub cursor_shape: CursorShapeConfig,
|
pub cursor_shape: CursorShapeConfig,
|
||||||
/// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`.
|
/// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`.
|
||||||
pub true_color: bool,
|
pub true_color: bool,
|
||||||
|
@ -180,6 +181,54 @@ pub struct SearchConfig {
|
||||||
pub wrap_around: bool,
|
pub wrap_around: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
|
||||||
|
pub struct StatusLineConfig {
|
||||||
|
pub left: Vec<StatusLineElement>,
|
||||||
|
pub center: Vec<StatusLineElement>,
|
||||||
|
pub right: Vec<StatusLineElement>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for StatusLineConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
use StatusLineElement as E;
|
||||||
|
|
||||||
|
return Self {
|
||||||
|
left: vec![E::Mode, E::Spinner, E::FileName],
|
||||||
|
center: vec![],
|
||||||
|
right: vec![E::Diagnostics, E::Selections, E::Position, E::FileEncoding],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub enum StatusLineElement {
|
||||||
|
/// The editor mode (Normal, Insert, Visual/Selection)
|
||||||
|
Mode,
|
||||||
|
|
||||||
|
/// The LSP activity spinner
|
||||||
|
Spinner,
|
||||||
|
|
||||||
|
/// The file nane/path, including a dirty flag if it's unsaved
|
||||||
|
FileName,
|
||||||
|
|
||||||
|
/// The file encoding
|
||||||
|
FileEncoding,
|
||||||
|
|
||||||
|
/// The file type (language ID or "text")
|
||||||
|
FileType,
|
||||||
|
|
||||||
|
/// A summary of the number of errors and warnings
|
||||||
|
Diagnostics,
|
||||||
|
|
||||||
|
/// The number of selections (cursors)
|
||||||
|
Selections,
|
||||||
|
|
||||||
|
/// The cursor position
|
||||||
|
Position,
|
||||||
|
}
|
||||||
|
|
||||||
// Cursor shape is read and used on every rendered frame and so needs
|
// Cursor shape is read and used on every rendered frame and so needs
|
||||||
// to be fast. Therefore we avoid a hashmap and use an enum indexed array.
|
// to be fast. Therefore we avoid a hashmap and use an enum indexed array.
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
@ -409,6 +458,7 @@ impl Default for Config {
|
||||||
completion_trigger_len: 2,
|
completion_trigger_len: 2,
|
||||||
auto_info: true,
|
auto_info: true,
|
||||||
file_picker: FilePickerConfig::default(),
|
file_picker: FilePickerConfig::default(),
|
||||||
|
status_line: StatusLineConfig::default(),
|
||||||
cursor_shape: CursorShapeConfig::default(),
|
cursor_shape: CursorShapeConfig::default(),
|
||||||
true_color: false,
|
true_color: false,
|
||||||
search: SearchConfig::default(),
|
search: SearchConfig::default(),
|
||||||
|
|
Loading…
Reference in New Issue