index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / ui / table.rs
-rw-r--r-- blob 7950d4d492211f39e8c740f757630e7395756f8c 5669 bytes (plain)
1
use std::rc::Rc;
2
3
use crate::{app::App, tui::Tabs, ui::list::StatefulList};
4
use ratatui::{
5
    Frame,
6
    layout::{Constraint, Rect},
7
    style::{Style, Stylize},
8
    widgets::{Block, Cell, Row, Table},
9
};
10
11
pub struct TableData<'a, T> {
12
    pub title: &'a str,
13
    pub header_cols: Vec<&'a str>,
14
    pub constraint: Vec<Constraint>,
15
    pub cells: Vec<Row<'a>>,
16
    pub list: &'a mut StatefulList<T>,
17
}
18
19
pub fn draw<Any>(f: &mut Frame, area: Rect, table_data: &mut TableData<Any>, is_active: bool) {
20
    let border_style = if is_active {
21
        Style::new().bold().green()
22
    } else {
23
        Style::new()
24
    };
25
    let row_style = if is_active {
26
        Style::new().on_blue().black()
27
    } else {
28
        Style::new()
29
    };
30
    let table = Table::new(table_data.cells.clone(), table_data.constraint.clone())
31
        .header(Row::new(table_data.header_cols.clone()).bold())
32
        .block(
33
            Block::bordered()
34
                .title(table_data.title)
35
                .border_style(border_style),
36
        )
37
        .row_highlight_style(row_style);
38
39
    f.render_stateful_widget(table, area, &mut table_data.list.state);
40
}
41
42
// TODO: this
43
pub fn draw_saved_connections(
44
    f: &mut Frame<'_>,
45
    body_chunks: &Rc<[Rect]>,
46
    app: &mut App,
47
    active_tab: &Tabs,
48
) {
49
    let rows: Vec<Row> = app
50
        .known_networks
51
        .items
52
        .iter()
53
        .map(|net| -> Row<'_> {
54
            Row::new(vec![
55
                Cell::from(net.ssid.clone()),
56
                Cell::from(if net.secured { "Secured" } else { "Open" }),
57
                Cell::from(if app.network_manager.is_connected_cached(&net.ssid) {
58
                    "Connected"
59
                } else {
60
                    "-"
61
                }),
62
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
63
            ])
64
        })
65
        .collect();
66
    draw(
67
        f,
68
        body_chunks[0],
69
        &mut TableData {
70
            title: " Known Networks ",
71
            header_cols: vec!["Name", "Security", "State", "Signal"],
72
            constraint: vec![
73
                Constraint::Percentage(40),
74
                Constraint::Percentage(20),
75
                Constraint::Percentage(20),
76
                Constraint::Percentage(20),
77
            ],
78
            cells: rows,
79
            list: &mut app.known_networks,
80
        },
81
        *active_tab == Tabs::KnownNetworks,
82
    );
83
}
84
85
pub fn draw_known_network(
86
    f: &mut Frame<'_>,
87
    body_chunks: &Rc<[Rect]>,
88
    app: &mut App,
89
    active_tab: &Tabs,
90
) {
91
    let rows: Vec<Row> = app
92
        .known_networks
93
        .items
94
        .iter()
95
        .map(|net| -> Row<'_> {
96
            Row::new(vec![
97
                Cell::from(net.ssid.clone()),
98
                Cell::from(if net.secured { "Secured" } else { "Open" }),
99
                Cell::from(if app.network_manager.is_connected_cached(&net.ssid) {
100
                    "Connected"
101
                } else {
102
                    "-"
103
                }),
104
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
105
            ])
106
        })
107
        .collect();
108
    draw(
109
        f,
110
        body_chunks[0],
111
        &mut TableData {
112
            title: " Known Networks ",
113
            header_cols: vec!["Name", "Security", "State", "Signal"],
114
            constraint: vec![
115
                Constraint::Percentage(40),
116
                Constraint::Percentage(20),
117
                Constraint::Percentage(20),
118
                Constraint::Percentage(20),
119
            ],
120
            cells: rows,
121
            list: &mut app.known_networks,
122
        },
123
        *active_tab == Tabs::KnownNetworks,
124
    );
125
}
126
127
pub fn draw_available_network(
128
    f: &mut Frame<'_>,
129
    body_chunks: &Rc<[Rect]>,
130
    app: &mut App,
131
    active_tab: &Tabs,
132
) {
133
    let rows: Vec<Row> = app
134
        .available_networks
135
        .items
136
        .iter()
137
        .map(|net| -> Row<'_> {
138
            Row::new(vec![
139
                Cell::from(net.ssid.clone()),
140
                Cell::from(if net.secured { "Secured" } else { "Open" }),
141
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
142
            ])
143
        })
144
        .collect();
145
    draw(
146
        f,
147
        body_chunks[1],
148
        &mut TableData {
149
            title: " Available Networks ",
150
            header_cols: vec!["Name", "Security", "Signal"],
151
            constraint: vec![
152
                Constraint::Percentage(40),
153
                Constraint::Percentage(20),
154
                Constraint::Percentage(20),
155
            ],
156
            cells: rows,
157
            list: &mut app.available_networks,
158
        },
159
        *active_tab == Tabs::AvailableNetworks,
160
    );
161
}
162
163
pub fn draw_devices(f: &mut Frame<'_>, body_chunks: &Rc<[Rect]>, app: &mut App, active_tab: &Tabs) {
164
    let rows: Vec<Row> = app
165
        .devices
166
        .items
167
        .iter()
168
        .map(|net| -> Row<'_> {
169
            Row::new(vec![
170
                Cell::from(net.interface.clone()),
171
                Cell::from(if app.network_manager.enabled {
172
                    "On"
173
                } else {
174
                    "Off"
175
                }),
176
            ])
177
        })
178
        .collect();
179
    draw(
180
        f,
181
        body_chunks[2],
182
        &mut TableData {
183
            title: " Device ",
184
            header_cols: vec!["Name", "Powered", "Scanning", "Frequency", "Security"],
185
            constraint: vec![
186
                Constraint::Percentage(20),
187
                Constraint::Percentage(20),
188
                Constraint::Percentage(20),
189
                Constraint::Percentage(20),
190
                Constraint::Percentage(20),
191
            ],
192
            cells: rows,
193
            list: &mut app.devices,
194
        },
195
        *active_tab == Tabs::Devices,
196
    );
197
}