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 478a319309e04ba2b341e709d4ea05071af67014 9175 bytes (plain)
1
use std::rc::Rc;
2
3
use crate::{
4
    app::App,
5
    tui::{Focus, Tabs},
6
    ui::list::StatefulList,
7
};
8
use ratatui::{
9
    Frame, layout::{Constraint, Flex, Rect}, style::Style, text::Line, widgets::{Block, BorderType, Cell, Row, Table}
10
};
11
12
pub struct TableData<'a, T> {
13
    pub title: &'static str,
14
    pub header_cols: Vec<Line<'a>>,
15
    pub constraint: Vec<Constraint>,
16
    pub cells: Vec<Row<'a>>,
17
    pub list: &'a mut StatefulList<T>,
18
}
19
20
pub fn draw<Any>(f: &mut Frame, area: Rect, table_data: &mut TableData<Any>, is_active: bool) {
21
    let border_style = if is_active {
22
        Style::new().bold().green()
23
    } else {
24
        Style::new()
25
    };
26
27
    let border_type = if is_active {
28
        BorderType::Thick
29
    } else {
30
        BorderType::Plain
31
    };
32
33
    let header_style = if is_active {
34
        Style::new().bold().yellow()
35
    } else {
36
        Style::new()
37
    };
38
39
    let row_style = if is_active {
40
        Style::new().on_dark_gray()
41
    } else {
42
        Style::new()
43
    };
44
    let table = Table::new(table_data.cells.clone(), table_data.constraint.clone())
45
        .header(Row::new(table_data.header_cols.clone()).style(header_style).bottom_margin(1))
46
        .block(
47
            Block::bordered()
48
                .title(table_data.title)
49
                .border_style(border_style)
50
                .border_type(border_type),
51
        )
52
        .flex(Flex::Center)
53
        .row_highlight_style(row_style);
54
55
    f.render_stateful_widget(table, area, &mut table_data.list.state);
56
}
57
58
// TODO: this
59
pub fn draw_saved_connections(
60
    f: &mut Frame<'_>,
61
    body_chunks: &Rc<[Rect]>,
62
    app: &mut App,
63
    active_tab: &Tabs,
64
) {
65
    let rows: Vec<Row> = app
66
        .known_networks
67
        .items
68
        .iter()
69
        .map(|net| -> Row<'_> {
70
            Row::new(vec![
71
                Line::from(net.ssid.clone()).centered(),
72
                Line::from(if net.secured { "Secured" } else { "Open" }).centered(),
73
                Line::from(if app.network_manager.is_connected_cached(&net.ssid) {
74
                    "Connected"
75
                } else {
76
                    "-"
77
                }).centered(),
78
                Line::from(format!("{}%", net.strength.unwrap_or(0))).centered(),
79
            ])
80
        })
81
        .collect();
82
    draw(
83
        f,
84
        body_chunks[0],
85
        &mut TableData {
86
            title: " Known Networks ",
87
            header_cols: vec![
88
                Line::from("Name").centered(),
89
                Line::from("Security").centered(),
90
                Line::from("State").centered(),
91
                Line::from("Signal").centered()
92
            ],
93
            constraint: vec![
94
                Constraint::Percentage(40),
95
                Constraint::Percentage(20),
96
                Constraint::Percentage(20),
97
                Constraint::Percentage(20),
98
            ],
99
            cells: rows,
100
            list: &mut app.known_networks,
101
        },
102
        *active_tab == Tabs::KnownNetworks,
103
    );
104
}
105
106
pub fn draw_known_network(
107
    f: &mut Frame<'_>,
108
    body_chunks: &Rc<[Rect]>,
109
    app: &mut App,
110
    focus: &Focus,
111
) {
112
    let active = if let Focus::Tab(tab) = focus
113
        && *tab == Tabs::KnownNetworks
114
    {
115
        true
116
    } else {
117
        false
118
    };
119
120
    let rows: Vec<Row> = app
121
        .known_networks
122
        .items
123
        .iter()
124
        .map(|net| -> Row<'_> {
125
            let security = if net.is_psk {
126
                "Psk"
127
            } else if net.is_eap {
128
                "Enterprise"
129
            } else if net.secured {
130
                "Other"
131
            } else {
132
                "Open"
133
            };
134
135
            let (strength, bars) = if let Some(strength) = net.strength {
136
                let bars = if strength > 80 {
137
                    "󰤨"
138
                } else if strength > 60 {
139
                    "󰤥"
140
                } else if strength > 40 {
141
                    "󰤢"
142
                } else if strength > 20 {
143
                    "󰤟"
144
                } else {
145
                    "󰤯"
146
                };
147
                (strength.to_string(), bars)
148
            } else {
149
                ("Unknown".to_string(), "󰤯")
150
            };
151
152
            Row::new(vec![
153
                Line::from(net.ssid.clone()).centered(),
154
                Line::from(security).centered(),
155
                Line::from(if app.network_manager.is_connected_cached(&net.ssid) {
156
                    "Connected"
157
                } else {
158
                    "-"
159
                }).centered(),
160
                Line::from(format!("{}% {}", strength, bars)).centered(),
161
            ])
162
        })
