Skip to content

Commit

Permalink
Add spacing between statusline elements
Browse files Browse the repository at this point in the history
Intersperses a raw space between statusline
elements. Uses a custom `IntersperseIter` iterator
because `intersperse()` has not landed in stable
Rust as of this commit.

This implementation currently resides in
`statusline.rs` as it is the only file that uses
it but it should probably be moved because it is
general but also temporary.
  • Loading branch information
hamrik committed Aug 25, 2023
1 parent 3bba8a3 commit 2680143
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,59 @@ pub struct RenderBuffer<'a> {
pub right: Spans<'a>,
}

struct IntersperseIter<T, I>
where
T: Clone,
I: Iterator<Item = T>,
{
iter: I,
spacer: T,
emit_spacer: bool,
next_item: Option<T>,
}

impl<T, I> Iterator for IntersperseIter<T, I>
where
T: Clone,
I: Iterator<Item = T>,
{
type Item = T;

fn next(&mut self) -> Option<T> {
if self.next_item.is_none() {
Option::None
} else if self.emit_spacer {
self.emit_spacer = false;
Option::Some(self.spacer.clone())
} else {
self.emit_spacer = true;
let curr_item = self.next_item.clone();
self.next_item = self.iter.next();
curr_item
}
}
}

trait Intersperse<T>
where
T: Clone,
Self: Iterator<Item = T> + Sized,
{
fn interspersed(self, elem: T) -> IntersperseIter<T, Self>;
}

impl<T: Clone, I: Iterator<Item = T>> Intersperse<T> for I {
fn interspersed(mut self, elem: T) -> IntersperseIter<T, Self> {
let first_item = self.next();
IntersperseIter {
iter: self,
spacer: elem,
emit_spacer: false,
next_item: first_item,
}
}
}

pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface) {
let base_style = if context.focused {
context.editor.theme.get("ui.statusline")
Expand All @@ -67,6 +120,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface
.iter()
.map(|element_id| get_render_function(*element_id))
.flat_map(|render| render(context).0)
.interspersed(Span::raw(" "))
.collect::<Vec<Span>>()
.into();

Expand All @@ -84,6 +138,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface
.iter()
.map(|element_id| get_render_function(*element_id))
.flat_map(|render| render(context).0)
.interspersed(Span::raw(" "))
.collect::<Vec<Span>>()
.into();

Expand All @@ -104,6 +159,7 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface
.iter()
.map(|element_id| get_render_function(*element_id))
.flat_map(|render| render(context).0)
.interspersed(Span::raw(" "))
.collect::<Vec<Span>>()
.into();

Expand Down

0 comments on commit 2680143

Please sign in to comment.