@@ -0,0 +1,310 @@
1+use helix_core::{coords_at_pos, encoding};
3+ document::{Mode, SCRATCH_BUFFER_NAME},
6+ Document, Editor, View,
9+use crate::ui::ProgressSpinners;
11+use helix_view::editor::StatusLineElement as StatusLineElementID;
12+use tui::buffer::Buffer as Surface;
13+use tui::text::{Span, Spans};
15+pub struct RenderContext<'a> {
16+ pub editor: &'a Editor,
17+ pub doc: &'a Document,
20+ pub spinners: &'a ProgressSpinners,
21+ pub parts: RenderBuffer<'a>,
24+impl<'a> RenderContext<'a> {
30+ spinners: &'a ProgressSpinners,
38+ parts: RenderBuffer::default(),
44+pub struct RenderBuffer<'a> {
46+ pub center: Spans<'a>,
47+ pub right: Spans<'a>,
50+pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface) {
51+ let base_style = if context.focused {
52+ context.editor.theme.get("ui.statusline")
54+ context.editor.theme.get("ui.statusline.inactive")
57+ surface.set_style(viewport.with_height(1), base_style);
59+ let write_left = |context: &mut RenderContext, text, style| {
60+ append(&mut context.parts.left, text, &base_style, style)
62+ let write_center = |context: &mut RenderContext, text, style| {
63+ append(&mut context.parts.center, text, &base_style, style)
65+ let write_right = |context: &mut RenderContext, text, style| {
66+ append(&mut context.parts.right, text, &base_style, style)
69+ // Left side of the status line.
71+ let element_ids = &context.editor.config().statusline.left;
74+ .map(|element_id| get_render_function(*element_id))
75+ .for_each(|render| render(context, write_left));
81+ context.parts.left.width() as u16,
84+ // Right side of the status line.
86+ let element_ids = &context.editor.config().statusline.right;
89+ .map(|element_id| get_render_function(*element_id))
90+ .for_each(|render| render(context, write_right));
96+ .saturating_sub(context.parts.right.width() as u16),
98+ &context.parts.right,
99+ context.parts.right.width() as u16,
102+ // Center of the status line.
104+ let element_ids = &context.editor.config().statusline.center;
107+ .map(|element_id| get_render_function(*element_id))
108+ .for_each(|render| render(context, write_center));
110+ // Width of the empty space between the left and center area and between the center and right area.
113+ let edge_width = context.parts.left.width().max(context.parts.right.width()) as u16;
114+ let center_max_width = viewport.width.saturating_sub(2 * edge_width + 2 * spacing);
115+ let center_width = center_max_width.min(context.parts.center.width() as u16);
118+ viewport.x + viewport.width / 2 - center_width / 2,
120+ &context.parts.center,
125+fn append(buffer: &mut Spans, text: String, base_style: &Style, style: Option<Style>) {
126+ buffer.0.push(Span::styled(
128+ style.map_or(*base_style, |s| (*base_style).patch(s)),
132+fn get_render_function<F>(element_id: StatusLineElementID) -> impl Fn(&mut RenderContext, F)
134+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
137+ helix_view::editor::StatusLineElement::Mode => render_mode,
138+ helix_view::editor::StatusLineElement::Spinner => render_lsp_spinner,
139+ helix_view::editor::StatusLineElement::FileName => render_file_name,
140+ helix_view::editor::StatusLineElement::FileEncoding => render_file_encoding,
141+ helix_view::editor::StatusLineElement::FileType => render_file_type,
142+ helix_view::editor::StatusLineElement::Diagnostics => render_diagnostics,
143+ helix_view::editor::StatusLineElement::Selections => render_selections,
144+ helix_view::editor::StatusLineElement::Position => render_position,
148+fn render_mode<F>(context: &mut RenderContext, write: F)
150+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
152+ let visible = context.focused;
159+ match context.doc.mode() {
160+ Mode::Insert => "INS",
161+ Mode::Select => "SEL",
162+ Mode::Normal => "NOR",
165+ // If not focused, explicitly leave an empty space instead of returning None.
169+ if visible && context.editor.config().color_modes {
170+ match context.doc.mode() {
171+ Mode::Insert => Some(context.editor.theme.get("ui.statusline.insert")),
172+ Mode::Select => Some(context.editor.theme.get("ui.statusline.select")),
173+ Mode::Normal => Some(context.editor.theme.get("ui.statusline.normal")),
181+fn render_lsp_spinner<F>(context: &mut RenderContext, write: F)
183+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
194+ .and_then(|spinner| spinner.frame())
196+ // Even if there's no spinner; reserve its space to avoid elements frequently shifting.
203+fn render_diagnostics<F>(context: &mut RenderContext, write: F)
205+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
207+ let (warnings, errors) = context
211+ .fold((0, 0), |mut counts, diag| {
212+ use helix_core::diagnostic::Severity;
213+ match diag.severity {
214+ Some(Severity::Warning) => counts.0 += 1,
215+ Some(Severity::Error) | None => counts.1 += 1,
225+ Some(context.editor.theme.get("warning")),
227+ write(context, format!(" {} ", warnings), None);
234+ Some(context.editor.theme.get("error")),
236+ write(context, format!(" {} ", errors), None);
240+fn render_selections<F>(context: &mut RenderContext, write: F)
242+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
244+ let count = context.doc.selection(context.view.id).len();
247+ format!(" {} sel{} ", count, if count == 1 { "" } else { "s" }),
252+fn render_position<F>(context: &mut RenderContext, write: F)
254+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
256+ let position = coords_at_pos(
257+ context.doc.text().slice(..),
260+ .selection(context.view.id)
262+ .cursor(context.doc.text().slice(..)),
267+ format!(" {}:{} ", position.row + 1, position.col + 1),
272+fn render_file_encoding<F>(context: &mut RenderContext, write: F)
274+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
276+ let enc = context.doc.encoding();
278+ if enc != encoding::UTF_8 {
279+ write(context, format!(" {} ", enc.name()), None);
283+fn render_file_type<F>(context: &mut RenderContext, write: F)
285+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
287+ let file_type = context.doc.language_id().unwrap_or("text");
289+ write(context, format!(" {} ", file_type), None);
292+fn render_file_name<F>(context: &mut RenderContext, write: F)
294+ F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
297+ let rel_path = context.doc.relative_path();
300+ .map(|p| p.to_string_lossy())
301+ .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
305+ if context.doc.is_modified() { "[+]" } else { "" }
309+ write(context, title, None);