summaryrefslogtreecommitdiffstats
path: root/src/app/widgets/bottom_widgets/disk.rs
blob: 74b63f860e9389271df8b3598e5578e7c6813626 (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
use std::collections::HashMap;

use crossterm::event::{KeyEvent, MouseEvent};
use tui::{backend::Backend, layout::Rect, widgets::Borders, Frame};

use crate::{
    app::{
        data_farmer::DataCollection, event::ComponentEventResult,
        sort_text_table::SimpleSortableColumn, text_table::TextTableData, AppScrollWidgetState,
        CanvasTableWidthState, Component, TextTable, Widget,
    },
    canvas::Painter,
    data_conversion::convert_disk_row,
    options::layout_options::LayoutRule,
};

pub struct DiskWidgetState {
    pub scroll_state: AppScrollWidgetState,
    pub table_width_state: CanvasTableWidthState,
}

#[derive(Default)]
pub struct DiskState {
    pub widget_states: HashMap<u64, DiskWidgetState>,
}

impl DiskState {
    pub fn init(widget_states: HashMap<u64, DiskWidgetState>) -> Self {
        DiskState { widget_states }
    }

    pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut DiskWidgetState> {
        self.widget_states.get_mut(&widget_id)
    }

    pub fn get_widget_state(&self, widget_id: u64) -> Option<&DiskWidgetState> {
        self.widget_states.get(&widget_id)
    }
}

/// A table displaying disk data.
pub struct DiskTable {
    table: TextTable<SimpleSortableColumn>,
    bounds: Rect,

    display_data: TextTableData,

    width: LayoutRule,
    height: LayoutRule,
    block_border: Borders,
    show_scroll_index: bool,
}

impl Default for DiskTable {
    fn default() -> Self {
        let table = TextTable::new(vec![
            SimpleSortableColumn::new_flex("Disk".into(), None, false, 0.2),
            SimpleSortableColumn::new_flex("Mount".into(), None, false, 0.2),
            SimpleSortableColumn::new_hard("Used".into(), None, false, Some(5)),
            SimpleSortableColumn::new_hard("Free".into(), None, false, Some(6)),
            SimpleSortableColumn::new_hard("Total".into(), None, false, Some(6)),
            SimpleSortableColumn::new_hard("R/s".into(), None, false, Some(7)),
            SimpleSortableColumn::new_hard("W/s".into(), None, false, Some(7)),
        ]);

        Self {
            table,
            bounds: Rect::default(),
            display_data: Default::default(),
            width: LayoutRule::default(),
            height: LayoutRule::default(),
            block_border: Borders::ALL,
            show_scroll_index: false,
        }
    }
}

impl DiskTable {
    /// Sets the width.
    pub fn width(mut self, width: LayoutRule) -> Self {
        self.width = width;
        self
    }

    /// Sets the height.
    pub fn height(mut self, height: LayoutRule) -> Self {
        self.height = height;
        self
    }

    /// Sets the block border style.
    pub fn basic_mode(mut self, basic_mode: bool) -> Self {
        if basic_mode {
            self.block_border = *crate::constants::SIDE_BORDERS;
        }

        self
    }

    /// Sets whether to show the scroll index.
    pub fn show_scroll_index(mut self, show_scroll_index: bool) -> Self {
        self.show_scroll_index = show_scroll_index;
        self
    }
}

impl Component for DiskTable {
    fn handle_key_event(&mut self, event: KeyEvent) -> ComponentEventResult {
        self.table.handle_key_event(event)
    }

    fn handle_mouse_event(&mut self, event: MouseEvent) -> ComponentEventResult {
        self.table.handle_mouse_event(event)
    }

    fn bounds(&self) -> Rect {
        self.bounds
    }

    fn set_bounds(&mut self, new_bounds: Rect) {
        self.bounds = new_bounds;
    }
}

impl Widget for DiskTable {
    fn get_pretty_name(&self) -> &'static str {
        "Disks"
    }

    fn draw<B: Backend>(
        &mut self, painter: &Painter, f: &mut Frame<'_, B>, area: Rect, selected: bool,
        expanded: bool,
    ) {
        let block = self
            .block()
            .selected(selected)
            .borders(self.block_border)
            .show_esc(expanded);

        self.table.draw_tui_table(
            painter,
            f,
            &self.display_data,
            block,
            area,
            selected,
            self.show_scroll_index,
        );
    }

    fn update_data(&mut self, data_collection: &DataCollection) {
        self.display_data = convert_disk_row(data_collection);
    }

    fn width(&self) -> LayoutRule {
        self.width
    }

    fn height(&self) -> LayoutRule {
        self.height
    }
}