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 a5b44283887e04457c71f3da8ef0cc0d3d402324 6285 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,
10
    layout::{Constraint, Rect},
11
    style::{Style, Stylize},
12
    widgets::{Block, Cell, Row, Table},
13
};
14
15
pub struct TableData<'a, T> {
16
    pub title: &'a str,
17
    pub header_cols: Vec<&'a str>,
18
    pub constraint: Vec<Constraint>,
19
    pub cells: Vec<Row<'a>>,
20
    pub list: &'a mut StatefulList<T>,
21
}
22
23
pub fn draw<Any>(f: &mut Frame, area: Rect, table_data: &mut TableData<Any>, is_active: bool) {
24
    let border_style = if is_active {
25
        Style::new().bold().green()
26
    } else {
27
        Style::new()
28
    };
29
    let row_style = if is_active {
30
        Style::new().on_dark_gray()
31
    } else {
32
        Style::new()
33
    };
34
    let table = Table::new(table_data.cells.clone(), table_data.constraint.clone())
35
        .header(Row::new(table_data.header_cols.clone()).bold())
36
        .block(
37
            Block::bordered()
38
                .title(table_data.title)
39
                .border_style(border_style),
40
        )
41
        .row_highlight_style(row_style);
42
43
    f.render_stateful_widget(table, area, &mut table_data.list.state);
44
}
45
46
// TODO: this
47
pub fn draw_saved_connections(
48
    f: &mut Frame<'_>,
49
    body_chunks: &Rc<[Rect]>,
50
    app: &mut App,
51
    active_tab: &Tabs,
52
) {
53
    let rows: Vec<Row> = app
54
        .known_networks
55
        .items
56
        .iter()
57
        .map(|net| -> Row<'_> {
58
            Row::new(vec![
59
                Cell::from(net.ssid.clone()),
60
                Cell::from(if net.secured { "Secured" } else { "Open" }),
61
                Cell::from(if app.network_manager.is_connected_cached(&net.ssid) {
62
                    "Connected"
63
                } else {
64
                    "-"
65
                }),
66
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
67
            ])
68
        })
69
        .collect();
70
    draw(
71
        f,
72
        body_chunks[0],
73
        &mut TableData {
74
            title: " Known Networks ",
75
            header_cols: vec!["Name", "Security", "State", "Signal"],
76
            constraint: vec![
77
                Constraint::Percentage(40),
78
                Constraint::Percentage(20),
79
                Constraint::Percentage(20),
80
                Constraint::Percentage(20),
81
            ],
82
            cells: rows,
83
            list: &mut app.known_networks,
84
        },
85
        *active_tab == Tabs::KnownNetworks,
86
    );
87
}
88
89
pub fn draw_known_network(
90
    f: &mut Frame<'_>,
91
    body_chunks: &Rc<[Rect]>,
92
    app: &mut App,
93
    focus: &Focus,
94
) {
95
    let active = if let Focus::Tab(tab) = focus
96
        && *tab == Tabs::KnownNetworks
97
    {
98
        true
99
    } else {
100
        false
101
    };
102
103
    let rows: Vec<Row> = app
104
        .known_networks
105
        .items
106
        .iter()
107
        .map(|net| -> Row<'_> {
108
            Row::new(vec![
109
                Cell::from(net.ssid.clone()),
110
                Cell::from(if net.secured { "Secured" } else { "Open" }),
111
                Cell::from(if app.network_manager.is_connected_cached(&net.ssid) {
112
                    "Connected"
113
                } else {
114
                    "-"
115
                }),
116
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
117
            ])
118
        })
119
        .collect();
120
    draw(
121
        f,
122
        body_chunks[0],
123
        &mut TableData {
124
            title: " Known Networks ",
125
            header_cols: vec!["Name", "Security", "State", "Signal"],
126
            constraint: vec![
127
                Constraint::Percentage(40),
128
                Constraint::Percentage(20),
129
                Constraint::Percentage(20),
130
                Constraint::Percentage(20),
131
            ],
132
            cells: rows,
133
            list: &mut app.known_networks,
134
        },
135
        active,
136
    );
137
}
138
139
// TODO: make constraint long as it need
140
pub fn draw_available_network(
141
    f: &mut Frame<'_>,
142
    body_chunks: &Rc<[Rect]>,
143
    app: &mut App,
144
    focus: &Focus,
145
) {
146
    let active = if let Focus::Tab(tab) = focus
147
        && *tab == Tabs::AvailableNetworks
148
    {
149
        true
150
    } else {
151
        false
152
    };
153
    let rows: Vec<Row> = app
154
        .available_networks
155
        .items
156
        .iter()
157
        .map(|net| -> Row<'_> {
158
            Row::new(vec![
159
                Cell::from(net.ssid.clone()),
160
                Cell::from(if net.is_psk {
161
                    "psk"
162
                } else if net.is_eap {
163
                    "Enterprise"
164
                } else if !net.secured {
165
                    "Open"
166
                } else {
167
                    "Unknown"
168
                }),
169
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
170
            ])
171
        })
172
        .collect();
173
    draw(
174
        f,
175
        body_chunks[1],
176
        &mut TableData {
177
            title: " Available Networks ",
178
            header_cols: vec!["Name", "Security", "Signal"],
179
            constraint: vec![
180
                Constraint::Percentage(40),
181
                Constraint::Percentage(20),
182
                Constraint::Percentage(20),
183
            ],
184
            cells: rows,
185
            list: &mut app.available_networks,
186
        },
187
        active,
188
    );
189
}
190
191
// TODO: Add cells
192
pub fn draw_devices(f: &mut Frame<'_>, body_chunks: &Rc<[Rect]>, app: &mut App, focus: &Focus) {
193
    let active = if let Focus::Tab(tab) = focus
194
        && *tab == Tabs::Devices
195
    {
196
        true
197
    } else {
198
        false
199
    };
200
    let rows: Vec<Row> = app
201
        .devices
202
        .items
203
        .iter()
204
        .map(|net| -> Row<'_> {
205
            Row::new(vec![
206
                Cell::from(net.interface.clone()),
207
                Cell::from(if app.network_manager.enabled {
208
                    "On"
209
                } else {
210
                    "Off"
211
                }),
212
            ])
213
        })
214
        .collect();
215
    draw(
216
        f,
217
        body_chunks[2],
218
        &mut TableData {
219
            title: " Device ",
220
            header_cols: vec!["Name", "Powered", "Scanning", "Frequency", "Security"],
221
            constraint: vec![
222
                Constraint::Percentage(20),
223
                Constraint::Percentage(20),
224
                Constraint::Percentage(20),
225
                Constraint::Percentage(20),
226
                Constraint::Percentage(20),
227
            ],
228
            cells: rows,
229
            list: &mut app.devices,
230
        },
231
        active,
232
    );
233
}