1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
use std::collections::BTreeMap;
use zellij_tile::{prelude::TabInfo, shim::switch_tab_to};
use crate::{config::ZellijState, render::FormattedPart};
use super::widget::Widget;
pub struct TabsWidget {
active_tab_format: Vec<FormattedPart>,
active_tab_fullscreen_format: Vec<FormattedPart>,
active_tab_sync_format: Vec<FormattedPart>,
normal_tab_format: Vec<FormattedPart>,
normal_tab_fullscreen_format: Vec<FormattedPart>,
normal_tab_sync_format: Vec<FormattedPart>,
}
impl TabsWidget {
pub fn new(config: BTreeMap<String, String>) -> Self {
let mut normal_tab_format: Vec<FormattedPart> = Vec::new();
if let Some(form) = config.get("tab_normal") {
normal_tab_format = FormattedPart::multiple_from_format_string(form.to_string());
}
let normal_tab_fullscreen_format = match config.get("tab_normal_fullscreen") {
Some(form) => FormattedPart::multiple_from_format_string(form.to_string()),
None => normal_tab_format.clone(),
};
let normal_tab_sync_format = match config.get("tab_normal_sync") {
Some(form) => FormattedPart::multiple_from_format_string(form.to_string()),
None => normal_tab_format.clone(),
};
let mut active_tab_format = normal_tab_format.clone();
if let Some(form) = config.get("tab_active") {
active_tab_format = FormattedPart::multiple_from_format_string(form.to_string());
}
let active_tab_fullscreen_format = match config.get("tab_active_fullscreen") {
Some(form) => FormattedPart::multiple_from_format_string(form.to_string()),
None => active_tab_format.clone(),
};
let active_tab_sync_format = match config.get("tab_active_sync") {
Some(form) => FormattedPart::multiple_from_format_string(form.to_string()),
None => active_tab_format.clone(),
};
Self {
normal_tab_format,
normal_tab_fullscreen_format,
normal_tab_sync_format,
active_tab_format,
active_tab_fullscreen_format,
active_tab_sync_format,
}
}
}
impl Widget for TabsWidget {
fn process(&self, _name: &str, state: &ZellijState) -> String {
let mut output = "".to_string();
for tab in &state.tabs {
let content = self.render_tab(tab);
output = format!("{}{}", output, content);
}
output
}
fn process_click(&self, state: &ZellijState, pos: usize) {
let mut output = "".to_string();
let mut offset = 0;
let mut index = 1;
for tab in &state.tabs {
let content = strip_ansi_escapes::strip_str(self.render_tab(tab));
if pos > offset && pos < offset + content.chars().count() {
switch_tab_to(index);
break;
}
index += 1;
offset += content.chars().count();
output = format!("{}{}", output, content);
}
}
}
impl TabsWidget {
fn select_format(&self, info: TabInfo) -> Vec<FormattedPart> {
if info.active && info.is_fullscreen_active {
return self.active_tab_fullscreen_format.clone();
}
if info.active && info.is_sync_panes_active {
return self.active_tab_sync_format.clone();
}
if info.active {
return self.active_tab_format.clone();
}
if info.is_fullscreen_active {
return self.normal_tab_fullscreen_format.clone();
}
if info.is_sync_panes_active {
return self.normal_tab_sync_format.clone();
}
self.normal_tab_format.clone()
}
fn render_tab(&self, tab: &TabInfo) -> String {
let formatters = self.select_format(tab.clone());
let mut output = "".to_string();
for f in formatters.iter() {
let mut content = f.content.clone();
if content.contains("{name}") {
content = content.replace("{name}", tab.name.as_str());
}
if content.contains("{index}") {
content = content.replace("{index}", (tab.position + 1).to_string().as_str());
}
output = format!("{}{}", output, f.format_string(&content));
}
output.to_string()
}
}
|