163
        .collect();
164
    draw(
165
        f,
166
        body_chunks[0],
167
        &mut TableData {
168
            title: " Known Networks ",
169
            header_cols: vec![
170
                Line::from("Name").centered(),
171
                Line::from("Security").centered(),
172
                Line::from("State").centered(),
173
                Line::from("Signal").centered()
174
            ],
175
            constraint: vec![
176
                Constraint::Percentage(40),
177
                Constraint::Percentage(20),
178
                Constraint::Percentage(20),
179
                Constraint::Percentage(20),
180
            ],
181
            cells: rows,
182
            list: &mut app.known_networks,
183
        },
184
        active,
185
    );
186
}
187
188
pub fn draw_available_network(
189
    f: &mut Frame<'_>,
190
    body_chunks: &Rc<[Rect]>,
191
    app: &mut App,
192
    focus: &Focus,
193
) {
194
    let active = if let Focus::Tab(tab) = focus
195
        && *tab == Tabs::AvailableNetworks
196
    {
197
        true
198
    } else {
199
        false
200
    };
201
    let rows: Vec<Row> = app
202
        .available_networks
203
        .items
204
        .iter()
205
        .map(|net| -> Row<'_> {
206
            let security = if net.is_psk {
207
                "Psk"
208
            } else if net.is_eap {
209
                "Enterprise"
210
            } else if net.secured {
211
                "Other"
212
            } else {
213
                "Open"
214
            };
215
216
            let (strength, bars) = if let Some(strength) = net.strength {
217
                let bars = if strength > 80 {
218
                    "󰤨"
219
                } else if strength > 60 {
220
                    "󰤥"
221
                } else if strength > 40 {
222
                    "󰤢"
223
                } else if strength > 20 {
224
                    "󰤟"
225
                } else {
226
                    "󰤯"
227
                };
228
                (strength.to_string(), bars)
229
            } else {
230
                ("Unknown".to_string(), "󰤯")
231
            };
232
233
            Row::new(vec![
234
                Line::from(net.ssid.clone()).centered(),
235
                Line::from(security).centered(),
236
                Line::from(format!("{}% {}", strength, bars)).centered(),
237
            ])
238
        })
239
        .collect();
240
    draw(
241
        f,
242
        body_chunks[1],
243
        &mut TableData {
244
            title: " Available Networks ",
245
            header_cols: vec![
246
                Line::from("Name").centered(),
247
                Line::from("Security").centered(),
248
                Line::from("Signal").centered()
249
            ],
250
            constraint: vec![
251
                Constraint::Percentage(40),
252
                Constraint::Percentage(20),
253
                Constraint::Percentage(20),
254
            ],
255
            cells: rows,
256
            list: &mut app.available_networks,
257
        },
258
        active,
259
    );
260
}
261
262
pub fn draw_devices(f: &mut Frame<'_>, body_chunks: &Rc<[Rect]>, app: &mut App, focus: &Focus) {
263
    let active = if let Focus::Tab(tab) = focus
264
        && *tab == Tabs::Devices
265
    {
266
        true
267
    } else {
268
        false
269
    };
270
    let rows: Vec<Row> = app
271
        .devices
272
        .items
273
        .iter()
274
        .map(|dev| -> Row<'_> {
275
            let freq = if let Some(current_connection) = &app.network_manager.current_connection {
276
                format!("{} MHz", current_connection.frequency.unwrap_or_default())
277
            } else {
278
                "-".to_string()
279
            };
280
281
            Row::new(vec![
282
                Line::from(dev.interface.clone()).centered(),
283
                Line::from(if app.network_manager.enabled {
284
                    "On"
285
                } else {
286
                    "Off"
287
                }).centered(),
288
                Line::from(format!("{}", dev.state)).centered(),
289
                Line::from(freq).centered(),
290
                Line::from(dev.hw_address.to_string()).centered(),
291
            ])
292
        })
293
        .collect();
294
    draw(
295
        f,
296
        body_chunks[2],
297
        &mut TableData {
298
            title: " Device ",
299
            header_cols: vec![
300
                Line::from("Name").centered(),
301
                Line::from("Powered").centered(),
302
                Line::from("State").centered(),
303
                Line::from("Frequency").centered(),
304
                Line::from("Address").centered(),
305
                Line::from("Security").centered(),
306
            ],
307
            constraint: vec![
308
                Constraint::Percentage(20),
309
                Constraint::Percentage(20),
310
                Constraint::Percentage(20),
311
                Constraint::Percentage(20),
312
                Constraint::Percentage(20),
313
            ],
314
            cells: rows,
315
            list: &mut app.devices,
316
        },
317
        active,
318
    );
319
}