summaryrefslogtreecommitdiffstats
path: root/src/util/input_process.rs
blob: 942703b09b6ab3ec43b32fdb12d0d59d70331aec (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
use signal_hook::consts::signal;
use termion::event::{MouseButton, MouseEvent};
use tui::layout::{Constraint, Direction, Layout};

use crate::commands::{JoshutoRunnable, KeyCommand};
use crate::context::JoshutoContext;
use crate::history::DirectoryHistory;
use crate::io::{FileOp, IOWorkerProgress};
use crate::ui;
use crate::util::event::JoshutoEvent;
use crate::util::format;

pub fn process_mouse(
    event: MouseEvent,
    context: &mut JoshutoContext,
    backend: &mut ui::TuiBackend,
) {
    let f_size = backend.terminal.as_ref().unwrap().size().unwrap();

    let constraints: &[Constraint; 3] = &context.config_ref().default_layout;
    let layout_rect = Layout::default()
        .direction(Direction::Horizontal)
        .vertical_margin(1)
        .constraints(constraints.as_ref())
        .split(f_size);

    let command = match event {
        MouseEvent::Press(MouseButton::WheelUp, x, _) => {
            if x < layout_rect[1].x {
                Some(KeyCommand::ParentCursorMoveUp(1))
            } else if x < layout_rect[2].x {
                Some(KeyCommand::CursorMoveUp(1))
            } else {
                // TODO: scroll in child list
                Some(KeyCommand::CursorMoveUp(1))
            }
        }
        MouseEvent::Press(MouseButton::WheelDown, x, _) => {
            if x < layout_rect[1].x {
                Some(KeyCommand::ParentCursorMoveDown(1))
            } else if x < layout_rect[2].x {
                Some(KeyCommand::CursorMoveDown(1))
            } else {
                // TODO: scroll in child list
                Some(KeyCommand::CursorMoveDown(1))
            }
        }
        _ => None,
    };

    if let Some(command) = command {
        if let Err(e) = command.execute(context, backend) {
            context.push_msg(e.to_string());
        }
    }
    context.flush_event();
}

pub fn process_noninteractive(event: JoshutoEvent, context: &mut JoshutoContext) {
    match event {
        JoshutoEvent::IOWorkerProgress(res) => process_worker_progress(context, res),
        JoshutoEvent::IOWorkerResult(res) => process_finished_worker(context, res),
        JoshutoEvent::Signal(signal::SIGWINCH) => {}
        _ => {}
    }
}

pub fn process_worker_progress(context: &mut JoshutoContext, res: IOWorkerProgress) {
    context.set_worker_progress(res);
    context.update_worker_msg();
}

pub fn process_finished_worker(
    context: &mut JoshutoContext,
    res: std::io::Result<IOWorkerProgress>,
) {
    let observer = context.remove_job().unwrap();
    let options = context.config_ref().sort_option.clone();
    for tab in context.tab_context_mut().iter_mut() {
        let _ = tab.history_mut().reload(observer.dest_path(), &options);
        let _ = tab.history_mut().reload(observer.src_path(), &options);
    }
    observer.join();
    match res {
        Ok(progress) => {
            let op = match progress.kind() {
                FileOp::Copy => "copied",
                FileOp::Cut => "moved",
            };
            let size_str = format::file_size_to_string(progress.processed());
            let msg = format!(
                "successfully {} {} items ({})",
                op,
                progress.len(),
                size_str
            );
            context.push_msg(msg);
        }
        Err(e) => {
            let msg = format!("{}", e);
            context.push_msg(msg);
        }
    }
}