fm/modes/utils/
line_display.rs

1use crate::app::Status;
2use crate::modes::{
3    InputCompleted, InputSimple, MarkAction, Menu, Navigate, NeedConfirmation, PasswordKind,
4    PasswordUsage,
5};
6
7/// Used by different kind of menu to display informations about the current menu.
8/// Most of the time it's a few lines describing the menu actions.
9pub trait LineDisplay {
10    /// Returns a displayable representation of the object as a vector of `String`s
11    fn line_display(&self, status: &Status) -> Vec<String>;
12}
13
14impl LineDisplay for Menu {
15    fn line_display(&self, status: &Status) -> Vec<String> {
16        match self {
17            Self::Navigate(mode) => mode.line_display(status),
18            Self::InputSimple(mode) => mode.line_display(status),
19            Self::InputCompleted(mode) => mode.line_display(status),
20            Self::NeedConfirmation(mode) => mode.line_display(status),
21            Self::Nothing => vec![],
22        }
23    }
24}
25
26impl LineDisplay for NeedConfirmation {
27    fn line_display(&self, _status: &Status) -> Vec<String> {
28        vec![format!("{self}"), " (y/n)".to_owned()]
29    }
30}
31
32impl LineDisplay for Navigate {
33    fn line_display(&self, _status: &Status) -> Vec<String> {
34        match self {
35            Self::Marks(MarkAction::Jump) => {
36                vec!["Jump to...".to_owned()]
37            }
38            Self::Marks(MarkAction::New) => {
39                vec!["Save mark...".to_owned()]
40            }
41            _ => {
42                vec![Menu::Navigate(*self).to_string()]
43            }
44        }
45    }
46}
47
48impl LineDisplay for InputCompleted {
49    fn line_display(&self, status: &Status) -> Vec<String> {
50        let tab = status.current_tab();
51        let mut completion_strings = vec![tab.menu_mode.to_string(), status.menu.input.string()];
52        if let Some(completion) = status
53            .menu
54            .completion
55            .complete_input_string(&status.menu.input.string())
56        {
57            completion_strings.push(completion.to_owned());
58        }
59        if matches!(*self, Self::Exec) {
60            for path in &status.menu.flagged.content {
61                completion_strings.push(format!(" {path}", path = path.display()));
62            }
63        }
64        completion_strings
65    }
66}
67
68impl LineDisplay for InputSimple {
69    fn line_display(&self, status: &Status) -> Vec<String> {
70        match self {
71            Self::Password(_, PasswordUsage::CRYPTSETUP(PasswordKind::CRYPTSETUP)) => {
72                vec![
73                    PasswordKind::CRYPTSETUP.to_string(),
74                    status.menu.input.password(),
75                ]
76            }
77            Self::Password(..) => {
78                vec![PasswordKind::SUDO.to_string(), status.menu.input.password()]
79            }
80            _ => {
81                vec![
82                    Menu::InputSimple(*self).to_string(),
83                    status.menu.input.string(),
84                ]
85            }
86        }
87    }
88}