summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_worker.rs
blob: 3033bb1a63c52b2344fca4f1c1247459df709f2e (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
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Modifier, Style};
use tui::widgets::Widget;

use crate::context::JoshutoContext;
use crate::io::FileOp;
use crate::ui::widgets::TuiTopBar;

pub struct TuiWorker<'a> {
    pub context: &'a JoshutoContext,
}

impl<'a> TuiWorker<'a> {
    pub fn new(context: &'a JoshutoContext) -> Self {
        Self { context }
    }
}

impl<'a> Widget for TuiWorker<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let f_size = area;

        let topbar_width = f_size.width;

        let curr_tab = self.context.tab_context_ref().curr_tab_ref();
        let rect = Rect {
            x: 0,
            y: 0,
            width: topbar_width,
            height: 1,
        };
        TuiTopBar::new(self.context, curr_tab.pwd()).render(rect, buf);

        // TODO: could be styled better

        match self.context.worker_ref() {
            Some(io_obs) => {
                if let Some(progress) = io_obs.progress.as_ref() {
                    let op_str = match progress.kind() {
                        FileOp::Copy => "Copying",
                        FileOp::Cut => "Moving",
                    };
                    let msg = format!(
                        "{} ({}/{}) {:?} -> {:?}",
                        op_str,
                        progress.index() + 1,
                        progress.len(),
                        io_obs.src_path(),
                        io_obs.dest_path()
                    );
                    let style = Style::default();
                    buf.set_stringn(0, 2, msg, area.width as usize, style);

                    let style = Style::default()
                        .fg(Color::Yellow)
                        .add_modifier(Modifier::BOLD);
                    buf.set_stringn(0, 4, "Queue:", area.width as usize, style);

                    let style = Style::default();
                    for (i, worker) in self.context.worker_iter().enumerate() {
                        let op_str = match worker.kind() {
                            FileOp::Copy => "Copy",
                            FileOp::Cut => "Move",
                        };
                        let msg = format!(
                            "{:02} {} {} items {:?} -> {:?}",
                            i + 1,
                            op_str,
                            worker.paths.len(),
                            worker.paths[0].parent().unwrap(),
                            worker.dest
                        );
                        buf.set_stringn(0, (4 + i + 2) as u16, msg, area.width as usize, style);
                    }
                }
            }
            _ => {
                let style = Style::default();
                buf.set_stringn(0, 2, "No operations running", area.width as usize, style);
                let style = Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD);
                buf.set_stringn(0, 4, "Queue:", area.width as usize, style);
            }
        }
    }
}