Skip to content

Commit

Permalink
Spans::push now takes Span
Browse files Browse the repository at this point in the history
  • Loading branch information
hamrik committed Jul 14, 2023
1 parent fb47ef2 commit 51dedf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
24 changes: 12 additions & 12 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,16 @@ fn render_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
let mut spans = Spans::default();

if warnings > 0 {
spans.push("●", context.editor.theme.get("warning"));
spans.push(
spans.push(Span::styled("●", context.editor.theme.get("warning")));
spans.push(Span::styled(
format!(" {}{}", warnings, if errors > 0 { " " } else { "" }),
context.base_style,
);
));
}

if errors > 0 {
spans.push("●", context.editor.theme.get("error"));
spans.push(format!(" {}", errors), context.base_style);
spans.push(Span::styled("●", context.editor.theme.get("error")));
spans.push(Span::styled(format!(" {}", errors), context.base_style));
}

spans
Expand All @@ -246,19 +246,19 @@ fn render_workspace_diagnostics<'a>(context: &RenderContext) -> Spans<'a> {
let mut spans = Spans::default();

if warnings > 0 || errors > 0 {
spans.push("W", context.base_style);
spans.push(Span::styled("W", context.base_style));
}

if warnings > 0 {
spans.push(" ", context.base_style);
spans.push("●", context.editor.theme.get("warning"));
spans.push(format!(" {}", warnings), context.base_style);
spans.push(Span::styled(" ", context.base_style));
spans.push(Span::styled("●", context.editor.theme.get("warning")));
spans.push(Span::styled(format!(" {}", warnings), context.base_style));
}

if errors > 0 {
spans.push(" ", context.base_style);
spans.push("●", context.editor.theme.get("error"));
spans.push(format!(" {}", errors), context.base_style);
spans.push(Span::styled(" ", context.base_style));
spans.push(Span::styled("●", context.editor.theme.get("error")));
spans.push(Span::styled(format!(" {}", errors), context.base_style));
}

spans
Expand Down
35 changes: 16 additions & 19 deletions helix-tui/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,12 @@ impl<'a> Spans<'a> {
/// ```rust
/// # use helix_tui::text::{Span, Spans};
/// # use helix_view::graphics::{Color, Style};
/// let mut spans = Spans::new();
/// spans.push("Hello!", Style::default());
/// let mut spans = Spans::default();
/// spans.push(Span::styled("Hello!", Style::default()));
/// assert_eq!("Hello!", spans.0[0].content);
/// ```
pub fn push<T>(&mut self, string: T, style: Style)
where
T: Into<Cow<'a, str>>,
{
self.0.push(Span::styled(string, style));
pub fn push(&mut self, span: Span<'a>) {
self.0.push(span);
}

/// Intersperses `sep` between items of the given iterator then returns the concatenated Spans.
Expand All @@ -258,26 +255,26 @@ impl<'a> Spans<'a> {
/// # use helix_view::graphics::Style;
/// let spans = Spans::join(
/// vec![
/// Spans::single("Hello", Style::default()),
/// Spans::single("World!", Style::default()),
/// Span::styled("Hello", Style::default()).into(),
/// Span::styled("World!", Style::default()).into(),
/// ].into_iter(),
/// &Spans::single(" ", Style::default()),
/// &Span::raw(" ").into(),
/// );
/// assert_eq!("Hello World!", String::from(spans));
/// ```
pub fn join<S>(mut spanses: S, sep: &Spans<'a>) -> Spans<'a>
pub fn join<S>(mut spans_iter: S, sep: &Spans<'a>) -> Spans<'a>
where
S: Iterator<Item = Spans<'a>>,
{
let mut spans: Vec<Span> = Vec::new();
if let Some(head) = spanses.next() {
spans.extend(head.0);
let mut new_spans: Vec<Span> = Vec::new();
if let Some(head) = spans_iter.next() {
new_spans.extend(head.0);
}
spanses.for_each(|s| {
spans.extend(sep.0.clone());
spans.extend(s.0);
});
Spans(spans)
for s in spans_iter {
new_spans.extend(sep.0.clone());
new_spans.extend(s.0);
}
Spans(new_spans)
}
}

Expand Down

0 comments on commit 51dedf3

Please sign in to comment.