From 620786260b79c086ac06a0a17086499ace9d94f5 Mon Sep 17 00:00:00 2001 From: Daniel Jankowski Date: Wed, 17 Apr 2024 13:50:00 +0200 Subject: [PATCH] feat: add formats for swap layout --- plugin-dev-workspace.kdl | 5 +++- src/widgets/swap_layout.rs | 47 ++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/plugin-dev-workspace.kdl b/plugin-dev-workspace.kdl index 76decc9..a5b4fee 100644 --- a/plugin-dev-workspace.kdl +++ b/plugin-dev-workspace.kdl @@ -7,7 +7,7 @@ layout { plugin location="file:target/wasm32-wasi/debug/zjstatus.wasm" { format_left "{mode}#[fg=#89B4FA,bg=#181825,bold] {session} {tabs}" format_center "{command_0} {command_1} {command_git_branch} {command_3}" - format_right "{notifications}{datetime}" + format_right "{notifications}{swap_layout}{datetime}" format_space "#[bg=#181825]" notification_format_unread "#[fg=#89B4FA,bg=#181825,blink]  #[fg=#89B4FA,bg=#181825] {message} " @@ -19,6 +19,9 @@ layout { border_format "#[fg=#6C7086]{char}" border_position "top" + swap_layout_format "#[bg=blue,fg=#000000] {name} #[bg=red,fg=black] foo " + swap_layout_hide_if_empty "false" + hide_frame_for_single_pane "true" mode_normal "#[bg=blue] #[bg=yellow] " diff --git a/src/widgets/swap_layout.rs b/src/widgets/swap_layout.rs index f744e38..7d36b14 100644 --- a/src/widgets/swap_layout.rs +++ b/src/widgets/swap_layout.rs @@ -1,13 +1,30 @@ use std::collections::BTreeMap; use zellij_tile::shim::next_swap_layout; +use crate::render::FormattedPart; use crate::{config::ZellijState, widgets::widget::Widget}; -pub struct SwapLayoutWidget {} +pub struct SwapLayoutWidget { + format: Vec, + hide_if_empty: bool, +} impl SwapLayoutWidget { - pub fn new(_config: &BTreeMap) -> Self { - Self {} + pub fn new(config: &BTreeMap) -> Self { + let mut format: Vec = Vec::new(); + if let Some(form) = config.get("swap_layout_format") { + format = FormattedPart::multiple_from_format_string(form); + } + + let hide_if_empty = match config.get("swap_layout_hide_if_empty") { + Some(hide_if_empty) => hide_if_empty == "true", + None => false, + }; + + Self { + format, + hide_if_empty, + } } } @@ -21,10 +38,32 @@ impl Widget for SwapLayoutWidget { let active_tab = active_tab.unwrap(); - match active_tab.active_swap_layout_name.clone() { + let name = match active_tab.active_swap_layout_name.clone() { Some(n) => n, None => "".to_owned(), + }; + + if name.is_empty() && self.hide_if_empty { + return "".to_owned(); } + + if self.format.is_empty() { + return name; + } + + let mut output = "".to_owned(); + + for f in &self.format { + let mut content = f.content.clone(); + + if content.contains("{name}") { + content = content.replace("{name}", &name); + } + + output = format!("{}{}", output, f.format_string(&content)); + } + + output } fn process_click(&self, _state: &ZellijState, _pos: usize) {