aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/status-bar/src/second_line.rs
blob: 56a71e4b56aa43dd8ac337b4c8b9ec9f804702f3 (plain) (blame)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use ansi_term::{
    ANSIStrings,
    Color::{Fixed, RGB},
    Style,
};
use zellij_tile::prelude::*;
use zellij_tile_utils::palette_match;

use crate::{
    tip::{data::TIPS, TipFn},
    LinePart, MORE_MSG,
};

#[derive(Clone, Copy)]
enum StatusBarTextColor {
    White,
    Green,
    Orange,
}

#[derive(Clone, Copy)]
enum StatusBarTextBoldness {
    Bold,
    NotBold,
}

fn full_length_shortcut(
    is_first_shortcut: bool,
    letter: &str,
    description: &str,
    palette: Palette,
) -> LinePart {
    let text_color = palette_match!(match palette.theme_hue {
        ThemeHue::Dark => palette.white,
        ThemeHue::Light => palette.black,
    });
    let green_color = palette_match!(palette.green);
    let separator = if is_first_shortcut { " " } else { " / " };
    let separator = Style::new().fg(text_color).paint(separator);
    let shortcut_len = letter.chars().count() + 3; // 2 for <>'s around shortcut, 1 for the space
    let shortcut_left_separator = Style::new().fg(text_color).paint("<");
    let shortcut = Style::new().fg(green_color).bold().paint(letter);
    let shortcut_right_separator = Style::new().fg(text_color).paint("> ");
    let description_len = description.chars().count();
    let description = Style::new().fg(text_color).bold().paint(description);
    let len = shortcut_len + description_len + separator.chars().count();
    LinePart {
        part: ANSIStrings(&[
            separator,
            shortcut_left_separator,
            shortcut,
            shortcut_right_separator,
            description,
        ])
        .to_string(),
        len,
    }
}

fn first_word_shortcut(
    is_first_shortcut: bool,
    letter: &str,
    description: &str,
    palette: Palette,
) -> LinePart {
    let text_color = palette_match!(match palette.theme_hue {
        ThemeHue::Dark => palette.white,
        ThemeHue::Light => palette.black,
    });
    let green_color = palette_match!(palette.green);
    let separator = if is_first_shortcut { " " } else { " / " };
    let separator = Style::new().fg(text_color).paint(separator);
    let shortcut_len = letter.chars().count() + 3; // 2 for <>'s around shortcut, 1 for the space
    let shortcut_left_separator = Style::new().fg(text_color).paint("<");
    let shortcut = Style::new().fg(green_color).bold().paint(letter);
    let shortcut_right_separator = Style::new().fg(text_color).paint("> ");
    let description_first_word = description.split(' ').next().unwrap_or("");
    let description_first_word_length = description_first_word.chars().count();
    let description_first_word = Style::new()
        .fg(text_color)
        .bold()
        .paint(description_first_word);
    let len = shortcut_len + description_first_word_length + separator.chars().count();
    LinePart {
        part: ANSIStrings(&[
            separator,
            shortcut_left_separator,
            shortcut,
            shortcut_right_separator,
            description_first_word,
        ])
        .to_string(),
        len,
    }
}

fn locked_interface_indication(palette: Palette) -> LinePart {
    let locked_text = " -- INTERFACE LOCKED -- ";
    let locked_text_len = locked_text.chars().count();
    let text_color = palette_match!(match palette.theme_hue {
        ThemeHue::Dark => palette.white,
        ThemeHue::Light => palette.black,
    });
    let locked_styled_text = Style::new().fg(text_color).bold().paint(locked_text);
    LinePart {
        part: locked_styled_text.to_string(),
        len: locked_text_len,
    }
}

