diff --git a/plugin-dev-workspace.kdl b/plugin-dev-workspace.kdl index 58131cc..8ebb6c0 100644 --- a/plugin-dev-workspace.kdl +++ b/plugin-dev-workspace.kdl @@ -32,18 +32,18 @@ layout { tab_fullscreen_indicator "FS" command_0_command "echo \"平仮名, ひらがな 📦\"" - command_0_format "#[fg=blue,bg=black] {exit_code} {stdout} " + command_0_format "#[fg=colour80] {exit_code} {stdout} " command_0_interval "1" command_1_command "date" - command_1_format "#[fg=blue,bg=black] {exit_code} {stdout} " + command_1_format "#[fg=blue,reverse,dashed-underscore,bg=default,us=red,blink,dim,strikethrough] {exit_code} {stdout} " command_1_interval "1" command_git_branch_command "git rev-parse --abbrev-ref HEAD" - command_git_branch_format "#[fg=red,bg=black] {stdout} " + command_git_branch_format "#[fg=red] {stdout} " command_git_branch_interval "2" - command_3_command "echo -e \"#[fg=red,bg=#000000,italic,bold] foo #[bg=red,fg=#000000,italic] bar \"" + command_3_command "echo -e \"#[italic,bold] foo #[dim,bold,italic] bar \"" command_3_format "{stdout}" command_3_interval "10" command_3_rendermode "dynamic" diff --git a/src/render.rs b/src/render.rs index cef70d4..f91f9f2 100644 --- a/src/render.rs +++ b/src/render.rs @@ -19,8 +19,20 @@ lazy_static! { pub struct FormattedPart { pub fg: Option, pub bg: Option, + pub us: Option, + pub effects: anstyle::Effects, pub bold: bool, pub italic: bool, + pub underscore: bool, + pub reverse: bool, + pub blink: bool, + pub hidden: bool, + pub dimmed: bool, + pub strikethrough: bool, + pub double_underscore: bool, + pub curly_underscore: bool, + pub dotted_underscore: bool, + pub dashed_underscore: bool, pub content: String, pub cache_mask: u8, pub cached_content: String, @@ -86,31 +98,69 @@ impl FormattedPart { result.bg = parse_color(part.strip_prefix("bg=").unwrap()); } - if part.eq("bold") { - result.bold = true; + if part.starts_with("us=") { + result.us = parse_color(part.strip_prefix("us=").unwrap()); } - if part.eq("italic") { - result.italic = true; + if part.eq("reverse") { + result.reverse = true; } + + result.parse_and_set_effect(part); } result } + fn parse_and_set_effect(&mut self, part: &str) { + match part { + "bold" => { + self.effects |= anstyle::Effects::BOLD; + } + "italic" | "italics" => { + self.effects |= anstyle::Effects::ITALIC; + } + "underscore" => { + self.effects |= anstyle::Effects::UNDERLINE; + } + "blink" => { + self.effects |= anstyle::Effects::BLINK; + } + "hidden" => { + self.effects |= anstyle::Effects::HIDDEN; + } + "dim" => { + self.effects |= anstyle::Effects::DIMMED; + } + "strikethrough" => { + self.effects |= anstyle::Effects::STRIKETHROUGH; + } + "double-underscore" => { + self.effects |= anstyle::Effects::DOUBLE_UNDERLINE; + } + "curly-underscore" => { + self.effects |= anstyle::Effects::CURLY_UNDERLINE; + } + "dotted-underscore" => { + self.effects |= anstyle::Effects::DOTTED_UNDERLINE; + } + "dashed-underscore" => { + self.effects |= anstyle::Effects::DASHED_UNDERLINE; + } + "reverse" => { + self.effects |= anstyle::Effects::INVERT; + } + _ => {} + } + } + pub fn format_string(&self, text: &str) -> String { let mut style = Style::new(); style = style.fg_color(self.fg); style = style.bg_color(self.bg); - - if self.italic { - style = style.italic(); - } - - if self.bold { - style = style.bold(); - } + style = style.underline_color(self.us); + style = style.effects(self.effects); format!( "{}{}{}{}", @@ -128,12 +178,12 @@ impl FormattedPart { state: &ZellijState, ) -> String { let skip_cache = self.cache_mask & UpdateEventMask::Always as u8 != 0; - + if !skip_cache && self.cache_mask & state.cache_mask == 0 && !self.cache.is_empty() { - tracing::debug!(msg = "hit", typ = "format_string", format = self.content); + tracing::debug!(msg = "hit", typ = "format_string", format = self.content); return self.cached_content.to_owned(); } - tracing::debug!(msg = "miss", typ = "format_string", format = self.content); + tracing::debug!(msg = "miss", typ = "format_string", format = self.content); let mut output = self.content.clone(); @@ -150,16 +200,16 @@ impl FormattedPart { let skip_widget_cache = widget_mask & UpdateEventMask::Always as u8 != 0; if !skip_widget_cache && widget_mask & state.cache_mask == 0 { if let Some(res) = self.cache.get(widget_key) { - tracing::debug!(msg = "hit", typ = "widget", widget = widget_key); + tracing::debug!(msg = "hit", typ = "widget", widget = widget_key); output = output.replace(match_name, res); continue; } } - + tracing::debug!( - msg = "miss", - typ = "widget", - widget = widget_key, + msg = "miss", + typ = "widget", + widget = widget_key, mask = widget_mask & state.cache_mask, skip_cache = skip_cache, ); @@ -186,8 +236,20 @@ impl Default for FormattedPart { Self { fg: None, bg: None, + us: None, + effects: anstyle::Effects::new(), bold: false, italic: false, + underscore: false, + reverse: false, + blink: false, + hidden: false, + dimmed: false, + strikethrough: false, + double_underscore: false, + curly_underscore: false, + dotted_underscore: false, + dashed_underscore: false, content: "".to_owned(), cache_mask: 0, cached_content: "".to_owned(), @@ -229,6 +291,7 @@ fn hex_to_rgb(s: &str) -> anyhow::Result> { convert = r#"{ (color.to_owned()) }"# )] fn parse_color(color: &str) -> Option { + let mut color = color; if color.starts_with('#') { let rgb = match hex_to_rgb(color.strip_prefix('#').unwrap()) { Ok(rgb) => rgb, @@ -253,6 +316,10 @@ fn parse_color(color: &str) -> Option { return Some(color.into()); } + if color.starts_with("colour") { + color = color.strip_prefix("colour").unwrap(); + } + if let Ok(result) = color.parse::() { return Some(Ansi256Color(result).into()); } @@ -270,6 +337,7 @@ fn color_by_name(color: &str) -> Option { "magenta" => Some(AnsiColor::Magenta), "cyan" => Some(AnsiColor::Cyan), "white" => Some(AnsiColor::White), + "default" => None, _ => None, } }