diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-08-31 16:45:19 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-08-31 16:45:19 +0200 |
| commit | ba68857410df33f424f3670149ef5e71b2e90b9f (patch) | |
| tree | 627a3c226116b3ae3f5f76a31766ec1f8dd94bdc | |
| parent | feat: initial commit (diff) | |
| download | sl0pstatus-ba68857410df33f424f3670149ef5e71b2e90b9f.tar.gz sl0pstatus-ba68857410df33f424f3670149ef5e71b2e90b9f.tar.xz sl0pstatus-ba68857410df33f424f3670149ef5e71b2e90b9f.zip | |
feat: implement format string renderer
| -rw-r--r-- | plugin-dev-workspace.kdl | 10 | ||||
| -rw-r--r-- | src/config.rs | 48 | ||||
| -rw-r--r-- | src/main.rs | 61 |
3 files changed, 57 insertions, 62 deletions
diff --git a/plugin-dev-workspace.kdl b/plugin-dev-workspace.kdl index ea46f46..d496c28 100644 --- a/plugin-dev-workspace.kdl +++ b/plugin-dev-workspace.kdl @@ -11,13 +11,13 @@ layout { } pane expanded=true { plugin location="file:target/wasm32-wasi/debug/zjstatus.wasm" { - format "#[fg=CYAN] {mode} #[fg=WHITE] :: {tab} {space} #[fg=ORANGE,bg=GREEN] {date}" + format "#[fg=0,bg=3,bold,italic] {mode} #[fg=255] :: {tabs} {space} #[fg=255,bg=0] {date}" - mode_normal_format "#[fg=BLUE,bg=NONE]>> normal" - mode_tmux_format "#[fg=ORANGE,bg=NONE]>> tmux" + mode_normal_format "#[fg=255,bg=0]>> normal" + mode_tmux_format "#[fg=255,bg=0]>> tmux" - tab_format "#[fg=GRAY,bg=NONE] {name}" - tab_active_format "#[fg=WHITE,bg=NONE,bold] {name}" + tab_format "#[fg=255,bg=0] {name}" + tab_active_format "#[fg=255,bg=0,bold] {name}" } } } diff --git a/src/config.rs b/src/config.rs index 2529bf1..3316d29 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,20 +1,35 @@ use std::collections::BTreeMap; +use ansi_term::{Colour, Colour::Fixed}; + #[derive(Default)] pub struct ModuleConfig { pub enabled_modules: Vec<String>, pub formatted_parts: Vec<FormattedPart>, } +#[derive(Clone, Debug)] pub struct FormattedPart { pub order: u8, - pub fg: u8, - pub bg: u8, + pub fg: Option<Colour>, + pub bg: Option<Colour>, pub bold: bool, pub italic: bool, pub content: String, } +fn parse_color(color: &str) -> Option<Colour> { + if color.starts_with("#") { + return Some(Fixed(1)); + } + + if let Ok(result) = color.parse::<u8>() { + return Some(Fixed(result)); + } + + None +} + impl FormattedPart { pub fn from_format_string(format: String) -> Self { let mut result = FormattedPart::default(); @@ -33,27 +48,20 @@ impl FormattedPart { let parts = format_content_split[0].split(","); for part in parts { if part.starts_with("fg=") { - // TODO: better error handling - result.fg = part - .strip_prefix("fg=") - .unwrap() - .to_string() - .parse::<u8>() - .unwrap(); + result.fg = parse_color(part.strip_prefix("fg=").unwrap()); } if part.starts_with("bg=") { - // TODO: better error handling - result.bg = part - .strip_prefix("bg=") - .unwrap() - .to_string() - .parse::<u8>() - .unwrap(); + result.bg = parse_color(part.strip_prefix("bg=").unwrap()); } - result.bold = part.eq("bold"); - result.italic = part.eq("italic"); + if part.eq("bold") { + result.bold = true; + } + + if part.eq("italic") { + result.italic = true; + } } result @@ -64,8 +72,8 @@ impl Default for FormattedPart { fn default() -> Self { Self { order: 0, - fg: 255, - bg: 0, + fg: None, + bg: None, bold: false, italic: false, content: "".to_string(), diff --git a/src/main.rs b/src/main.rs index e4e3f30..6b76cfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use ansi_term::{Colour::Fixed, Style}; +use ansi_term::Style; +use config::FormattedPart; use zellij_tile::prelude::*; use std::{ @@ -54,47 +55,33 @@ impl ZellijPlugin for State { } fn render(&mut self, rows: usize, cols: usize) { - // TODO: render blocks in correct order and print them + let mut output = "".to_string(); - let colored_rows = color(CYAN, &rows.to_string()); - let colored_cols = color_bold(CYAN, &cols.to_string()); - println!(""); - println!("I have {} rows and {} columns", colored_rows, colored_cols); - println!(""); - println!( - "{} {:#?}", - color_bold( - GREEN, - "I was started with the following user configuration:" - ), - self.userspace_configuration - ); - println!(""); - println!("{}", color_bold(GREEN, "Modes:")); - for (mode, count) in &self.mode_log { - let count = color_bold(ORANGE, &count.to_string()); - println!("{} -> Changed {} times", mode, count); + for part in self.module_config.formatted_parts.to_vec() { + // TODO: render widgets + + output = format!( + "{}{}", + output, + format!("{}", color(part.clone(), part.content.as_str())) + ); } - println!(""); - let current_tabs = color_bold(GREEN, "Current Tabs:"); - let comma = color_bold(ORANGE, ", "); - println!("{} {}", current_tabs, self.tabs.join(&comma)); + + println!("{}", output); } } -pub const CYAN: u8 = 51; -pub const GRAY_LIGHT: u8 = 238; -pub const GRAY_DARK: u8 = 245; -pub const WHITE: u8 = 15; -pub const BLACK: u8 = 16; -pub const RED: u8 = 124; -pub const GREEN: u8 = 154; -pub const ORANGE: u8 = 166; +fn color(part: FormattedPart, text: &str) -> String { + let mut style = match part.fg { + Some(color) => Style::new().fg(color), + None => Style::new(), + }; -fn color(color: u8, text: &str) -> String { - format!("{}", Style::new().fg(Fixed(color)).paint(text)) -} + style.background = part.bg; + style.is_italic = part.italic; + style.is_bold = part.bold; + + let style = style.paint(text); -fn color_bold(color: u8, text: &str) -> String { - format!("{}", Style::new().fg(Fixed(color)).bold().paint(text)) + format!("{}", style) } |
