| 1 |
use std::{hash::Hash, mem}; |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
app::{App, Focus, Tabs}, |
| 5 |
ui::{ |
| 6 |
area::{self, Border}, |
| 7 |
list::StatefulList, |
| 8 |
margin::Margin, |
| 9 |
style_config::StyleConfig, |
| 10 |
}, |
| 11 |
}; |
| 12 |
use nmrs::Network; |
| 13 |
use ratatui::{ |
| 14 |
Frame, |
| 15 |
layout::{Constraint, Flex, Rect}, |
| 16 |
style::Style, |
| 17 |
text::Line, |
| 18 |
widgets::{Block, BorderType, Row, Table}, |
| 19 |
}; |
| 20 |
use serde::{Deserialize, Serialize}; |
| 21 |
|
| 22 |
#[derive(Deserialize, Serialize)] |
| 23 |
pub struct TableConfig<K> { |
| 24 |
pub title: String, |
| 25 |
pub header: Header<K>, |
| 26 |
pub border: Border, |
| 27 |
#[serde(default)] |
| 28 |
pub margin: Margin, |
| 29 |
} |
| 30 |
|
| 31 |
#[derive(Deserialize, Serialize)] |
| 32 |
pub struct Header<K> { |
| 33 |
pub columns: Vec<Column<K>>, |
| 34 |
#[serde(default)] |
| 35 |
pub style_normal: StyleConfig, |
| 36 |
#[serde(default)] |
| 37 |
pub style_active: StyleConfig, |
| 38 |
} |
| 39 |
|
| 40 |
#[derive(Deserialize, Serialize)] |
| 41 |
pub struct Column<K> { |
| 42 |
pub title: String, |
| 43 |
pub value: K, |
| 44 |
pub constraint: Constraint, |
| 45 |
} |
| 46 |
|
| 47 |
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] |
| 48 |
pub enum KnownNetworkColumnKind { |
| 49 |
Name, |
| 50 |
Security, |
| 51 |
State, |
| 52 |
Signal, |
| 53 |
} |
| 54 |
|
| 55 |
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] |
| 56 |
pub enum AvailableNetworkColumnKind { |
| 57 |
Name, |
| 58 |
Security, |
| 59 |
Signal, |
| 60 |
} |
| 61 |
|
| 62 |
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] |
| 63 |
pub enum DeviceColumnKind { |
| 64 |
Name, |
| 65 |
Powered, |
| 66 |
State, |
| 67 |
Frequency, |
| 68 |
Address, |
| 69 |
Security, |
| 70 |
} |
| 71 |
|
| 72 |
pub struct Data<'a, T> { |
| 73 |
pub cells: Vec<Row<'a>>, |
| 74 |
pub list: &'a mut StatefulList<T>, |
| 75 |
} |
| 76 |
|
| 77 |
pub fn network_info<'a>(net: &Network) -> (&'a str, String, &'a str) { |
| 78 |
let security = if net.is_psk { |
| 79 |
"Psk" |
| 80 |
} else if net.is_eap { |
| 81 |
"Enterprise" |
| 82 |
} else if net.secured { |
| 83 |
"Other" |
| 84 |
} else { |
| 85 |
"Open" |
| 86 |
}; |
| 87 |
|
| 88 |
let (strength, bars) = if let Some(strength) = net.strength { |
| 89 |
let bars = if strength > 80 { |
| 90 |
"" |
| 91 |
} else if strength > 60 { |
| 92 |
"" |
| 93 |
} else if strength > 40 { |
| 94 |
"" |
| 95 |
} else if strength > 20 { |
| 96 |
"" |
| 97 |
} else { |
| 98 |
"" |
| 99 |
}; |
| 100 |
(strength.to_string(), bars) |
| 101 |
} else { |
| 102 |
("Unknown".to_string(), "") |
| 103 |
}; |
| 104 |
|
| 105 |
(security, strength, bars) |
| 106 |
} |
| 107 |
|
| 108 |
pub fn draw<Any, K: Eq + Hash>( |
| 109 |
f: &mut Frame, |
| 110 |
area: Rect, |
| 111 |
config: &TableConfig<K>, |
| 112 |
data: &mut Data<Any>, |
| 113 |
is_active: bool, |
| 114 |
) { |
| 115 |
let area = area::fill_rect(area, config.margin); |
| 116 |
|
| 117 |
let border_style: Style = if is_active { |
| 118 |
config.border.style_active.format().unwrap_or_default() |
| 119 |
} else { |
| 120 |
config.border.style_normal.format().unwrap_or_default() |
| 121 |
}; |
| 122 |
|
| 123 |
let border_type: BorderType = if is_active { |
| 124 |
config.border.type_active |
| 125 |
} else { |
| 126 |
config.border.type_normal |
| 127 |
}; |
| 128 |
|
| 129 |
let header_style: Style = if is_active { |
| 130 |
config.header.style_active.format().unwrap_or_default() |
| 131 |
} else { |
| 132 |
config.header.style_normal.format().unwrap_or_default() |
| 133 |
}; |
| 134 |
|
| 135 |
let row_style = if is_active { |
| 136 |
Style::new().on_dark_gray() |
| 137 |
} else { |
| 138 |
Style::new() |
| 139 |
}; |
| 140 |
|
| 141 |
let (columns, constraints): (Vec<Line>, Vec<Constraint>) = config |
| 142 |
.header |
| 143 |
.columns |
| 144 |
.iter() |
| 145 |
.map(|c| (Line::from(c.title.clone()).centered(), c.constraint)) |
| 146 |
.unzip(); |
| 147 |
let table = Table::new(mem::take(&mut data.cells), constraints) |
| 148 |
.header(Row::new(columns).style(header_style).bottom_margin(1)) |
| 149 |
.block( |
| 150 |
Block::bordered() |
| 151 |
.title(config.title.clone()) |
| 152 |
.border_style(border_style) |
| 153 |
.border_type(border_type), |
| 154 |
) |
| 155 |
.flex(Flex::Center) |
| 156 |
.row_highlight_style(row_style); |
| 157 |
|
| 158 |
f.render_stateful_widget(table, area, &mut data.list.state); |
| 159 |
} |
| 160 |
|
| 161 |
pub fn draw_known_network(f: &mut Frame<'_>, area: Rect, app: &mut App) { |
| 162 |
let active = matches!(app.focus, Focus::Tab(Tabs::KnownNetworks)); |
| 163 |
let columns = &app.config.ui.known_networks.header.columns; |
| 164 |
|
| 165 |
let rows: Vec<Row> = app |
| 166 |
.known_networks |
| 167 |
.items |
| 168 |
.iter() |
| 169 |
.map(|net| -> Row<'_> { |
| 170 |
let (security, strength, bars) = network_info(net); |
| 171 |
|
| 172 |
let connected = app |
| 173 |
.network_manager |
| 174 |
.current_network |
| 175 |
.as_ref() |
| 176 |
.is_some_and(|current| current.ssid == net.ssid); |
| 177 |
|
| 178 |
let cells: Vec<Line> = columns |
| 179 |
.iter() |
| 180 |
.map(|c| match c.value { |
| 181 |
KnownNetworkColumnKind::Name => Line::from(net.ssid.clone()), |
| 182 |
KnownNetworkColumnKind::Security => Line::from(security), |
| 183 |
KnownNetworkColumnKind::State => { |
| 184 |
Line::from(if connected { "Connected" } else { "-" }) |
| 185 |
} |
| 186 |
KnownNetworkColumnKind::Signal => Line::from(format!("{}% {}", strength, bars)), |
| 187 |
}) |
| 188 |
.map(|line| line.centered()) |
| 189 |
.collect(); |
| 190 |
|
| 191 |
Row::new(cells) |
| 192 |
}) |
| 193 |
.collect(); |
| 194 |
draw( |
| 195 |
f, |
| 196 |
area, |
| 197 |
&app.config.ui.known_networks, |
| 198 |
&mut Data { |
| 199 |
cells: rows, |
| 200 |
list: &mut app.known_networks, |
| 201 |
}, |
| 202 |
active, |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
pub fn draw_available_network(f: &mut Frame<'_>, area: Rect, app: &mut App) { |
| 207 |
let active = matches!(app.focus, Focus::Tab(Tabs::AvailableNetworks)); |
| 208 |
let columns = &app.config.ui.available_networks.header.columns; |
| 209 |
|
| 210 |
let rows: Vec<Row> = app |
| 211 |
.available_networks |
| 212 |
.items |
| 213 |
.iter() |
| 214 |
.map(|net| -> Row<'_> { |
| 215 |
let (security, strength, bars) = network_info(net); |
| 216 |
|
| 217 |
let cells: Vec<Line> = columns |
| 218 |
.iter() |
| 219 |
.map(|c| match c.value { |
| 220 |
AvailableNetworkColumnKind::Name => Line::from(net.ssid.clone()), |
| 221 |
AvailableNetworkColumnKind::Security => Line::from(security), |
| 222 |
AvailableNetworkColumnKind::Signal => { |
| 223 |
Line::from(format!("{}% {}", strength, bars)) |
| 224 |
} |
| 225 |
}) |
| 226 |
.map(|line| line.centered()) |
| 227 |
.collect(); |
| 228 |
|
| 229 |
Row::new(cells) |
| 230 |
}) |
| 231 |
.collect(); |
| 232 |
draw( |
| 233 |
f, |
| 234 |
area, |
| 235 |
&app.config.ui.available_networks, |
| 236 |
&mut Data { |
| 237 |
cells: rows, |
| 238 |
list: &mut app.available_networks, |
| 239 |
}, |
| 240 |
active, |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
pub fn draw_devices(f: &mut Frame<'_>, area: Rect, app: &mut App) { |
| 245 |
let active = matches!(app.focus, Focus::Tab(Tabs::Devices)); |
| 246 |
let columns = &app.config.ui.devices.header.columns; |
| 247 |
|
| 248 |
let security: String = app |
| 249 |
.network_manager |
| 250 |
.current_network_info |
| 251 |
.as_ref() |
| 252 |
.map(|info| info.security.clone()) |
| 253 |
.unwrap_or_else(|| "-".to_string()); |
| 254 |
|
| 255 |
let rows: Vec<Row> = app |
| 256 |
.devices |
| 257 |
.items |
| 258 |
.iter() |
| 259 |
.map(|dev| -> Row<'_> { |
| 260 |
let cells: Vec<Line> = columns |
| 261 |
.iter() |
| 262 |
.map(|c| match c.value { |
| 263 |
DeviceColumnKind::Name => Line::from(dev.interface.clone()), |
| 264 |
DeviceColumnKind::Powered => { |
| 265 |
Line::from(if dev.state.is_enabled() { "On" } else { "Off" }) |
| 266 |
} |
| 267 |
DeviceColumnKind::State => Line::from(format!("{}", dev.state)), |
| 268 |
DeviceColumnKind::Frequency => { |
| 269 |
Line::from(format!("{} MHz", dev.active_frequency_mhz.unwrap_or(0))) |
| 270 |
} |
| 271 |
DeviceColumnKind::Address => Line::from(dev.hw_address.to_string()), |
| 272 |
DeviceColumnKind::Security => Line::from(security.clone()), |
| 273 |
}) |
| 274 |
.map(|line| line.centered()) |
| 275 |
.collect(); |
| 276 |
|
| 277 |
Row::new(cells) |
| 278 |
}) |
| 279 |
.collect(); |
| 280 |
draw( |
| 281 |
f, |
| 282 |
area, |
| 283 |
&app.config.ui.devices, |
| 284 |
&mut Data { |
| 285 |
cells: rows, |
| 286 |
list: &mut app.devices, |
| 287 |
}, |
| 288 |
active, |
| 289 |
); |
| 290 |
} |