From e5f4d21bf081e97f25e99e316db30b96a01458e0 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 17:39:50 -0400 Subject: [PATCH 1/8] Improve statusline * Change diagnostic count display to show counts of individual diagnostic types next to their corresponding gutter dots. * Add selection count to the statusline. --- helix-term/src/ui/editor.rs | 112 +++++++++++++++++++++++++++--------- 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 97658c645012..091e6e016906 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -548,6 +548,8 @@ impl EditorView { theme: &Theme, is_focused: bool, ) { + use tui::text::{Span, Spans}; + //------------------------------- // Left side of the status line. //------------------------------- @@ -566,17 +568,17 @@ impl EditorView { }) .unwrap_or(""); - let style = if is_focused { + let base_style = if is_focused { theme.get("ui.statusline") } else { theme.get("ui.statusline.inactive") }; // statusline - surface.set_style(viewport.with_height(1), style); + surface.set_style(viewport.with_height(1), base_style); if is_focused { - surface.set_string(viewport.x + 1, viewport.y, mode, style); + surface.set_string(viewport.x + 1, viewport.y, mode, base_style); } - surface.set_string(viewport.x + 5, viewport.y, progress, style); + surface.set_string(viewport.x + 5, viewport.y, progress, base_style); if let Some(path) = doc.relative_path() { let path = path.to_string_lossy(); @@ -587,7 +589,7 @@ impl EditorView { viewport.y, title, viewport.width.saturating_sub(6) as usize, - style, + base_style, ); } @@ -595,8 +597,65 @@ impl EditorView { // Right side of the status line. //------------------------------- - // Compute the individual info strings. - let diag_count = format!("{}", doc.diagnostics().len()); + let mut right_side_text = Spans::default(); + + // Compute the individual info strings and add them to `right_side_text`. + + // Diagnostics + let diags = doc + .diagnostics() + .iter() + .fold((0, 0, 0, 0), |mut counts, diag| { + use helix_core::diagnostic::Severity; + match diag.severity { + Some(Severity::Warning) => counts.0 += 1, + Some(Severity::Error) | None => counts.1 += 1, + Some(Severity::Info) => counts.2 += 1, + Some(Severity::Hint) => counts.3 += 1, + } + counts + }); + let (warnings, errors, infos, hints) = diags; + let warning_style = theme.get("warning"); + let error_style = theme.get("error"); + let info_style = theme.get("info"); + let hint_style = theme.get("hint"); + for i in 0..4 { + let (count, style) = match i { + 0 => (warnings, warning_style), + 1 => (errors, error_style), + 2 => (infos, info_style), + 3 => (hints, hint_style), + _ => unreachable!(), + }; + if count == 0 { + continue; + } + let style = Style { + bg: base_style.bg, + ..style + }; + right_side_text.0.push(Span { + content: Cow::Borrowed("●"), + style, + }); + right_side_text.0.push(Span { + content: Cow::Owned(format!(" {} ", count)), + style: base_style, + }); + } + + // Selections + let sels_count = doc.selection(view.id).len(); + right_side_text.0.push(Span { + content: Cow::Owned(format!( + " {} sel{} ", + sels_count, + if sels_count == 1 { "" } else { "s" } + )), + style: base_style, + }); + // let indent_info = match doc.indent_style { // IndentStyle::Tabs => "tabs", // IndentStyle::Spaces(1) => "spaces:1", @@ -609,29 +668,28 @@ impl EditorView { // IndentStyle::Spaces(8) => "spaces:8", // _ => "indent:ERROR", // }; - let position_info = { - let pos = coords_at_pos( - doc.text().slice(..), - doc.selection(view.id) - .primary() - .cursor(doc.text().slice(..)), - ); - format!("{}:{}", pos.row + 1, pos.col + 1) // convert to 1-indexing - }; - // Render them to the status line together. - let right_side_text = format!( - "{} {} ", - &diag_count[..diag_count.len().min(4)], - // indent_info, - position_info + // Position + let pos = coords_at_pos( + doc.text().slice(..), + doc.selection(view.id) + .primary() + .cursor(doc.text().slice(..)), ); - let text_len = right_side_text.len() as u16; - surface.set_string( - viewport.x + viewport.width.saturating_sub(text_len), + right_side_text.0.push(Span { + content: Cow::Owned(format!(" {}:{} ", pos.row + 1, pos.col + 1)), // Convert to 1-indexing. + style: base_style, + }); + + // Render to the statusline. + surface.set_spans( + viewport.x + + viewport + .width + .saturating_sub(right_side_text.width() as u16), viewport.y, - right_side_text, - style, + &right_side_text, + right_side_text.width() as u16, ); } From 7d78ccea3c7fce4711fbe21ae10478e18b88cb61 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 21:47:16 -0400 Subject: [PATCH 2/8] Do not display info or hint count in statusline --- helix-term/src/ui/editor.rs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 091e6e016906..55ee51831c7b 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -602,30 +602,22 @@ impl EditorView { // Compute the individual info strings and add them to `right_side_text`. // Diagnostics - let diags = doc - .diagnostics() - .iter() - .fold((0, 0, 0, 0), |mut counts, diag| { - use helix_core::diagnostic::Severity; - match diag.severity { - Some(Severity::Warning) => counts.0 += 1, - Some(Severity::Error) | None => counts.1 += 1, - Some(Severity::Info) => counts.2 += 1, - Some(Severity::Hint) => counts.3 += 1, - } - counts - }); - let (warnings, errors, infos, hints) = diags; + let diags = doc.diagnostics().iter().fold((0, 0), |mut counts, diag| { + use helix_core::diagnostic::Severity; + match diag.severity { + Some(Severity::Warning) => counts.0 += 1, + Some(Severity::Error) | None => counts.1 += 1, + _ => {} + } + counts + }); + let (warnings, errors) = diags; let warning_style = theme.get("warning"); let error_style = theme.get("error"); - let info_style = theme.get("info"); - let hint_style = theme.get("hint"); - for i in 0..4 { + for i in 0..2 { let (count, style) = match i { 0 => (warnings, warning_style), 1 => (errors, error_style), - 2 => (infos, info_style), - 3 => (hints, hint_style), _ => unreachable!(), }; if count == 0 { From b9f4b33f3d1d260daee2a89d8e87d8465fb7827d Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 21:48:31 -0400 Subject: [PATCH 3/8] Reduce padding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Blaž Hrastnik --- helix-term/src/ui/editor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 55ee51831c7b..08fce76ec972 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -641,7 +641,7 @@ impl EditorView { let sels_count = doc.selection(view.id).len(); right_side_text.0.push(Span { content: Cow::Owned(format!( - " {} sel{} ", + " {} sel{} ", sels_count, if sels_count == 1 { "" } else { "s" } )), From 7cb523f264920ea0a3f8b6e2f488ce942b231d45 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 21:49:21 -0400 Subject: [PATCH 4/8] Reduce padding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Blaž Hrastnik --- helix-term/src/ui/editor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 08fce76ec972..8a34c5100a24 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -669,7 +669,7 @@ impl EditorView { .cursor(doc.text().slice(..)), ); right_side_text.0.push(Span { - content: Cow::Owned(format!(" {}:{} ", pos.row + 1, pos.col + 1)), // Convert to 1-indexing. + content: Cow::Owned(format!(" {}:{} ", pos.row + 1, pos.col + 1)), // Convert to 1-indexing. style: base_style, }); From 7ab65eb9585bdb9b7bbd85730deb65e6f45d3859 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 22:02:19 -0400 Subject: [PATCH 5/8] Use `Span::styled` --- helix-term/src/ui/editor.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 8a34c5100a24..77b999d8c0b4 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -627,26 +627,25 @@ impl EditorView { bg: base_style.bg, ..style }; - right_side_text.0.push(Span { - content: Cow::Borrowed("●"), - style, - }); - right_side_text.0.push(Span { - content: Cow::Owned(format!(" {} ", count)), - style: base_style, - }); + right_side_text + .0 + .push(Span::styled(Cow::Borrowed("●"), style)); + right_side_text.0.push(Span::styled( + Cow::Owned(format!(" {} ", count)), + base_style, + )); } // Selections let sels_count = doc.selection(view.id).len(); - right_side_text.0.push(Span { - content: Cow::Owned(format!( + right_side_text.0.push(Span::styled( + Cow::Owned(format!( " {} sel{} ", sels_count, if sels_count == 1 { "" } else { "s" } )), - style: base_style, - }); + base_style, + )); // let indent_info = match doc.indent_style { // IndentStyle::Tabs => "tabs", @@ -668,10 +667,10 @@ impl EditorView { .primary() .cursor(doc.text().slice(..)), ); - right_side_text.0.push(Span { - content: Cow::Owned(format!(" {}:{} ", pos.row + 1, pos.col + 1)), // Convert to 1-indexing. - style: base_style, - }); + right_side_text.0.push(Span::styled( + Cow::Owned(format!(" {}:{} ", pos.row + 1, pos.col + 1)), // Convert to 1-indexing. + base_style, + )); // Render to the statusline. surface.set_spans( From 123f4b056dacee2d39c489d526122b10b6ca70cc Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 22:02:37 -0400 Subject: [PATCH 6/8] Reduce padding --- helix-term/src/ui/editor.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 77b999d8c0b4..22ef45327f24 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -630,10 +630,9 @@ impl EditorView { right_side_text .0 .push(Span::styled(Cow::Borrowed("●"), style)); - right_side_text.0.push(Span::styled( - Cow::Owned(format!(" {} ", count)), - base_style, - )); + right_side_text + .0 + .push(Span::styled(Cow::Owned(format!(" {} ", count)), base_style)); } // Selections From 397d9547af3cbed77394b75aba8ea4ba7ea8f8f9 Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 22:04:01 -0400 Subject: [PATCH 7/8] Use `Style::patch` --- helix-term/src/ui/editor.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 22ef45327f24..e2250ef13b96 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -623,10 +623,7 @@ impl EditorView { if count == 0 { continue; } - let style = Style { - bg: base_style.bg, - ..style - }; + let style = base_style.patch(style); right_side_text .0 .push(Span::styled(Cow::Borrowed("●"), style)); From 7c1baf55a738a8b30372842ffc01b7411b481dbb Mon Sep 17 00:00:00 2001 From: Omnikar Date: Tue, 26 Oct 2021 22:11:14 -0400 Subject: [PATCH 8/8] Remove unnecessary `Cow` creation --- helix-term/src/ui/editor.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index e2250ef13b96..bf316ee36dc5 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -624,22 +624,20 @@ impl EditorView { continue; } let style = base_style.patch(style); + right_side_text.0.push(Span::styled("●", style)); right_side_text .0 - .push(Span::styled(Cow::Borrowed("●"), style)); - right_side_text - .0 - .push(Span::styled(Cow::Owned(format!(" {} ", count)), base_style)); + .push(Span::styled(format!(" {} ", count), base_style)); } // Selections let sels_count = doc.selection(view.id).len(); right_side_text.0.push(Span::styled( - Cow::Owned(format!( + format!( " {} sel{} ", sels_count, if sels_count == 1 { "" } else { "s" } - )), + ), base_style, )); @@ -664,7 +662,7 @@ impl EditorView { .cursor(doc.text().slice(..)), ); right_side_text.0.push(Span::styled( - Cow::Owned(format!(" {}:{} ", pos.row + 1, pos.col + 1)), // Convert to 1-indexing. + format!(" {}:{} ", pos.row + 1, pos.col + 1), // Convert to 1-indexing. base_style, ));