summaryrefslogtreecommitdiffstats
path: root/src/commands/shell.rs
blob: d813ed6fb94d9e4514544779e59a9e1165e29bb9 (plain)
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
use std::process;

use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::ui::TuiBackend;

use super::reload;

pub fn shell_command(context: &mut JoshutoContext, words: &[String]) -> std::io::Result<()> {
    let mut command = process::Command::new(words[0].clone());
    for word in words.iter().skip(1) {
        match (*word).as_str() {
            "%s" => {
                if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
                    let mut i = 0;
                    for entry in curr_list.selected_entries().map(|e| e.file_name()) {
                        command.arg(entry);
                        i += 1;
                    }
                    if i == 0 {
                        if let Some(entry) = curr_list.curr_entry_ref() {
                            command.arg(entry.file_name());
                        }
                    }
                }
            }
            s => {
                command.arg(s);
            }
        };
    }
    command.status()?;
    Ok(())
}

pub fn shell(
    context: &mut JoshutoContext,
    backend: &mut TuiBackend,
    words: &[String],
) -> JoshutoResult<()> {
    backend.terminal_drop();
    let res = shell_command(context, words);
    reload::soft_reload(context.tab_context_ref().get_index(), context)?;
    context.push_msg(format!("Finished: {}", words.join(" ")));
    backend.terminal_restore()?;
    res?;
    Ok(())
}