fm/modes/menu/
tui_menu.rs

1use anyhow::Result;
2use serde_yml::Mapping;
3
4use crate::app::Status;
5use crate::common::{is_in_path, TUIS_PATH};
6use crate::io::External;
7use crate::modes::{Execute, TerminalApplications};
8use crate::{impl_content, impl_draw_menu_with_char, impl_selectable, log_info};
9
10/// Directly open a a TUI application
11/// The TUI application shares the same window as fm.
12/// If the user picked "shell", we use the environment variable `$SHELL` or `bash` if it's not set.
13pub fn open_tui_program(program: &str) -> Result<()> {
14    if program == "shell" {
15        External::open_shell_in_window()
16    } else if is_in_path(program) {
17        log_info!("Tui menu execute {program}");
18        External::open_command_in_window(&[program])
19    } else {
20        log_info!("Tui menu program {program} isn't in path");
21        Ok(())
22    }
23}
24
25impl Execute<()> for String {
26    fn execute(&self, _status: &Status) -> Result<()> {
27        open_tui_program(self)
28    }
29}
30
31/// Tui applications which requires a new terminal for interaction.
32#[derive(Clone)]
33pub struct TuiApplications {
34    pub content: Vec<String>,
35    index: usize,
36}
37
38impl TuiApplications {
39    pub fn setup(&mut self) {
40        self.update_from_config(TUIS_PATH);
41    }
42
43    pub fn is_not_set(&self) -> bool {
44        self.content.len() == 1
45    }
46}
47
48impl Default for TuiApplications {
49    fn default() -> Self {
50        let index = 0;
51        let content = vec!["shell".to_owned()];
52        Self { content, index }
53    }
54}
55
56impl TerminalApplications<String, ()> for TuiApplications {
57    fn parse_yaml(&mut self, yaml: &Mapping) {
58        for (key, _) in yaml {
59            let Some(command) = key.as_str() else {
60                continue;
61            };
62            if is_in_path(command) {
63                self.content.push(command.to_owned());
64            }
65        }
66    }
67}
68
69impl_selectable!(TuiApplications);
70impl_content!(TuiApplications, String);
71impl_draw_menu_with_char!(TuiApplications, String);