| 1 |
use anyhow::{Ok, Result}; |
| 2 |
use crossterm::{ |
| 3 |
ExecutableCommand, |
| 4 |
event::{self, Event, KeyCode}, |
| 5 |
terminal::{self, disable_raw_mode, enable_raw_mode}, |
| 6 |
}; |
| 7 |
use nmrs::Network; |
| 8 |
use ratatui::{ |
| 9 |
Frame, Terminal, |
| 10 |
backend::CrosstermBackend, |
| 11 |
layout::{Constraint, Direction, Layout}, |
| 12 |
widgets::Cell, |
| 13 |
}; |
| 14 |
use ratatui::{layout::Rect, widgets::Row}; |
| 15 |
use std::{io::stdout, rc::Rc}; |
| 16 |
|
| 17 |
use crate::{ |
| 18 |
app::{App, InputMode}, |
| 19 |
ui::{ |
| 20 |
list::StatefulList, |
| 21 |
table::{self, TableData}, |
| 22 |
}, |
| 23 |
}; |
| 24 |
|
| 25 |
#[derive(PartialEq)] |
| 26 |
pub enum Tabs { |
| 27 |
KnownNetworks, |
| 28 |
AvailableNetworks, |
| 29 |
Devices, |
| 30 |
} |
| 31 |
|
| 32 |
pub struct Tui { |
| 33 |
pub terminal: Terminal<CrosstermBackend<std::io::Stdout>>, |
| 34 |
|
| 35 |
pub active_tab: Tabs, |
| 36 |
pub selected_network: Option<Network>, |
| 37 |
} |
| 38 |
|
| 39 |
impl<'a> Tui { |
| 40 |
pub fn new(app: &mut App) -> anyhow::Result<Self> { |
| 41 |
let mut stdout = stdout(); |
| 42 |
enable_raw_mode()?; |
| 43 |
stdout |
| 44 |
.execute(terminal::EnterAlternateScreen)? |
| 45 |
.execute(terminal::Clear(terminal::ClearType::All))?; |
| 46 |
|
| 47 |
let backend = CrosstermBackend::new(stdout); |
| 48 |
let terminal = Terminal::new(backend)?; |
| 49 |
|
| 50 |
if !app.new_networks.items.is_empty() { |
| 51 |
app.new_networks.state.select(Some(0)); |
| 52 |
} |
| 53 |
if !app.known_networks.items.is_empty() { |
| 54 |
app.known_networks.state.select(Some(0)); |
| 55 |
} |
| 56 |
if !app.devices.items.is_empty() { |
| 57 |
app.devices.state.select(Some(0)); |
| 58 |
} |
| 59 |
|
| 60 |
Ok(Tui { |
| 61 |
terminal, |
| 62 |
selected_network: Self::selected_network(&app.new_networks), |
| 63 |
active_tab: Tabs::KnownNetworks, |
| 64 |
}) |
| 65 |
} |
| 66 |
|
| 67 |
fn selected_network(list: &StatefulList<Network>) -> Option<Network> { |
| 68 |
list.state |
| 69 |
.selected() |
| 70 |
.and_then(|index| list.items.get(index)) |
| 71 |
.cloned() |
| 72 |
} |
| 73 |
|
| 74 |
pub async fn run( |
| 75 |
&mut self, |
| 76 |
app: &mut App<'a>, |
| 77 |
mut terminal: Terminal<CrosstermBackend<std::io::Stdout>>, |
| 78 |
) -> Result<()> { |
| 79 |
while !app.should_quit { |
| 80 |
terminal.draw(|f| { |
| 81 |
let size = f.area(); |
| 82 |
|
| 83 |
let main_chunks = Layout::default() |
| 84 |
.direction(Direction::Vertical) |
| 85 |
.constraints([Constraint::Length(1), Constraint::Min(0)]) |
| 86 |
.split(size); |
| 87 |
|
| 88 |
let body_chunks = Layout::new( |
| 89 |
Direction::Vertical, |
| 90 |
[ |
| 91 |
Constraint::Percentage(40), |
| 92 |
Constraint::Percentage(40), |
| 93 |
Constraint::Percentage(20), |
| 94 |
], |
| 95 |
) |
| 96 |
.split(main_chunks[1]); |
| 97 |
|
| 98 |
self.render_all_tables(f, body_chunks, app); |
| 99 |
|
| 100 |
if app.input_mode == InputMode::Editing { |
| 101 |
self.render_password_popup(f, app); |
| 102 |
} |
| 103 |
|
| 104 |
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { |
| 105 |
let popup_layout = Layout::default() |
| 106 |
.direction(Direction::Vertical) |
| 107 |
.constraints([ |
| 108 |
Constraint::Percentage((100 - percent_y) / 2), |
| 109 |
Constraint::Percentage(percent_y), |
| 110 |
Constraint::Percentage((100 - percent_y) / 2), |
| 111 |
]) |
| 112 |
.split(r); |
| 113 |
|
| 114 |
Layout::default() |
| 115 |
.direction(Direction::Horizontal) |
| 116 |
.constraints([ |
| 117 |
Constraint::Percentage((100 - percent_x) / 2), |
| 118 |
Constraint::Percentage(percent_x), |
| 119 |
Constraint::Percentage((100 - percent_x) / 2), |
| 120 |
]) |
| 121 |
.split(popup_layout[1])[1] |
| 122 |
} |
| 123 |
|
| 124 |
if app.input_mode == InputMode::Editing { |
| 125 |
let area = centered_rect(60, 20, f.area()); |
| 126 |
let block = ratatui::widgets::Block::bordered() |
| 127 |
.title(" Enter Password ") |
| 128 |
.style(ratatui::style::Style::default().fg(ratatui::style::Color::Yellow)); |
| 129 |
let text = |
| 130 |
ratatui::widgets::Paragraph::new(app.password_input.clone()).block(block); |
| 131 |
f.render_widget(ratatui::widgets::Clear, area); |
| 132 |
f.render_widget(text, area); |
| 133 |
} |
| 134 |
})?; |
| 135 |
|
| 136 |
if event::poll(std::time::Duration::from_millis(200))? |
| 137 |
&& let Event::Key(key) = event::read()? |
| 138 |
{ |
| 139 |
match app.input_mode { |
| 140 |
InputMode::Normal => match key.code { |
| 141 |
KeyCode::Char('q') => app.quit(), |
| 142 |
KeyCode::Tab | KeyCode::Char('l') => { |
| 143 |
self.active_tab = match self.active_tab { |
| 144 |
Tabs::KnownNetworks => Tabs::AvailableNetworks, |
| 145 |
Tabs::AvailableNetworks => Tabs::Devices, |
| 146 |
Tabs::Devices => Tabs::KnownNetworks, |
| 147 |
}; |
| 148 |
} |
| 149 |
KeyCode::BackTab | KeyCode::Char('h') => { |
| 150 |
self.active_tab = match self.active_tab { |
| 151 |
Tabs::KnownNetworks => Tabs::Devices, |
| 152 |
Tabs::AvailableNetworks => Tabs::KnownNetworks, |
| 153 |
Tabs::Devices => Tabs::AvailableNetworks, |
| 154 |
}; |
| 155 |
} |
| 156 |
KeyCode::Char('j') | KeyCode::Down => match self.active_tab { |
| 157 |
Tabs::Devices => app.devices.next(), |
| 158 |
Tabs::AvailableNetworks => app.new_networks.next(), |
| 159 |
Tabs::KnownNetworks => app.known_networks.next(), |
| 160 |
}, |
| 161 |
KeyCode::Char('k') | KeyCode::Up => match self.active_tab { |
| 162 |
Tabs::Devices => app.devices.previous(), |
| 163 |
Tabs::AvailableNetworks => app.new_networks.previous(), |
| 164 |
Tabs::KnownNetworks => app.known_networks.previous(), |
| 165 |
}, |
| 166 |
KeyCode::Char('r') => match self.active_tab { |
| 167 |
Tabs::AvailableNetworks | Tabs::KnownNetworks => app.scan_networks().await?, |
| 168 |
_ => {} |
| 169 |
} |
| 170 |
KeyCode::Enter => { |
| 171 |
if self.active_tab == Tabs::AvailableNetworks |
| 172 |
&& let Some(network) = Self::selected_network(&app.new_networks) |
| 173 |
{ |
| 174 |
self.selected_network = Some(network.clone()); |
| 175 |
if network.is_psk { |
| 176 |
app.password_input.clear(); |
| 177 |
app.input_mode = InputMode::Editing |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
KeyCode::Char('f') => { |
| 182 |
if self.active_tab == Tabs::KnownNetworks { |
| 183 |
self.selected_network = Self::selected_network(&app.known_networks); |
| 184 |
app.network_manager |
| 185 |
.forget(&self.selected_network.clone().unwrap().ssid) |
| 186 |
.await?; |
| 187 |
} |
| 188 |
} |
| 189 |
_ => {} |
| 190 |
}, |
| 191 |
InputMode::Editing => match key.code { |
| 192 |
KeyCode::Enter => { |
| 193 |
app.input_mode = InputMode::Normal; |
| 194 |
app.network_manager |
| 195 |
.connect( |
| 196 |
&self.selected_network.clone().unwrap().ssid, |
| 197 |
&app.password_input, |
| 198 |
) |
| 199 |
.await?; |
| 200 |
app.password_input.clear(); |
| 201 |
} |
| 202 |
KeyCode::Esc => { |
| 203 |
app.input_mode = InputMode::Normal; |
| 204 |
} |
| 205 |
KeyCode::Char(c) => { |
| 206 |
app.password_input.push(c); |
| 207 |
} |
| 208 |
KeyCode::Backspace => { |
| 209 |
app.password_input.pop(); |
| 210 |
} |
| 211 |
_ => {} |
| 212 |
}, |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
self.cleanup()?; |
| 218 |
Ok(()) |
| 219 |
} |
| 220 |
|
| 221 |
fn render_password_popup(&self, f: &mut ratatui::Frame, app: &App) { |
| 222 |
let area = self.centered_rect(50, 3, f.area()); |
| 223 |
f.render_widget(ratatui::widgets::Clear, area); |
| 224 |
|
| 225 |
|
| 226 |
let display_pass: String = app.password_input.chars().map(|_| '*').collect(); |
| 227 |
|
| 228 |
let block = ratatui::widgets::Block::bordered() |
| 229 |
.title(format!( |
| 230 |
" Password for {} ", |
| 231 |
self.selected_network |
| 232 |
.as_ref() |
| 233 |
.map(|n| n.ssid.as_str()) |
| 234 |
.unwrap_or("Network") |
| 235 |
)) |
| 236 |
.border_style(ratatui::style::Style::default().fg(ratatui::style::Color::Yellow)); |
| 237 |
|
| 238 |
let text = ratatui::widgets::Paragraph::new(display_pass).block(block); |
| 239 |
f.render_widget(text, area); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
fn centered_rect(&self, percent_x: u16, height: u16, r: Rect) -> Rect { |
| 244 |
let popup_layout = Layout::default() |
| 245 |
.direction(Direction::Vertical) |
| 246 |
.constraints([ |
| 247 |
Constraint::Length((r.height.saturating_sub(height)) / 2), |
| 248 |
Constraint::Length(height), |
| 249 |
Constraint::Min(0), |
| 250 |
]) |
| 251 |
.split(r); |
| 252 |
|
| 253 |
Layout::default() |
| 254 |
.direction(Direction::Horizontal) |
| 255 |
.constraints([ |
| 256 |
Constraint::Percentage((100 - percent_x) / 2), |
| 257 |
Constraint::Percentage(percent_x), |
| 258 |
Constraint::Percentage((100 - percent_x) / 2), |
| 259 |
]) |
| 260 |
.split(popup_layout[1])[1] |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
fn cleanup(&mut self) -> Result<()> { |
| 265 |
disable_raw_mode()?; |
| 266 |
self.terminal |
| 267 |
.backend_mut() |
| 268 |
.execute(terminal::LeaveAlternateScreen)?; |
| 269 |
self.terminal.show_cursor()?; |
| 270 |
Ok(()) |
| 271 |
} |
| 272 |
|
| 273 |
fn render_all_tables(&self, f: &mut Frame, body_chunks: Rc<[Rect]>, app: &mut App<'a>) { |
| 274 |
let rows: Vec<Row> = app |
| 275 |
.known_networks |
| 276 |
.items |
| 277 |
.iter() |
| 278 |
.map(|net| -> Row<'_> { |
| 279 |
Row::new(vec![ |
| 280 |
Cell::from(net.ssid.clone()), |
| 281 |
Cell::from(if net.secured { "Secured" } else { "Open" }), |
| 282 |
Cell::from(if app.network_manager.is_connected(&net.ssid) { |
| 283 |
"Connected" |
| 284 |
} else { |
| 285 |
"-" |
| 286 |
}), |
| 287 |
Cell::from(format!("{}%", net.strength.unwrap_or(0))), |
| 288 |
]) |
| 289 |
}) |
| 290 |
.collect(); |
| 291 |
table::draw( |
| 292 |
f, |
| 293 |
body_chunks[0], |
| 294 |
&mut TableData { |
| 295 |
title: " Known Networks ", |
| 296 |
header_cols: vec!["Name", "Security", "State", "Signal"], |
| 297 |
constraint: vec![ |
| 298 |
Constraint::Percentage(40), |
| 299 |
Constraint::Percentage(20), |
| 300 |
Constraint::Percentage(20), |
| 301 |
Constraint::Percentage(20), |
| 302 |
], |
| 303 |
cells: rows, |
| 304 |
list: &mut app.known_networks, |
| 305 |
}, |
| 306 |
self.active_tab == Tabs::KnownNetworks, |
| 307 |
); |
| 308 |
let rows: Vec<Row> = app |
| 309 |
.new_networks |
| 310 |
.items |
| 311 |
.iter() |
| 312 |
.map(|net| -> Row<'_> { |
| 313 |
Row::new(vec![ |
| 314 |
Cell::from(net.ssid.clone()), |
| 315 |
Cell::from(if net.secured { "Secured" } else { "Open" }), |
| 316 |
Cell::from(format!("{}%", net.strength.unwrap_or(0))), |
| 317 |
]) |
| 318 |
}) |
| 319 |
.collect(); |
| 320 |
table::draw( |
| 321 |
f, |
| 322 |
body_chunks[1], |
| 323 |
&mut TableData { |
| 324 |
title: " Available Networks ", |
| 325 |
header_cols: vec!["Name", "Security", "Signal"], |
| 326 |
constraint: vec![ |
| 327 |
Constraint::Percentage(40), |
| 328 |
Constraint::Percentage(20), |
| 329 |
Constraint::Percentage(20), |
| 330 |
], |
| 331 |
cells: rows, |
| 332 |
list: &mut app.new_networks, |
| 333 |
}, |
| 334 |
self.active_tab == Tabs::AvailableNetworks, |
| 335 |
); |
| 336 |
|
| 337 |
let rows: Vec<Row> = app |
| 338 |
.devices |
| 339 |
.items |
| 340 |
.iter() |
| 341 |
.map(|net| -> Row<'_> { |
| 342 |
Row::new(vec![ |
| 343 |
Cell::from(net.interface.clone()), |
| 344 |
Cell::from(if app.network_manager.enabled { |
| 345 |
"On" |
| 346 |
} else { |
| 347 |
"Off" |
| 348 |
}), |
| 349 |
|
| 350 |
]) |
| 351 |
}) |
| 352 |
.collect(); |
| 353 |
table::draw( |
| 354 |
f, |
| 355 |
body_chunks[2], |
| 356 |
&mut TableData { |
| 357 |
title: " Device ", |
| 358 |
header_cols: vec!["Name", "Powered", "Scanning", "Frequency", "Security"], |
| 359 |
constraint: vec![ |
| 360 |
Constraint::Percentage(20), |
| 361 |
Constraint::Percentage(20), |
| 362 |
Constraint::Percentage(20), |
| 363 |
Constraint::Percentage(20), |
| 364 |
Constraint::Percentage(20), |
| 365 |
], |
| 366 |
cells: rows, |
| 367 |
list: &mut app.devices, |
| 368 |
}, |
| 369 |
self.active_tab == Tabs::Devices, |
| 370 |
); |
| 371 |
} |
| 372 |
} |