Skip to content

Commit

Permalink
feat(tabs): format for renaming tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
dj95 committed May 25, 2024
1 parent 61b35de commit 7fb1e83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions plugin-dev-workspace.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ layout {
mode_default_to_mode "tmux"

tab_normal "#[fg=#6C7086,bg=#181825] {index} {name} {floating_indicator} "
tab_rename "#[fg=#eba0ac,bg=#181825] {index} {name} {floating_indicator} "
tab_normal_fullscreen "#[fg=#6C7086,bg=#181825] {index} {name} [] "
tab_normal_sync "#[fg=#6C7086,bg=#181825] {index} {name} <> "
tab_active "#[fg=#9399B2,bg=#181825,bold,italic] {index} {name} {floating_total_count}{floating_indicator}{sync_indicator}{fullscreen_indicator}"
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct ZellijState {
pub enum UpdateEventMask {
Always = 0b10000000,
Mode = 0b00000001,
Tab = 0b00000010,
Tab = 0b00000011,
Command = 0b00000100,
Session = 0b00001000,
None = 0b00000000,
Expand Down
33 changes: 26 additions & 7 deletions src/widgets/tabs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;

use zellij_tile::{
prelude::{PaneInfo, PaneManifest, TabInfo},
prelude::{InputMode, ModeInfo, PaneInfo, PaneManifest, TabInfo},
shim::switch_tab_to,
};

Expand All @@ -16,6 +16,7 @@ pub struct TabsWidget {
normal_tab_format: Vec<FormattedPart>,
normal_tab_fullscreen_format: Vec<FormattedPart>,
normal_tab_sync_format: Vec<FormattedPart>,
rename_tab_format: Vec<FormattedPart>,
separator: Option<FormattedPart>,
fullscreen_indicator: Option<String>,
floating_indicator: Option<String>,
Expand Down Expand Up @@ -54,6 +55,11 @@ impl TabsWidget {
None => active_tab_format.clone(),
};

let rename_tab_format = match config.get("tab_rename") {
Some(form) => FormattedPart::multiple_from_format_string(form),
None => active_tab_format.clone(),
};

let separator = config
.get("tab_separator")
.map(|s| FormattedPart::from_format_string(s));
Expand All @@ -65,6 +71,7 @@ impl TabsWidget {
active_tab_format,
active_tab_fullscreen_format,
active_tab_sync_format,
rename_tab_format,
separator,
floating_indicator: config.get("tab_floating_indicator").cloned(),
sync_indicator: config.get("tab_sync_indicator").cloned(),
Expand All @@ -79,7 +86,7 @@ impl Widget for TabsWidget {
let mut counter = 0;

for tab in &state.tabs {
let content = self.render_tab(tab, &state.panes);
let content = self.render_tab(tab, &state.panes, &state.mode);
counter += 1;

output = format!("{}{}", output, content);
Expand All @@ -101,7 +108,7 @@ impl Widget for TabsWidget {
for tab in &state.tabs {
counter += 1;

let mut rendered_content = self.render_tab(tab, &state.panes);
let mut rendered_content = self.render_tab(tab, &state.panes, &state.mode);

if counter < state.tabs.len() {
if let Some(sep) = &self.separator {
Expand All @@ -125,7 +132,11 @@ impl Widget for TabsWidget {
}

impl TabsWidget {
fn select_format(&self, info: &TabInfo) -> &Vec<FormattedPart> {
fn select_format(&self, info: &TabInfo, mode: &ModeInfo) -> &Vec<FormattedPart> {
if info.active && mode.mode == InputMode::RenameTab {
return &self.rename_tab_format;
}

if info.active && info.is_fullscreen_active {
return &self.active_tab_fullscreen_format;
}
Expand All @@ -149,15 +160,23 @@ impl TabsWidget {
&self.normal_tab_format
}

fn render_tab(&self, tab: &TabInfo, panes: &PaneManifest) -> String {
let formatters = self.select_format(tab);
fn render_tab(&self, tab: &TabInfo, panes: &PaneManifest, mode: &ModeInfo) -> String {
let formatters = self.select_format(tab, mode);
let mut output = "".to_owned();

for f in formatters.iter() {
let mut content = f.content.clone();

let tab_name = match mode.mode {
InputMode::RenameTab => match tab.name.is_empty() {
true => "Enter name...",
false => tab.name.as_str(),
},
_name => tab.name.as_str(),
};

if content.contains("{name}") {
content = content.replace("{name}", tab.name.as_str());
content = content.replace("{name}", tab_name);
}

if content.contains("{index}") {
Expand Down

0 comments on commit 7fb1e83

Please sign in to comment.