summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_worker.rs
blob: 110d6943d4c7befe65df165a31e4a9f0dbc18989 (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::AppContext;
use crate::io::FileOp;
use crate::util::format;

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

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

impl<'a> Widget for TuiWorker<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        match self.context.worker_context_ref().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 processed_size = format::file_size_to_string(progress.bytes_processed());
                    let total_size = format::file_size_to_string(progress.total_bytes());

                    let msg = format!(
                        "{} ({}/{}) ({}/{}) {:?}",
                        op_str,
                        progress.files_processed() + 1,
                        progress.total_files(),
                        processed_size,
                        total_size,
                        io_obs.dest_path()
                    );

                    let style = Style::default();
                    buf.set_stringn(0, 2, msg, area.width as usize, style);

                    // draw a progress bar
                    let progress_bar_width = (progress.files_processed() as f32
                        / progress.total_files() as f32
                        * area.width as f32) as usize;

                    let progress_bar_space = " ".repeat(progress_bar_width);
                    let style = Style::default().bg(Color::Blue);
                    buf.set_stringn(0, 3, progress_bar_space, area.width as usize, style);

                    // draw queued up work
                    let style = Style::default()
                        .fg(Color::Yellow)
                        .add_modifier(Modifier::BOLD);
                    buf.set_stringn(0, 5, "Queue:", area.width as usize, style);

                    let style = Style::default();
                    for (i, worker) in self.context.worker_context_ref().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.dest
                        );
                        buf.set_stringn(0, (5 + 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);
            }
        }
    }
}