fn show_extra_hints(
    palette: Palette,
    text_with_style: Vec<(&str, StatusBarTextColor, StatusBarTextBoldness)>,
) -> LinePart {
    use StatusBarTextBoldness::*;
    use StatusBarTextColor::*;
    // get the colors
    let white_color = palette_match!(palette.white);
    let green_color = palette_match!(palette.green);
    let orange_color = palette_match!(palette.orange);
    // calculate length of tipp
    let len = text_with_style
        .iter()
        .fold(0, |len_sum, (text, _, _)| len_sum + text.chars().count());
    // apply the styles defined above
    let styled_text = text_with_style
        .into_iter()
        .map(|(text, color, is_bold)| {
            let color = match color {
                White => white_color,
                Green => green_color,
                Orange => orange_color,
            };
            match is_bold {
                Bold => Style::new().fg(color).bold().paint(text),
                NotBold => Style::new().fg(color).paint(text),
            }
        })
        .collect::<Vec<_>>();
    LinePart {
        part: ANSIStrings(&styled_text[..]).to_string(),
        len,
    }
}

/// Creates hints for usage of Pane Mode
fn confirm_pane_selection(palette: Palette) -> LinePart {
    use StatusBarTextBoldness::*;
    use StatusBarTextColor::*;
    let text_with_style = [
        (" / ", White, NotBold),
        ("<ENTER>", Green, Bold),
        (" Select pane", White, Bold),
    ];
    show_extra_hints(palette, text_with_style.to_vec())
}

/// Creates hints for usage of Rename Mode in Pane Mode
fn select_pane_shortcut(palette: Palette) -> LinePart {
    use StatusBarTextBoldness::*;
    use StatusBarTextColor::*;
    let text_with_style = [
        (" / ", White, NotBold),
        ("Alt", Orange, Bold),
        (" + ", White, NotBold),
        ("<", Green, Bold),
        ("[]", Green, Bold),
        (" or ", White, NotBold),
        ("hjkl", Green, Bold),
        (">", Green, Bold),
        (" Select pane", White, Bold),
    ];
    show_extra_hints(palette, text_with_style.to_vec())
}

fn full_shortcut_list_nonstandard_mode(
    extra_hint_producing_function: fn(Palette) -> LinePart,
) -> impl FnOnce(&ModeInfo) -> LinePart {
    move |help| {
        let mut line_part = LinePart::default();
        for (i, (letter, description)) in help.keybinds.iter().enumerate() {
            let shortcut = full_length_shortcut(i == 0, letter, description, help.style.colors);
            line_part.len += shortcut.len;
            line_part.part = format!("{}{}", line_part.part, shortcut,);
        }
        let select_pane_shortcut = extra_hint_producing_function(help.style.colors);
        line_part.len += select_pane_shortcut.len;
        line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,);
        line_part
    }
}

fn full_shortcut_list(help: &ModeInfo, tip: TipFn) -> LinePart {
    match help.mode {
        InputMode::Normal => tip(help.style.colors),
        InputMode::Locked => locked_interface_indication(help.style.colors),
        InputMode::Tmux => full_tmux_mode_indication(help),
        InputMode::RenamePane => full_shortcut_list_nonstandard_mode(select_pane_shortcut)(help),
        _ => full_shortcut_list_nonstandard_mode(confirm_pane_selection)(help),
    }
}

fn shortened_shortcut_list_nonstandard_mode(
    extra_hint_producing_function: fn(Palette) -> LinePart,
) -> impl FnOnce(&ModeInfo) -> LinePart {
    move |help| {
        let mut line_part = LinePart::default();
        for (i, (letter, description)) in help.keybinds.iter().enumerate() {
            let shortcut = first_word_shortcut(i == 0, letter, description, help.style.colors);
            line_part.len += shortcut.len;
            line_part.part = format!("{}{}", line_part.part, shortcut,);
        }
        let select_pane_shortcut = extra_hint_producing_function(help.style.colors);
        line_part.len += select_pane_shortcut.len;
        line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,);
        line_part
    }
}

fn shortened_shortcut_list(help: &ModeInfo, tip: TipFn) -> LinePart {
    match help.mode {
        InputMode::Normal => tip(help.style.colors),
        InputMode::Locked => locked_interface_indication(help.style.colors),
        InputMode::Tmux => short_tmux_mode_indication(help),
        InputMode::RenamePane => {
            shortened_shortcut_list_nonstandard_mode(select_pane_shortcut)(help)
        },
        _ => shortened_shortcut_list_nonstandard_mode(confirm_pane_selection)(help),
    }
}

