diff --git a/helix-term/src/ui/statusline.rs b/helix-term/src/ui/statusline.rs index 562c0d9a9f309..93a13b94a159e 100644 --- a/helix-term/src/ui/statusline.rs +++ b/helix-term/src/ui/statusline.rs @@ -11,7 +11,7 @@ use crate::ui::ProgressSpinners; use helix_view::editor::StatusLineElement as StatusLineElementID; use tui::buffer::Buffer as Surface; -use tui::text::Spans; +use tui::text::{Span, Spans}; pub struct RenderContext<'a> { pub editor: &'a Editor, @@ -60,7 +60,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface let spacer = { let spacer_str = &config.statusline.spacer; - Spans::single(spacer_str.clone(), context.base_style) + Spans::from(Span::styled(spacer_str.clone(), context.base_style)) }; context.parts.left = Spans::join( @@ -168,7 +168,7 @@ fn render_mode<'a>(context: &RenderContext) -> Spans<'a> { if context.focused { if config.color_modes { - Spans::single( + Span::styled( format!(" {} ", mode_str), match context.editor.mode() { Mode::Normal => context.editor.theme.get("ui.statusline.normal"), @@ -176,11 +176,12 @@ fn render_mode<'a>(context: &RenderContext) -> Spans<'a> { Mode::Insert => context.editor.theme.get("ui.statusline.insert"), }, ) + .into() } else { - Spans::single(String::from(mode_str), context.base_style) + Span::styled(String::from(mode_str), context.base_style).into() } } else { - Spans::new() + Spans::default() } } @@ -194,9 +195,9 @@ fn render_lsp_spinner<'a>(context: &RenderContext) -> Spans<'a> { }); if let Some(frame) = spinner { - Spans::single(String::from(frame), context.base_style) + Span::styled(String::from(frame), context.base_style).into() } else { - Spans::new() + Spans::default() } } @@ -213,7 +214,7 @@ fn render_diagnostics<'a>(context: &RenderContext) -> Spans<'a> { _ => (warnings, errors), }); - let mut spans = Spans::new(); + let mut spans = Spans::default(); if warnings > 0 { spans.push("●", context.editor.theme.get("warning")); @@ -243,7 +244,7 @@ fn render_workspace_diagnostics<'a>(context: &RenderContext) -> Spans<'a> { }, ); - let mut spans = Spans::new(); + let mut spans = Spans::default(); if warnings > 0 || errors > 0 { spans.push("W", context.base_style); @@ -267,19 +268,21 @@ fn render_workspace_diagnostics<'a>(context: &RenderContext) -> Spans<'a> { fn render_selections<'a>(context: &RenderContext) -> Spans<'a> { let count = context.doc.selection(context.view.id).len(); - Spans::single( + Span::styled( format!("{} sel{}", count, if count == 1 { "" } else { "s" }), context.base_style, ) + .into() } fn render_primary_selection_length<'a>(context: &RenderContext) -> Spans<'a> { let tot_sel = context.doc.selection(context.view.id).primary().len(); - Spans::single( + Span::styled( format!("{} char{}", tot_sel, if tot_sel == 1 { "" } else { "s" }), context.base_style, ) + .into() } fn get_position(context: &RenderContext) -> Position { @@ -296,36 +299,39 @@ fn get_position(context: &RenderContext) -> Position { fn render_position<'a>(context: &RenderContext) -> Spans<'a> { let position = get_position(context); - Spans::single( + Span::styled( format!("{}:{}", position.row + 1, position.col + 1), context.base_style, ) + .into() } fn render_total_line_numbers<'a>(context: &RenderContext) -> Spans<'a> { - Spans::single( + Span::styled( context.doc.text().len_lines().to_string(), context.base_style, ) + .into() } fn render_position_percentage<'a>(context: &RenderContext) -> Spans<'a> { let position = get_position(context); let maxrows = context.doc.text().len_lines(); - Spans::single( + Span::styled( format!("{}%", (position.row + 1) * 100 / maxrows), context.base_style, ) + .into() } fn render_file_encoding<'a>(context: &RenderContext) -> Spans<'a> { let enc = context.doc.encoding(); if enc != encoding::UTF_8 { - Spans::single(enc.name(), context.base_style) + Span::styled(enc.name(), context.base_style).into() } else { - Spans::new() + Spans::default() } } @@ -348,13 +354,13 @@ fn render_file_line_ending<'a>(context: &RenderContext) -> Spans<'a> { PS => "PS", // U+2029 -- ParagraphSeparator }; - Spans::single(line_ending, context.base_style) + Span::styled(line_ending, context.base_style).into() } fn render_file_type<'a>(context: &RenderContext) -> Spans<'a> { let file_type = context.doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME); - Spans::single(String::from(file_type), context.base_style) + Span::styled(String::from(file_type), context.base_style).into() } fn render_file_name<'a>(context: &RenderContext) -> Spans<'a> { @@ -367,22 +373,22 @@ fn render_file_name<'a>(context: &RenderContext) -> Spans<'a> { format!("{}", path) }; - Spans::single(title, context.base_style) + Span::styled(title, context.base_style).into() } fn render_file_modification_indicator<'a>(context: &RenderContext) -> Spans<'a> { if context.doc.is_modified() { - Spans::single(String::from("[+]"), context.base_style) + Span::styled(String::from("[+]"), context.base_style).into() } else { - Spans::new() + Spans::default() } } -fn render_read_only_indicator<'a>(context: &mut RenderContext) -> Spans<'a> { +fn render_read_only_indicator<'a>(context: &RenderContext) -> Spans<'a> { if context.doc.readonly { - Spans::single("[readonly]", context.base_style.clone()) + Span::styled(String::from("[readonly]"), context.base_style).into() } else { - Spans::new() + Spans::default() } } @@ -396,32 +402,33 @@ fn render_file_base_name<'a>(context: &RenderContext) -> Spans<'a> { String::from(path) }; - Spans::single(title, context.base_style) + Span::styled(title, context.base_style).into() } fn render_separator<'a>(context: &RenderContext) -> Spans<'a> { - Spans::single( + Span::styled( context.editor.config().statusline.separator.to_string(), context.editor.theme.get("ui.statusline.separator"), ) + .into() } fn render_spacer<'a>(context: &RenderContext) -> Spans<'a> { - Spans::single("", context.base_style) + Span::styled("", context.base_style).into() } fn render_version_control<'a>(context: &RenderContext) -> Spans<'a> { if let Some(head) = context.doc.version_control_head() { - Spans::single(head.to_string(), context.base_style) + Span::styled(head.to_string(), context.base_style).into() } else { - Spans::new() + Spans::default() } } fn render_register<'a>(context: &RenderContext) -> Spans<'a> { if let Some(reg) = context.editor.selected_register { - Spans::single(format!("reg={}", reg), context.base_style) + Span::styled(format!("reg={}", reg), context.base_style).into() } else { - Spans::new() + Spans::default() } } diff --git a/helix-tui/src/text.rs b/helix-tui/src/text.rs index f2016788384e0..5de0b3c42ab74 100644 --- a/helix-tui/src/text.rs +++ b/helix-tui/src/text.rs @@ -249,20 +249,6 @@ impl<'a> Spans<'a> { self.0.push(Span::styled(string, style)); } - /// Creates a new, empty `Spans`. - pub fn new() -> Spans<'a> { - Spans(vec![]) - } - - /// Constructs a single, styled `Span` with the given `string` and `style` - /// and wraps it in a `Spans`. - pub fn single(string: T, style: Style) -> Spans<'a> - where - T: Into>, - { - Spans(vec![Span::styled(string, style)]) - } - /// Intersperses `sep` between items of the given iterator then returns the concatenated Spans. /// /// ## Examples