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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
use super::text_utils::{
get_last_modified_string, get_layout_display_info, truncate_with_ellipsis,
};
use crate::DisplayLayout;
use zellij_tile::prelude::*;
fn create_table_row(
name: &str,
metadata: Option<&LayoutMetadata>,
is_error: bool,
is_builtin: bool,
max_name_width: Option<usize>,
matched_indices: Option<&Vec<usize>>,
) -> Vec<Text> {
let display_name = if let Some(max_width) = max_name_width {
truncate_with_ellipsis(name, max_width)
} else {
name.to_string()
};
let name_text = if let Some(indices) = matched_indices {
// Create text with highlighted matched characters
let mut text = Text::new(&display_name);
// Apply base color to entire text
text = if is_error {
text.error_color_all()
} else {
text.color_all(1)
};
text = text.color_indices(3, indices.iter().cloned().collect());
text
} else {
// No matches, use original coloring
if is_error {
Text::new(&display_name).error_color_all()
} else {
Text::new(&display_name).color_all(1)
}
};
let last_modified_string = get_last_modified_string(metadata, is_builtin);
let last_modified = if is_builtin {
Text::new(&last_modified_string).color_all(0)
} else {
Text::new(&last_modified_string)
};
vec![name_text, last_modified]
}
fn apply_selection(mut row: Vec<Text>) -> Vec<Text> {
for cell in &mut row {
*cell = std::mem::take(cell).selected();
}
row
}
pub struct LayoutsTable {
display_layouts: Vec<DisplayLayout>,
selected_layout_index: usize,
hidden_above: usize,
hidden_below: usize,
matched_indices: Vec<Option<Vec<usize>>>,
}
impl LayoutsTable {
pub fn new(
display_layouts: Vec<DisplayLayout>,
selected_layout_index: usize,
hidden_above: usize,
hidden_below: usize,
) -> Self {
Self {
display_layouts,
selected_layout_index,
hidden_above,
hidden_below,
matched_indices: Vec::new(),
}
}
pub fn with_matched_indices(mut self, matched_indices: Vec<Option<Vec<usize>>>) -> Self {
self.matched_indices = matched_indices;
self
}
pub fn render(&self, x: usize, y: usize, _max_rows: usize, max_cols: usize) {
let mut table = match self.overflow_indicator(self.hidden_above) {
Some(overflow_indicator) => {
Table::new().add_styled_row(vec![Text::new(" "), overflow_indicator])
},
None => Table::new().add_row(vec![" ", " "]),
};
// Calculate the actual width needed for the "last modified" column
let max_last_modified_width = self
.display_layouts
.iter()
.map(|layout| {
let (_, metadata_opt) = get_layout_display_info(layout);
get_last_modified_string(metadata_opt, layout.is_builtin()).len()
})
.max()
.unwrap_or(1);
let table_padding = 2;
let max_name_width = if max_cols > max_last_modified_width + table_padding {
Some(max_cols.saturating_sub(max_last_modified_width + table_padding))
} else {
Some(max_cols.saturating_div(2))
};
for (i, layout) in self.display_layouts.iter().enumerate() {
let (name, metadata_opt) = get_layout_display_info(layout);
let matched_indices = self.matched_indices.get(i).and_then(|opt| opt.as_ref());
let mut row = create_table_row(
&name,
metadata_opt,
layout.is_error(),
layout.is_builtin(),
max_name_width,
matched_indices,
);
if i == self.selected_layout_index {
row = apply_selection(row);
}
table = table.add_styled_row(row);
}
if let Some(overflow_indicator) = self.overflow_indicator(self.hidden_below) {
table = table.add_styled_row(vec![Text::new(" "), overflow_indicator]);
}
print_table_with_coordinates(table, x, y, Some(max_cols), None);
}
fn overflow_indicator(&self, count: usize) -> Option<Text> {
if count > 0 {
Some(Text::new(format!("[+{}]", count)).color_all(2))
} else {
None
}
}
}
fn color_control_text(text: &str, keys: &[&str]) -> Text {
let mut colored_text = Text::new(text);
for key in keys {
colored_text = colored_text.color_substring(3, key)
}
colored_text
}
pub struct Controls {
retain_terminal_panes: bool,
retain_plugin_panes: bool,
apply_only_to_active_tab: bool,
show_more_options: bool,
typing_filter: bool,
filter_active: bool,
}
impl Controls {
pub fn new(
retain_terminal_panes: bool,
retain_plugin_panes: bool,
apply_only_to_active_tab: bool,
show_more_options: bool,
typing_filter: bool,
filter_active: bool,
) -> Self {
Self {
retain_terminal_panes,
retain_plugin_panes,
apply_only_to_active_tab,
show_more_options,
typing_filter,
filter_active,
}
}
fn get_typing_filter_controls(&self, max_cols: usize) -> (&str, &[&str]) {
let long_text =
"- <↑↓> Navigate, <Tab> Complete, <Enter> Open, <Alt+w> Apply, <Esc> Manage & New";
let short_text = "<↑↓>/<Tab>/<Enter>/<Alt+w>/<Esc> Nav/Complete/Open/Apply/Manage";
let minimum_text = "<↑↓>/<Tab>/<Enter>/<Alt+w> ...";
let text = if max_cols >= long_text.chars().count() {
long_text
} else if max_cols >= short_text.chars().count() {
short_text
} else {
minimum_text
};
(text, &["<↑↓>", "<Tab>", "<Enter>", "<Alt+w>", "<Esc>"])
}
fn get_filter_active_controls(&self, max_cols: usize) -> (&str, &[&str]) {
let long_text = "- <Enter> Open, <↓↑> Nav, <e> Edit, <r> Rename, <Del> Delete";
let short_text = "<Enter>/<↓↑>/<e>/<r>/<Del> Open/Nav/Edit/Rename/Del";
let minimum_text = "<Enter>/<↓↑>/<e>/<r>/<Del> ...";
let text = if max_cols >= long_text.chars().count() {
long_text
} else if max_cols >= short_text.chars().count() {
short_text
} else {
minimum_text
};
(text, &["<Enter>", "<↓↑>", "<e>", "<r>", "<Del>"])
}
fn get_default_controls(&self, max_cols: usize) -> (&str, &[&str]) {
let long_text = "- <Enter> Open, <↓↑> Nav, </> Search, <e> Edit, <r> Rename, <Del> Del";
let short_text = "<Enter>/<↓↑>/</>/<e>/<r>/<Del> Open/Nav/Search/Edit/Rename/Del";
let minimum_text = "<Enter>/<↓↑>/</>/<e>/<r>/<Del> ...";
let text = if max_cols >= long_text.chars().count() {
long_text
} else if max_cols >= short_text.chars().count() {
short_text
} else {
minimum_text
};
(text, &["<Enter>", "<↓↑>", "</>", "<e>", "<r>", "<Del>"])
}
fn get_basic_controls_text_and_keys(&self, max_cols: usize) -> (&str, &[&str]) {
if self.filter_active {
self.get_filter_active_controls(max_cols)
} else {
self.get_default_controls(max_cols)
}
}
fn get_override_text_and_keys(&self, max_cols: usize) -> (String, &[&str]) {
let toggle_word = if self.show_more_options {
"less"
} else {
"more"
};
let long_text = format!(
"- <Alt+w> Override Session Layout, <?> {} options",
toggle_word
);
let short_text = format!("<Alt+w> Override, <?> {} options", toggle_word);
let minimum_text = format!("<Alt+w>/<?> ...");
let text = if max_cols >= long_text.chars().count() {
long_text
} else if max_cols >= short_text.chars().count() {
short_text
} else {
minimum_text
};
(text, &["<Alt+w>", "<?>"])
}
fn get_new_layout_text_and_keys(&self, max_cols: usize) -> (&str, &[&str]) {
let long_text = "- New Layout: <n> from current session, <i> import";
let short_text = "New: <n> current, <i> import";
let minimum_text = "New: <n>/<N>/<i> ...";
let text = if max_cols >= long_text.chars().count() {
long_text
} else if max_cols >= short_text.chars().count() {
short_text
} else {
minimum_text
};
(text, &["<n>", "<i>"])
}
pub fn calculate_width(&self, max_cols: usize) -> usize {
let mut width = 0;
// Line 1: Basic controls
let (basic_text, _) = self.get_default_controls(max_cols);
width = width.max(basic_text.chars().count());
// Line 2: Override and toggle
let (override_text, _) = self.get_override_text_and_keys(max_cols);
width = width.max(override_text.chars().count());
// Line 3 (or 5 if show_more_options): New layout controls
let (new_layout_text, _) = self.get_new_layout_text_and_keys(max_cols);
width = width.max(new_layout_text.chars().count());
// Lines 4-5: Advanced options (only if show_more_options)
if self.show_more_options {
let (retain_text, _) = self.get_retain_text_and_highlight(max_cols);
width = width.max(retain_text.chars().count());
let (target_text, _) = self.get_target_text_and_highlight(max_cols);
width = width.max(target_text.chars().count());
}
width
}
pub fn render(&self, x: usize, y: usize, max_cols: usize) {
if self.typing_filter {
self.render_typing_filter_controls(x, y, max_cols);
} else {
self.render_basic_controls(x, y, max_cols);
self.render_override_and_toggle(x, y + 1, max_cols);
let new_layout_y = if self.show_more_options { y + 4 } else { y + 2 };
self.render_new_layout_controls(x, new_layout_y, max_cols);
if self.show_more_options {
self.render_advanced_options(x, y + 2, max_cols);
}
}
}
fn render_basic_controls(&self, x: usize, y: usize, max_width: usize) {
let (text, keys) = self.get_basic_controls_text_and_keys(max_width);
let controls_line = color_control_text(text, keys);
print_text_with_coordinates(controls_line, x, y, None, None);
}
fn render_override_and_toggle(&self, x: usize, y: usize, max_cols: usize) {
let (text, keys) = self.get_override_text_and_keys(max_cols);
let override_line = color_control_text(&text, keys);
print_text_with_coordinates(override_line, x, y, None, None);
}
fn render_new_layout_controls(&self, x: usize, y: usize, max_cols: usize) {
let (text, keys) = self.get_new_layout_text_and_keys(max_cols);
let new_layout_line = color_control_text(text, keys).color_substring(2, "New Layout:");
print_text_with_coordinates(new_layout_line, x, y, None, None);
}
fn render_typing_filter_controls(&self, x: usize, y: usize, max_cols: usize) {
let (text, keys) = self.get_typing_filter_controls(max_cols);
let controls_line = color_control_text(text, keys);
print_text_with_coordinates(controls_line, x, y, None, None);
}
fn render_advanced_options(&self, x: usize, y: usize, max_cols: usize) {
self.render_retain_option(x, y, max_cols);
self.render_target_option(x, y + 1, max_cols);
}
fn render_retain_option(&self, x: usize, y: usize, max_cols: usize) {
let (retain_text, retain_substring) = self.get_retain_text_and_highlight(max_cols);
let retain_line =
color_control_text(retain_text, &["<t>"]).color_substring(0, retain_substring);
print_text_with_coordinates(retain_line, x, y, None, None);
}
fn get_retain_text_and_highlight(&self, max_cols: usize) -> (&str, &str) {
let long_text = " <t> Retain: Terminals | Plugins | Both | None ";
let short_text = " <t> Retain: Term | Pl | Both | None ";
if max_cols >= long_text.chars().count() {
match (self.retain_terminal_panes, self.retain_plugin_panes) {
(true, false) => (
" <t> Retain: [Terminals] | Plugins | Both | None",
"[Terminals]",
),
(false, true) => (
" <t> Retain: Terminals | [Plugins] | Both | None",
"[Plugins]",
),
(true, true) => (
" <t> Retain: Terminals | Plugins | [Both] | None",
"[Both]",
),
(false, false) => (
" <t> Retain: Terminals | Plugins | Both | [None]",
"[None]",
),
}
} else if max_cols >= short_text.chars().count() {
match (self.retain_terminal_panes, self.retain_plugin_panes) {
(true, false) => (" <t> Retain: [Term]| Pl | Both | None", "[Term]"),
(false, true) => (" <t> Retain: Term |[Pl]| Both | None", "[Pl]"),
(true, true) => (" <t> Retain: Term | Pl |[Both]| None", "[Both]"),
(false, false) => (" <t> Retain: Term | Pl | Both |[None]", "[None]"),
}
} else {
match (self.retain_terminal_panes, self.retain_plugin_panes) {
(true, false) => (" <t> R: [T] | P | B | N ...", "[T]"),
(false, true) => (" <t> R: T |[P]| B | N ...", "[P]"),
(true, true) => (" <t> R: T | P |[B]| N ...", "[B]"),
(false, false) => (" <t> R: T | P | B |[N]...", "[N]"),
}
}
}
fn render_target_option(&self, x: usize, y: usize, max_cols: usize) {
let (target_text, target_substring) = self.get_target_text_and_highlight(max_cols);
let target_line =
color_control_text(target_text, &["<a>"]).color_substring(2, target_substring);
print_text_with_coordinates(target_line, x, y, None, None);
}
fn get_target_text_and_highlight(&self, max_cols: usize) -> (&str, &str) {
if self.apply_only_to_active_tab {
let long_text = " <a> Target: All Tabs | [Current]";
let short_text = " <a> Target: All |[Current]";
if max_cols >= long_text.chars().count() {
(long_text, "[Current]")
} else {
(short_text, "[Current]")
}
} else {
let long_text = " <a> Target: [All Tabs] | Current ";
let short_text = " <a> Target: [All]| Current ";
if max_cols >= long_text.chars().count() {
(long_text, "[All Tabs]")
} else {
(short_text, "[All]")
}
}
}
}
pub struct Title<'a> {
text: &'a str,
}
impl<'a> Title<'a> {
pub fn new(text: &'a str) -> Self {
Self { text }
}
pub fn render(&self, x: usize, y: usize) {
let title = Text::new(self.text).color_all(2);
print_text_with_coordinates(title, x, y, None, None);
}
}
pub struct ErrorMessage<'a> {
message: &'a str,
}
impl<'a> ErrorMessage<'a> {
pub fn new(message: &'a str) -> Self {
Self { message }
}
pub fn render(&self, x: usize, y: usize) {
let title = Text::new("Error").error_color_all();
print_text_with_coordinates(title, x, y, None, None);
let message = Text::new(self.message).error_color_all();
print_text_with_coordinates(message, x, y + 2, None, None);
let help = Text::new("Press any key to continue");
print_text_with_coordinates(help, x, y + 4, None, None);
}
}
|