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