| 1 |
use anyhow::{Ok, Result}; |
| 2 |
use crossterm::{ |
| 3 |
ExecutableCommand, |
| 4 |
event::{self, Event}, |
| 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, time::Duration}; |
| 16 |
|
| 17 |
use crate::{ |
| 18 |
app::{App, InputMode}, events, ui::{ |
| 19 |
list::StatefulList, |
| 20 |
table::{self, TableData}, |
| 21 |
} |
| 22 |
}; |
| 23 |
|
| 24 |
#[derive(PartialEq)] |
| 25 |
pub enum Tabs { |
| 26 |
KnownNetworks, |
| 27 |
AvailableNetworks, |
| 28 |
Devices, |
| 29 |
} |
| 30 |
|
| 31 |
pub struct Tui { |
| 32 |
pub terminal: Terminal<CrosstermBackend<std::io::Stdout>>, |
| 33 |
|
| 34 |
pub active_tab: Tabs, |
| 35 |
pub selected_network: Option<Network>, |
| 36 |
} |
| 37 |
|
| 38 |
impl<'a> Tui { |
| 39 |
pub fn new(app: &mut App) -> anyhow::Result<Self> { |
| 40 |
let mut stdout = stdout(); |
| 41 |
enable_raw_mode()?; |
| 42 |
stdout |
| 43 |
.execute(terminal::EnterAlternateScreen)? |
| 44 |
.execute(terminal::Clear(terminal::ClearType::All))?; |
| 45 |
|
| 46 |
let backend = CrosstermBackend::new(stdout); |
| 47 |
let terminal = Terminal::new(backend)?; |
| 48 |
|
| 49 |
if !app.available_networks.items.is_empty() { |
| 50 |
app.available_networks.state.select(Some(0)); |
| 51 |
} |
| 52 |
if !app.known_networks.items.is_empty() { |
| 53 |
app.known_networks.state.select(Some(0)); |
| 54 |
} |
| 55 |
if !app.devices.items.is_empty() { |
| 56 |
app.devices.state.select(Some(0)); |
| 57 |
} |
| 58 |
|
| 59 |
Ok(Tui { |
| 60 |
terminal, |
| 61 |
|
| 62 |
selected_network: Self::selected_network(&app.available_networks), |
| 63 |
active_tab: Tabs::KnownNetworks, |
| 64 |
}) |
| 65 |
} |
| 66 |
|
| 67 |
pub 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, |
| 77 |
mut terminal: Terminal<CrosstermBackend<std::io::Stdout>>, |
| 78 |
) -> Result<()> { |
| 79 |
|
| 80 |
let mut last_tick = tokio::time::Instant::now(); |
| 81 |
let tick_rate = Duration::from_secs(3); |
| 82 |
|
| 83 |
while !app.should_quit { |
| 84 |
if last_tick.elapsed() >= tick_rate { |
| 85 |
if let Err(e) = app.refresh_networks().await { |
| 86 |
eprintln!("scan error: {:?}", e); |
| 87 |
} |
| 88 |
last_tick = tokio::time::Instant::now(); |
| 89 |
} |
| 90 |
terminal.draw(|f| { |
| 91 |
|
| 92 |
|
| 93 |
let size = f.area(); |
| 94 |
|
| 95 |
let main_chunks = Layout::default() |
| 96 |
.direction(Direction::Vertical) |
| 97 |
.constraints([Constraint::Length(1), Constraint::Min(0)]) |
| 98 |
.split(size); |
| 99 |
|
| 100 |
let body_chunks = Layout::new( |
| 101 |
Direction::Vertical, |
| 102 |
[ |
| 103 |
Constraint::Percentage(40), |
| 104 |
Constraint::Percentage(40), |
| 105 |
Constraint::Percentage(20), |
| 106 |
], |
| 107 |
) |
| 108 |
.split(main_chunks[1]); |
| 109 |
|
| 110 |
self.render_all_tables(f, body_chunks, app); |
| 111 |
|
| 112 |
if app.input_mode == InputMode::Editing { |
| 113 |
self.render_password_popup(f, app); |
| 114 |
} |
| 115 |
|
| 116 |
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { |
| 117 |
let popup_layout = Layout::default() |
| 118 |
.direction(Direction::Vertical) |
| 119 |
.constraints([ |
| 120 |
Constraint::Percentage((100 - percent_y) / 2), |
| 121 |
Constraint::Percentage(percent_y), |
| 122 |
Constraint::Percentage((100 - percent_y) / 2), |
| 123 |
]) |
| 124 |
.split(r); |
| 125 |
|
| 126 |
Layout::default() |
| 127 |
.direction(Direction::Horizontal) |
| 128 |
.constraints([ |
| 129 |
Constraint::Percentage((100 - percent_x) / 2), |
| 130 |
Constraint::Percentage(percent_x), |
| 131 |
Constraint::Percentage((100 - percent_x) / 2), |
| 132 |
]) |
| 133 |
.split(popup_layout[1])[1] |
| 134 |
} |
| 135 |
|
| 136 |
if app.input_mode == InputMode::Editing { |
| 137 |
let area = centered_rect(60, 20, f.area()); |
| 138 |
let block = ratatui::widgets::Block::bordered() |
| 139 |
.title(" Enter Password ") |
| 140 |
.style(ratatui::style::Style::default().fg(ratatui::style::Color::Yellow)); |
| 141 |
let text = |
| 142 |
ratatui::widgets::Paragraph::new(app.password_input.clone()).block(block); |
| 143 |
f.render_widget(ratatui::widgets::Clear, area); |
| 144 |
f.render_widget(text, area); |
| 145 |
} |
| 146 |
})?; |
| 147 |
|
| 148 |
if event::poll(std::time::Duration::from_millis(200))? |
| 149 |
&& let Event::Key(key) = event::read()? |
| 150 |
{ |
| 151 |
events::handle_events(app, self, key).await?; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
self.cleanup()?; |
| 156 |
Ok(()) |
| 157 |
} |
| 158 |
|
| 159 |
fn render_password_popup(&self, f: &mut ratatui::Frame, app: &App) { |
| 160 |
let area = self.centered_rect(50, 3, f.area()); |
| 161 |
f.render_widget(ratatui::widgets::Clear, area); |
| 162 |
|
| 163 |
|
| 164 |
let display_pass: String = app.password_input.chars().map(|_| '*').collect(); |
| 165 |
|
| 166 |
let block = ratatui::widgets::Block::bordered() |
| 167 |
.title(format!( |
| 168 |
" Password for {} ", |
| 169 |
self.selected_network |
| 170 |
.as_ref() |
| 171 |
.map(|n| n.ssid.as_str()) |
| 172 |
.unwrap_or("Network") |
| 173 |
)) |
| 174 |
.border_style(ratatui::style::Style::default().fg(ratatui::style::Color::Yellow)); |
| 175 |
|
| 176 |
let text = ratatui::widgets::Paragraph::new(display_pass).block(block); |
| 177 |
f.render_widget(text, area); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
fn centered_rect(&self, percent_x: u16, height: u16, r: Rect) -> Rect { |
| 182 |
let popup_layout = Layout::default() |
| 183 |
.direction(Direction::Vertical) |
| 184 |
.constraints([ |
| 185 |
Constraint::Length((r.height.saturating_sub(height)) / 2), |
| 186 |
Constraint::Length(height), |
| 187 |
Constraint::Min(0), |
| 188 |
]) |
| 189 |
.split(r); |
| 190 |
|
| 191 |
Layout::default() |
| 192 |
.direction(Direction::Horizontal) |
| 193 |
.constraints([ |
| 194 |
Constraint::Percentage((100 - percent_x) / 2), |
| 195 |
Constraint::Percentage(percent_x), |
| 196 |
Constraint::Percentage((100 - percent_x) / 2), |
| 197 |
]) |
| 198 |
.split(popup_layout[1])[1] |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
fn cleanup(&mut self) -> Result<()> { |
| 203 |
disable_raw_mode()?; |
| 204 |
self.terminal |
| 205 |
.backend_mut() |
| 206 |
.execute(terminal::LeaveAlternateScreen)?; |
| 207 |
self.terminal.show_cursor()?; |
| 208 |
Ok(()) |
| 209 |
} |
| 210 |
|
| 211 |
fn render_all_tables(&self, f: &mut Frame<'_>, body_chunks: Rc<[Rect]>, app: &mut App) { |
| 212 |
let rows: Vec<Row> = app |
| 213 |
.known_networks |
| 214 |
.items |
| 215 |
.iter() |
| 216 |
.map(|net| -> Row<'_> { |
| 217 |
Row::new(vec![ |
| 218 |
Cell::from(net.ssid.clone()), |
| 219 |
Cell::from(if net.secured { "Secured" } else { "Open" }), |
| 220 |
Cell::from(if app.network_manager.is_connected_cached(&net.ssid) { |
| 221 |
"Connected" |
| 222 |
} else { |
| 223 |
"-" |
| 224 |
}), |
| 225 |
Cell::from(format!("{}%", net.strength.unwrap_or(0))), |
| 226 |
]) |
| 227 |
}) |
| 228 |
.collect(); |
| 229 |
table::draw( |
| 230 |
f, |
| 231 |
body_chunks[0], |
| 232 |
&mut TableData { |
| 233 |
title: " Known Networks ", |
| 234 |
header_cols: vec!["Name", "Security", "State", "Signal"], |
| 235 |
constraint: vec![ |
| 236 |
Constraint::Percentage(40), |
| 237 |
Constraint::Percentage(20), |
| 238 |
Constraint::Percentage(20), |
| 239 |
Constraint::Percentage(20), |
| 240 |
], |
| 241 |
cells: rows, |
| 242 |
list: &mut app.known_networks, |
| 243 |
}, |
| 244 |
self.active_tab == Tabs::KnownNetworks, |
| 245 |
); |
| 246 |
let rows: Vec<Row> = app |
| 247 |
.new_networks |
| 248 |
.items |
| 249 |
.iter() |
| 250 |
.map(|net| -> Row<'_> { |
| 251 |
Row::new(vec![ |
| 252 |
Cell::from(net.ssid.clone()), |
| 253 |
Cell::from(if net.secured { "Secured" } else { "Open" }), |
| 254 |
Cell::from(format!("{}%", net.strength.unwrap_or(0))), |
| 255 |
]) |
| 256 |
}) |
| 257 |
.collect(); |
| 258 |
table::draw( |
| 259 |
f, |
| 260 |
body_chunks[1], |
| 261 |
&mut TableData { |
| 262 |
title: " Available Networks ", |
| 263 |
header_cols: vec!["Name", "Security", "Signal"], |
| 264 |
constraint: vec![ |
| 265 |
Constraint::Percentage(40), |
| 266 |
Constraint::Percentage(20), |
| 267 |
Constraint::Percentage(20), |
| 268 |
], |
| 269 |
cells: rows, |
| 270 |
list: &mut app.new_networks, |
| 271 |
}, |
| 272 |
self.active_tab == Tabs::AvailableNetworks, |
| 273 |
); |
| 274 |
|
| 275 |
let rows: Vec<Row> = app |
| 276 |
.devices |
| 277 |
.items |
| 278 |
.iter() |
| 279 |
.map(|net| -> Row<'_> { |
| 280 |
Row::new(vec![ |
| 281 |
Cell::from(net.interface.clone()), |
| 282 |
Cell::from(if app.network_manager.enabled { |
| 283 |
"On" |
| 284 |
} else { |
| 285 |
"Off" |
| 286 |
}), |
| 287 |
|
| 288 |
]) |
| 289 |
}) |
| 290 |
.collect(); |
| 291 |
table::draw( |
| 292 |
f, |
| 293 |
body_chunks[2], |
| 294 |
&mut TableData { |
| 295 |
title: " Device ", |
| 296 |
header_cols: vec!["Name", "Powered", "Scanning", "Frequency", "Security"], |
| 297 |
constraint: vec![ |
| 298 |
Constraint::Percentage(20), |
| 299 |
Constraint::Percentage(20), |
| 300 |
Constraint::Percentage(20), |
| 301 |
Constraint::Percentage(20), |
| 302 |
Constraint::Percentage(20), |
| 303 |
], |
| 304 |
cells: rows, |
| 305 |
list: &mut app.devices, |
| 306 |
}, |
| 307 |
self.active_tab == Tabs::Devices, |
| 308 |
); |
| 309 |
} |
| 310 |
} |