use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)]
pub enum StatusLineSection {
Left,
Right,
Center,
}
pub struct StatusLineItem {
pub text: String,
pub section: StatusLineSection,
pub priority: i32,
pub color: Option<String>,
}
impl StatusLineItem {
pub fn new(text: &str, section: StatusLineSection) -> Self {
Self {
text: text.to_string(),
section,
priority: 0,
color: None,
}
}
pub fn with_priority(mut self, priority: i32) -> Self {
self.priority = priority;
self
}
pub fn with_color(mut self, color: &str) -> Self {
self.color = Some(color.to_string());
self
}
}
pub struct StatusLine {
pub items: Vec<StatusLineItem>,
}
impl StatusLine {
pub fn new() -> Self {
Self { items: Vec::new() }
}
pub fn add_item(&mut self, item: StatusLineItem) {
self.items.push(item);
}
pub fn render(&self) -> String {
let mut left: Vec<&StatusLineItem> = self
.items
.iter()
.filter(|i| i.section == StatusLineSection::Left)
.collect();
let mut right: Vec<&StatusLineItem> = self
.items
.iter()
.filter(|i| i.section == StatusLineSection::Right)
.collect();
let mut center: Vec<&StatusLineItem> = self
.items
.iter()
.filter(|i| i.section == StatusLineSection::Center)
.collect();
left.sort_by_key(|i| i.priority);
right.sort_by_key(|i| i.priority);
center.sort_by_key(|i| i.priority);
let left_text: String = left
.iter()
.map(|i| i.text.as_str())
.collect::<Vec<&str>>()
.join(" ");
let center_text: String = center
.iter()
.map(|i| i.text.as_str())
.collect::<Vec<&str>>()
.join(" ");
let right_text: String = right
.iter()
.map(|i| i.text.as_str())
.collect::<Vec<&str>>()
.join(" ");
format!("{} | {} | {}", left_text, center_text, right_text)
}
}
impl Default for StatusLine {
fn default() -> Self {
Self::new()
}
}