diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-09-02 14:15:04 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-09-02 14:15:04 +0200 |
| commit | 66afd6d4de80099c298466fffd1a8c3044a13247 (patch) | |
| tree | 93980c3b8821f1993a867e71ae1bf79b5581b04e /src/render.rs | |
| parent | fix: hide frame for single panes with multiple tabs (diff) | |
| download | sl0pstatus-66afd6d4de80099c298466fffd1a8c3044a13247.tar.gz sl0pstatus-66afd6d4de80099c298466fffd1a8c3044a13247.tar.xz sl0pstatus-66afd6d4de80099c298466fffd1a8c3044a13247.zip | |
chore: refactor renderer
Diffstat (limited to '')
| -rw-r--r-- | src/render.rs | 141 |
1 files changed, 98 insertions, 43 deletions
diff --git a/src/render.rs b/src/render.rs index 279b458..561e2ab 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,66 +1,121 @@ use std::{collections::BTreeMap, sync::Arc}; -use ansi_term::Style; +use ansi_term::{Colour, Colour::Fixed, Colour::RGB, Style}; -use crate::{config::FormattedPart, widgets::widget::Widget, ZellijState}; +use crate::{widgets::widget::Widget, ZellijState}; -pub fn formatting(part: FormattedPart, text: String) -> String { - let mut style = match part.fg { - Some(color) => Style::new().fg(color), - None => Style::new(), - }; +#[derive(Clone, Debug, PartialEq)] +pub struct FormattedPart { + pub fg: Option<Colour>, + pub bg: Option<Colour>, + pub bold: bool, + pub italic: bool, + pub content: String, +} - style.background = part.bg; - style.is_italic = part.italic; - style.is_bold = part.bold; +impl FormattedPart { + pub fn from_format_string(format: String) -> Self { + let mut format = format; + if format.starts_with("#[") { + format = format.strip_prefix("#[").unwrap().to_string(); + } + let mut result = FormattedPart::default(); - let style = style.paint(text); + let format_content_split = format.split(']'); - format!("{}", style) -} + if format_content_split.clone().count() == 1 { + result.content = format; -pub fn widgets_and_formatting( - part: FormattedPart, - widgets: BTreeMap<String, Arc<dyn Widget>>, - state: ZellijState, -) -> String { - let mut output = part.content.clone(); + return result; + } - if output.contains("{datetime}") { - let result = match widgets.get("datetime") { - Some(widget) => widget.process(state.clone()), - None => "Use of uninitialized widget".to_string(), - }; + let format_content_split = format_content_split.collect::<Vec<&str>>(); + result.content = format_content_split[1].to_string(); + + let parts = format_content_split[0].split(','); + for part in parts { + if part.starts_with("fg=") { + result.fg = parse_color(part.strip_prefix("fg=").unwrap()); + } + + if part.starts_with("bg=") { + result.bg = parse_color(part.strip_prefix("bg=").unwrap()); + } - output = output.replace("{datetime}", &result); + if part.eq("bold") { + result.bold = true; + } + + if part.eq("italic") { + result.italic = true; + } + } + + result } - if output.contains("{mode}") { - let result = match widgets.get("mode") { - Some(widget) => widget.process(state.clone()), - None => "Use of uninitialized widget".to_string(), + pub fn format_string(&self, text: String) -> String { + let mut style = match self.fg { + Some(color) => Style::new().fg(color), + None => Style::new(), }; - output = output.replace("{mode}", &result); + style.background = self.bg; + style.is_italic = self.italic; + style.is_bold = self.bold; + + let style = style.paint(text); + + format!("{}", style) } - if output.contains("{session}") { - let result = match widgets.get("session") { - Some(widget) => widget.process(state.clone()), - None => "Use of uninitialized widget".to_string(), - }; + pub fn format_string_with_widgets( + &self, + widgets: BTreeMap<String, Arc<dyn Widget>>, + state: ZellijState, + ) -> String { + let mut output = self.content.clone(); - output = output.replace("{session}", &result); + for key in widgets.keys() { + let token = format!("{{{key}}}"); + if !output.contains(token.as_str()) { + continue; + } + + let result = match widgets.get(key) { + Some(widget) => widget.process(state.clone()), + None => "Use of uninitialized widget".to_string(), + }; + + output = output.replace(token.as_str(), &result); + } + + self.format_string(output) } +} - if output.contains("{tabs}") { - let result = match widgets.get("tabs") { - Some(widget) => widget.process(state), - None => "Use of uninitialized widget".to_string(), - }; +impl Default for FormattedPart { + fn default() -> Self { + Self { + fg: None, + bg: None, + bold: false, + italic: false, + content: "".to_string(), + } + } +} + +fn parse_color(color: &str) -> Option<Colour> { + if color.starts_with('#') { + let rgb = hex_rgb::convert_hexcode_to_rgb(color.to_string()).unwrap(); + + return Some(RGB(rgb.red, rgb.green, rgb.blue)); + } - output = output.replace("{tabs}", &result); + if let Ok(result) = color.parse::<u8>() { + return Some(Fixed(result)); } - formatting(part, output) + None } |