fn best_effort_shortcut_list_nonstandard_mode(
    extra_hint_producing_function: fn(Palette) -> LinePart,
) -> impl FnOnce(&ModeInfo, usize) -> LinePart {
    move |help, max_len| {
        let mut line_part = LinePart::default();
        for (i, (letter, description)) in help.keybinds.iter().enumerate() {
            let shortcut = first_word_shortcut(i == 0, letter, description, help.style.colors);
            if line_part.len + shortcut.len + MORE_MSG.chars().count() > max_len {
                // TODO: better
                line_part.part = format!("{}{}", line_part.part, MORE_MSG);
                line_part.len += MORE_MSG.chars().count();
                break;
            }
            line_part.len += shortcut.len;
            line_part.part = format!("{}{}", line_part.part, shortcut);
        }
        let select_pane_shortcut = extra_hint_producing_function(help.style.colors);
        if line_part.len + select_pane_shortcut.len <= max_len {
            line_part.len += select_pane_shortcut.len;
            line_part.part = format!("{}{}", line_part.part, select_pane_shortcut,);
        }
        line_part
    }
}

fn best_effort_tmux_shortcut_list(help: &ModeInfo, max_len: usize) -> LinePart {
    let mut line_part = tmux_mode_indication(help);
    for (i, (letter, description)) in help.keybinds.iter().enumerate() {
        let shortcut = first_word_shortcut(i == 0, letter, description, help.style.colors);
        if line_part.len + shortcut.len + MORE_MSG.chars().count() > max_len {
            // TODO: better
            line_part.part = format!("{}{}", line_part.part, MORE_MSG);
            line_part.len += MORE_MSG.chars().count();
            break;
        }
        line_part.len += shortcut.len;
        line_part.part = format!("{}{}", line_part.part, shortcut);
    }
    line_part
}

fn best_effort_shortcut_list(help: &ModeInfo, tip: TipFn, max_len: usize) -> LinePart {
    match help.mode {
        InputMode::Normal => {
            let line_part = tip(help.style.colors);
            if line_part.len <= max_len {
                line_part
            } else {
                LinePart::default()
            }
        },
        InputMode::Locked => {
            let line_part = locked_interface_indication(help.style.colors);
            if line_part.len <= max_len {
                line_part
            } else {
                LinePart::default()
            }
        },
        InputMode::Tmux => best_effort_tmux_shortcut_list(help, max_len),
        InputMode::RenamePane => {
            best_effort_shortcut_list_nonstandard_mode(select_pane_shortcut)(help, max_len)
        },
        _ => best_effort_shortcut_list_nonstandard_mode(confirm_pane_selection)(help, max_len),
    }
}

pub fn keybinds(help: &ModeInfo, tip_name: &str, max_width: usize) -> LinePart {
    // It is assumed that there is at least one TIP data in the TIPS HasMap.
    let tip_body = TIPS
        .get(tip_name)
        .unwrap_or_else(|| TIPS.get("quicknav").unwrap());

    let full_shortcut_list = full_shortcut_list(help, tip_body.full);
    if full_shortcut_list.len <= max_width {
        return full_shortcut_list;
    }
    let shortened_shortcut_list = shortened_shortcut_list(help, tip_body.medium);
    if shortened_shortcut_list.len <= max_width {
        return shortened_shortcut_list;
    }
    best_effort_shortcut_list(help, tip_body.short, max_width)
}

pub fn text_copied_hint(palette: &Palette, copy_destination: CopyDestination) -> LinePart {
    let green_color = palette_match!(palette.green);
    let hint = match copy_destination {
        CopyDestination::Command => "Text piped to external command",
        #[cfg(not(target_os = "macos"))]
        CopyDestination::Primary => "Text copied to system primary selection",
        #[cfg(target_os = "macos")] // primary selection does not exist on macos
        CopyDestination::Primary => "Text copied to system clipboard",
        CopyDestination::System => "Text copied to system clipboard",
    };
    LinePart {
        part: Style::new().fg(green_color).bold().paint(hint).to_string(),
        len: hint.len(),
    }
}

