summaryrefslogtreecommitdiffstats
path: root/src/run.rs
blob: 54656a2327821c44f1ebe6faf0e7377350b7050f (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
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
use std::thread;

use termion::event::Key;

use crate::commands::{CommandKeybind, CursorMoveUp, JoshutoCommand, JoshutoRunnable};
use crate::config::{JoshutoCommandMapping, JoshutoConfig};
use crate::context::JoshutoContext;
use crate::tab::JoshutoTab;
use crate::ui;
use crate::util::event::{Event, Events};
use crate::util::menu::OptionMenu;

fn recurse_get_keycommand<'a>(
    events: &Events,
    keymap: &'a JoshutoCommandMapping,
    backend: &'a mut ui::TuiBackend,
) -> Option<&'a dyn JoshutoCommand> {
    let event = {
        let mut menu = OptionMenu::new(backend, events);
        let keymap_len = keymap.len();

        // TODO: format keys better, rather than debug
        let mut display_vec: Vec<String> = keymap
            .iter()
            .map(|(k, v)| format!("  {:?}\t{}", k, v))
            .collect();
        display_vec.sort();
        let display_str: Vec<&str> = display_vec.iter().map(|v| v.as_str()).collect();
        let result = menu.get_option(&display_str);
        eprintln!("{:?}", result);

        let event = events.next();
        event
    };

    let command = match event {
        Ok(Event::Input(input)) => match input {
            Key::Esc => None,
            key @ Key::Char(_) => match keymap.get(&key) {
                Some(CommandKeybind::CompositeKeybind(m)) => {
                    recurse_get_keycommand(events, &m, backend)
                }
                Some(CommandKeybind::SimpleKeybind(s)) => Some(s.as_ref()),
                _ => None,
            },
            _ => None,
        },
        _ => None,
    };
    command
}

pub fn run(config_t: JoshutoConfig, keymap_t: JoshutoCommandMapping) {
    let mut backend: ui::TuiBackend = ui::TuiBackend::new().unwrap();

    let mut context = JoshutoContext::new(config_t);
    match std::env::current_dir() {
        Ok(curr_path) => match JoshutoTab::new(curr_path, &context.config_t.sort_option) {
            Ok(s) => context.push_tab(s),
            Err(e) => {
                eprintln!("{}", e);
                return;
            }
        },
        Err(e) => {
            eprintln!("{}", e);
            return;
        }
    }
    {
        let tmp = CursorMoveUp::new(0);
        tmp.execute(&mut context, &mut backend);
    }
    backend.render(&context);

    let mut io_handle = None;
    while !context.exit {
        /* checking if there are workers that need to be run */
        if !context.worker_queue.is_empty() {
            if let None = io_handle.as_ref() {
                let worker = context.worker_queue.pop_front().unwrap();
                io_handle = {
                    let event_tx = context.events.event_tx.clone();
                    let sync_tx = context.events.sync_tx.clone();
                    let thread = thread::spawn(move || {
                        worker.start();
                        while let Ok(evt) = worker.recv() {
                            let _ = event_tx.send(evt);
                            let _ = sync_tx.send(());
                        }
                        worker.handle.join();
                        let _ = event_tx.send(Event::IOWorkerResult);
                        let _ = sync_tx.send(());
                    });
                    Some(thread)
                };
            }
        }

        match context.events.next() {
            Ok(event) => {
                match event {
                    Event::IOWorkerProgress(p) => {
                        eprintln!("{}", &format!("bytes copied {}", p));
                    }
                    Event::IOWorkerResult => {
                        match io_handle {
                            Some(handle) => {
                                handle.join();
                                eprintln!("io_worker done");
                            }
                            None => {}
                        }
                        io_handle = None;
                    }
                    Event::Input(key) => match keymap_t.get(&key) {
                        None => {
                            eprintln!("Unknown keycode: {:?}", key);
                        }
                        Some(CommandKeybind::SimpleKeybind(command)) => {
                            if let Err(e) = command.execute(&mut context, &mut backend) {
                                eprintln!("{}", e.cause());
                            }
                        }
                        Some(CommandKeybind::CompositeKeybind(m)) => {
                            let mut cmd = None;
                            let mut map: &JoshutoCommandMapping = &m;

                            loop {
                                let event2 = {
                                    let mut menu = OptionMenu::new(&mut backend, &context.events);

                                    // TODO: format keys better, rather than debug
                                    let mut display_vec: Vec<String> = map
                                        .iter()
                                        .map(|(k, v)| format!("  {:?}\t{}", k, v))
                                        .collect();
                                    display_vec.sort();
                                    let display_str: Vec<&str> =
                                        display_vec.iter().map(|v| v.as_str()).collect();
                                    let result = menu.get_option(&display_str);
                                    result
                                };

                                match event2 {
                                    None => break,
                                    Some(key) => match key {
                                        Key::Char(_) => match map.get(&key) {
                                            Some(CommandKeybind::CompositeKeybind(m)) => map = &m,
                                            Some(CommandKeybind::SimpleKeybind(s)) => {
                                                cmd = Some(s.as_ref());
                                                break;
                                            }
                                            None => break,
                                        },
                                        _ => {}
                                    },
                                }
                            }
                            if let Some(command) = cmd {
                                if let Err(e) = command.execute(&mut context, &mut backend) {
                                    eprintln!("{}", e.cause());
                                }
                            }
                        }
                    },
                }
                backend.render(&context);
            }
            Err(e) => {
                eprintln!("{:?}", e);
                break;
            }
        }
    }
}