summaryrefslogtreecommitdiffstats
path: root/src/run.rs
blob: 78a2391e5947984daf1c1dfdd8159d9766255cd0 (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
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::window::JoshutoPanel;
use crate::window::JoshutoView;

fn recurse_get_keycommand<'a>(
    events: &Events,
    keymap: &'a JoshutoCommandMapping,
) -> Option<&'a dyn JoshutoCommand> {
    let (term_rows, term_cols) = ui::getmaxyx();

    let event = {
        let keymap_len = keymap.len();
        let win = JoshutoPanel::new(
            keymap_len as i32 + 1,
            term_cols,
            ((term_rows - keymap_len as i32 - 2) as usize, 0),
        );

        // 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();

        win.move_to_top();
        ui::display_menu(&win, &display_vec);
        ncurses::doupdate();

        let event = events.next();
        event
    };
    ncurses::doupdate();

    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),
                Some(CommandKeybind::SimpleKeybind(s)) => Some(s.as_ref()),
                _ => None,
            },
            _ => None,
        },
        _ => None,
    };
    ncurses::doupdate();
    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);
    let mut view = JoshutoView::new(context.config_t.column_ratio);
    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 */
        match io_handle.as_ref() {
            None => {
                if !context.worker_queue.is_empty() {
                    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::Input(key) => match keymap_t.get(&key) {
                        Some(CommandKeybind::CompositeKeybind(m)) => {
                            match recurse_get_keycommand(&context.events, &m) {
                                Some(command) => {
                                    if let Err(e) = command.execute(&mut context, &mut backend) {
                                        ui::wprint_err(&view.bot_win, e.cause());
                                    }
                                }
                                None => {
                                    ui::wprint_err(
                                        &view.bot_win,
                                        &format!("Unknown keycode: {:?}", key),
                                    );
                                }
                            }
                        }
                        Some(CommandKeybind::SimpleKeybind(command)) => {
                            if let Err(e) = command.execute(&mut context, &mut backend) {
                                eprintln!("{}", e.cause());
                            }
                        }
                        None => {
                            eprintln!("Unknown keycode: {:?}", key);
                        }
                    },
                    Event::IOWorkerProgress(p) => {
                        ui::wprint_err(&view.bot_win, &format!("bytes copied {}", p));
                    }
                    Event::IOWorkerResult => {
                        match io_handle {
                            Some(handle) => {
                                handle.join();
                                ui::wprint_err(&view.bot_win, "io_worker done");
                            }
                            None => {}
                        }
                        io_handle = None;
                    }
                }
                backend.render(&context);
            }
            Err(e) => {
                eprintln!("{:?}", e);
                break;
            }
        }
    }
}