pub fn system_clipboard_error(palette: &Palette) -> LinePart {
    let hint = " Error using the system clipboard.";
    let red_color = palette_match!(palette.red);
    LinePart {
        part: Style::new().fg(red_color).bold().paint(hint).to_string(),
        len: hint.len(),
    }
}

pub fn fullscreen_panes_to_hide(palette: &Palette, panes_to_hide: usize) -> LinePart {
    let text_color = palette_match!(match palette.theme_hue {
        ThemeHue::Dark => palette.white,
        ThemeHue::Light => palette.black,
    });
    let green_color = palette_match!(palette.green);
    let orange_color = palette_match!(palette.orange);
    let shortcut_left_separator = Style::new().fg(text_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(text_color).bold().paint("): ");
    let fullscreen = "FULLSCREEN";
    let puls = "+ ";
    let panes = panes_to_hide.to_string();
    let hide = " hidden panes";
    let len = fullscreen.chars().count()
        + puls.chars().count()
        + panes.chars().count()
        + hide.chars().count()
        + 5; // 3 for ():'s around shortcut, 2 for the space
    LinePart {
        part: format!(
            "{}{}{}{}{}{}",
            shortcut_left_separator,
            Style::new().fg(orange_color).bold().paint(fullscreen),
            shortcut_right_separator,
            Style::new().fg(text_color).bold().paint(puls),
            Style::new().fg(green_color).bold().paint(panes),
            Style::new().fg(text_color).bold().paint(hide)
        ),
        len,
    }
}

pub fn floating_panes_are_visible(palette: &Palette) -> LinePart {
    let white_color = match palette.white {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let green_color = match palette.green {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let orange_color = match palette.orange {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let shortcut_left_separator = Style::new().fg(white_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(white_color).bold().paint("): ");
    let floating_panes = "FLOATING PANES VISIBLE";
    let press = "Press ";
    let ctrl = "Ctrl-p ";
    let plus = "+ ";
    let p_left_separator = "<";
    let p = "w";
    let p_right_separator = "> ";
    let to_hide = "to hide.";

    let len = floating_panes.chars().count()
        + press.chars().count()
        + ctrl.chars().count()
        + plus.chars().count()
        + p_left_separator.chars().count()
        + p.chars().count()
        + p_right_separator.chars().count()
        + to_hide.chars().count()
        + 5; // 3 for ():'s around floating_panes, 2 for the space
    LinePart {
        part: format!(
            "{}{}{}{}{}{}{}{}{}{}",
            shortcut_left_separator,
            Style::new().fg(orange_color).bold().paint(floating_panes),
            shortcut_right_separator,
            Style::new().fg(white_color).bold().paint(press),
            Style::new().fg(green_color).bold().paint(ctrl),
            Style::new().fg(white_color).bold().paint(plus),
            Style::new().fg(white_color).bold().paint(p_left_separator),
            Style::new().fg(green_color).bold().paint(p),
            Style::new().fg(white_color).bold().paint(p_right_separator),
            Style::new().fg(white_color).bold().paint(to_hide),
        ),
        len,
    }
}

pub fn tmux_mode_indication(help: &ModeInfo) -> LinePart {
    let white_color = match help.style.colors.white {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let orange_color = match help.style.colors.orange {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };

    let shortcut_left_separator = Style::new().fg(white_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(white_color).bold().paint("): ");
    let tmux_mode_text = "TMUX MODE";
    let tmux_mode_indicator = Style::new().fg(orange_color).bold().paint(tmux_mode_text);
    let line_part = LinePart {
        part: format!(
            "{}{}{}",
            shortcut_left_separator, tmux_mode_indicator, shortcut_right_separator
        ),
        len: tmux_mode_text.chars().count() + 5, // 2 for the separators, 3 for the colon and following space
    };
    line_part
}

pub fn full_tmux_mode_indication(help: &ModeInfo) -> LinePart {
    let white_color = match help.style.colors.white {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let orange_color = match help.style.colors.orange {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };

    let shortcut_left_separator = Style::new().fg(white_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(white_color).bold().paint("): ");
    let tmux_mode_text = "TMUX MODE";
    let tmux_mode_indicator = Style::new().fg(orange_color).bold().paint(tmux_mode_text);
    let mut line_part = LinePart {
        part: format!(
            "{}{}{}",
            shortcut_left_separator, tmux_mode_indicator, shortcut_right_separator
        ),
        len: tmux_mode_text.chars().count() + 5, // 2 for the separators, 3 for the colon and following space
    };

    for (i, (letter, description)) in help.keybinds.iter().enumerate() {
        let shortcut = full_length_shortcut(i == 0, letter, description, help.style.colors);
        line_part.len += shortcut.len;
        line_part.part = format!("{}{}", line_part.part, shortcut,);
    }
    line_part
}

pub fn short_tmux_mode_indication(help: &ModeInfo) -> LinePart {
    let white_color = match help.style.colors.white {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let orange_color = match help.style.colors.orange {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };

    let shortcut_left_separator = Style::new().fg(white_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(white_color).bold().paint("): ");
    let tmux_mode_text = "TMUX MODE";
    let tmux_mode_indicator = Style::new().fg(orange_color).bold().paint(tmux_mode_text);
    let mut line_part = LinePart {
        part: format!(
            "{}{}{}",
            shortcut_left_separator, tmux_mode_indicator, shortcut_right_separator
        ),
        len: tmux_mode_text.chars().count() + 5, // 2 for the separators, 3 for the colon and following space
    };

    for (i, (letter, description)) in help.keybinds.iter().enumerate() {
        let shortcut = first_word_shortcut(i == 0, letter, description, help.style.colors);
        line_part.len += shortcut.len;
        line_part.part = format!("{}{}", line_part.part, shortcut);
    }
    line_part
}

pub fn locked_fullscreen_panes_to_hide(palette: &Palette, panes_to_hide: usize) -> LinePart {
    let text_color = palette_match!(match palette.theme_hue {
        ThemeHue::Dark => palette.white,
        ThemeHue::Light => palette.black,
    });
    let green_color = palette_match!(palette.green);
    let orange_color = palette_match!(palette.orange);
    let locked_text = " -- INTERFACE LOCKED -- ";
    let shortcut_left_separator = Style::new().fg(text_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(text_color).bold().paint("): ");
    let fullscreen = "FULLSCREEN";
    let puls = "+ ";
    let panes = panes_to_hide.to_string();
    let hide = " hidden panes";
    let len = locked_text.chars().count()
        + fullscreen.chars().count()
        + puls.chars().count()
        + panes.chars().count()
        + hide.chars().count()
        + 5; // 3 for ():'s around shortcut, 2 for the space
    LinePart {
        part: format!(
            "{}{}{}{}{}{}{}",
            Style::new().fg(text_color).bold().paint(locked_text),
            shortcut_left_separator,
            Style::new().fg(orange_color).bold().paint(fullscreen),
            shortcut_right_separator,
            Style::new().fg(text_color).bold().paint(puls),
            Style::new().fg(green_color).bold().paint(panes),
            Style::new().fg(text_color).bold().paint(hide)
        ),
        len,
    }
}

pub fn locked_floating_panes_are_visible(palette: &Palette) -> LinePart {
    let white_color = match palette.white {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let orange_color = match palette.orange {
        PaletteColor::Rgb((r, g, b)) => RGB(r, g, b),
        PaletteColor::EightBit(color) => Fixed(color),
    };
    let shortcut_left_separator = Style::new().fg(white_color).bold().paint(" (");
    let shortcut_right_separator = Style::new().fg(white_color).bold().paint(")");
    let locked_text = " -- INTERFACE LOCKED -- ";
    let floating_panes = "FLOATING PANES VISIBLE";

    let len = locked_text.chars().count() + floating_panes.chars().count();
    LinePart {
        part: format!(
            "{}{}{}{}",
            Style::new().fg(white_color).bold().paint(locked_text),
            shortcut_left_separator,
            Style::new().fg(orange_color).bold().paint(floating_panes),
            shortcut_right_separator,
        ),
        len,
    }
}