feat: Status bar (#26)
* feat: Status bar
* fmt
9 files changed, 120 insertions(+), 39 deletions(-)
@@ -6,6 +6,10 @@
direction: Vertical,
panels: [
(
kind: Header,
constraint: Length(1),
),
(
kind: KnownNetworks,
constraint: Percentage(50),
),
@@ -61,7 +65,6 @@
type_active: Thick,
),
margin: Margin(
top: 1,
left: 1,
right: 1,
),
@@ -4,7 +4,7 @@
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use crate::{
app::{App, Focus, Popups, Tabs},
app::{App, Focus, Popups, Status, Tabs},
ui::{
input::InputMode,
toast::{Toast, Urgency},
@@ -17,6 +17,7 @@
Tick(Duration),
Refresh,
SetFocus(Focus),
SetStatus(Status),
SetInputMode(InputMode),
// Network Commands
@@ -160,6 +161,12 @@
Focus::Tab(_) => app.scan.enable(),
}
}
Action::SetStatus(new_status) => {
debug!("Set status to: {:?}!", new_status);
app.status = new_status;
}
Action::SetInputMode(new_inputmode) => {
debug!("Set InputMode to: {:?}!", new_inputmode);
app.input.mode = new_inputmode;
@@ -196,12 +203,9 @@
}
Action::Connect(box_data) => {
let conn_req = *box_data;
app.action.send(Action::ShowToast(Box::new(ToastRequest {
title: None,
msg: format!("Connecting to {}...", conn_req.ssid).into(),
urgency: Urgency::Normal,
duration: None,
})));
app.action.send(Action::SetStatus(Status::Connecting(
conn_req.ssid.to_string(),
)));
let action_tx = app.action.sender();
let network_manager = app.network_manager.clone();
@@ -251,6 +255,11 @@
urgency,
duration: None,
})));
_ = action_tx.send(Action::SetStatus(Status::None));
// Trigger a refresh so current_network update
_ = action_tx.send(Action::Refresh);
});
}
Action::Forget { ssid } => {
@@ -275,6 +284,7 @@
})));
}
}
// Trigger a refresh so current_network update
_ = action_tx.send(Action::Refresh);
});
}
@@ -295,22 +305,23 @@
}
};
let msg;
let urgency;
match network_manager.set_airplane_mode(!enabled).await {
let (msg, urgency) = match network_manager.set_airplane_mode(!enabled).await
{
Ok(_) => {
debug!("Airplane set to {}", !enabled);
msg = format!(
"Airplane mode {}",
if !enabled { "On" } else { "Off" }
);
urgency = Urgency::Success;
}
Err(_) => {
msg = "Toggle airplane mode failed!".to_string();
urgency = Urgency::Critical;
}
(
format!(
"Airplane mode {}",
if !enabled { "On" } else { "Off" }
),
Urgency::Success,
)
}
Err(_) => (
"Toggle airplane mode failed!".to_string(),
Urgency::Critical,
),
};
_ = action_tx.send(Action::ShowToast(Box::new(ToastRequest {
title: None,
@@ -337,20 +348,18 @@
return;
}
};
let msg;
let urgency;
match network_manager.set_wireless_enabled(!enabled).await {
Ok(_) => {
debug!("Wifi radio set to {}", !enabled);
msg = format!("Wifi {}", if !enabled { "On" } else { "Off" });
urgency = Urgency::Success;
}
Err(_) => {
msg = "Toggle power failed!".to_string();
urgency = Urgency::Critical;
}
};
let (msg, urgency) =
match network_manager.set_wireless_enabled(!enabled).await {
Ok(_) => {
debug!("Wifi radio set to {}", !enabled);
(
format!("Wifi {}", if !enabled { "On" } else { "Off" }),
Urgency::Success,
)
}
Err(_) => ("Toggle power failed!".to_string(), Urgency::Critical),
};
_ = action_tx.send(Action::ShowToast(Box::new(ToastRequest {
title: None,
msg: msg.into(),
@@ -22,6 +22,13 @@
Password,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Status {
// Scanning,
Connecting(String),
None,
}
#[derive(Debug, Clone, Copy)]
pub enum Focus {
Tab(Tabs),
@@ -41,6 +48,7 @@
pub should_quit: bool,
// Data
pub status: Status,
pub input: Input,
pub focus: Focus,
pub last_focus: Focus,
@@ -70,6 +78,7 @@
should_quit: false,
// Data
status: Status::None,
input: Input::new(),
focus: Focus::Tab(Tabs::KnownNetworks),
last_focus: Focus::Tab(Tabs::KnownNetworks),
@@ -18,7 +18,7 @@
app::{App, Focus, Popups, Selected},
events,
ui::{
PanelKind, help, popup, table,
PanelKind, header, help, popup, table,
toast::{self, Urgency},
},
};
@@ -89,6 +89,7 @@
for (kind, area) in panel_kinds.iter().zip(chunks.iter()) {
match kind {
PanelKind::Header => header::draw(f, *area, app),
PanelKind::KnownNetworks => table::draw_known_network(f, *area, app),
PanelKind::AvailableNetworks => table::draw_available_network(f, *area, app),
PanelKind::Devices => table::draw_devices(f, *area, app),
@@ -1,0 +1,55 @@
use ratatui::{
Frame,
layout::{Alignment, Constraint, Layout, Rect},
style::{Color, Style},
text::{Line, Span},
};
use crate::{
app::{App, Status},
ui::table::network_info,
};
pub fn draw(f: &mut Frame<'_>, area: Rect, app: &mut App) {
let chunks = Layout::horizontal([
Constraint::Fill(12),
Constraint::Fill(1),
Constraint::Fill(1),
])
.split(area);
let left_line = Line::from(vec![Span::styled(
" nmrs-tui",
Style::new().bold().fg(Color::Yellow),
)])
.alignment(Alignment::Left);
// Center: status
let status_span = match &app.status {
Status::Connecting(ssid) => Span::styled(
format!("Connecting to {}...", ssid),
Style::new().fg(Color::Green),
),
Status::None => Span::raw(""),
};
let center_line = Line::from(status_span).alignment(Alignment::Center);
// Right: Currently connected network
let right_line = if let Some(current) = &app.network_manager.current_network {
let (_, strength, bars) = network_info(current);
Line::from(vec![
Span::styled(format!("{} ", bars), Style::new().fg(Color::Green)),
Span::raw(format!("{} ({}%) ", current.ssid, strength)),
])
} else {
Line::from(vec![Span::styled(
" Disconnected ",
Style::new().fg(Color::DarkGray),
)])
}
.alignment(Alignment::Right);
f.render_widget(left_line, chunks[0]);
f.render_widget(center_line, chunks[1]);
f.render_widget(right_line, chunks[2]);
}
@@ -8,6 +8,7 @@
};
pub mod area;
pub mod header;
pub mod help;
pub mod input;
pub mod list;
@@ -45,6 +46,7 @@
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
pub enum PanelKind {
Header,
KnownNetworks,
AvailableNetworks,
Devices,
@@ -72,7 +72,9 @@
};
// Horizontal scrolling when password is to long
let string_offset = password.len().saturating_sub(input_chunks[0].width.saturating_sub(1) as usize);
let string_offset = password
.len()
.saturating_sub(input_chunks[0].width.saturating_sub(1) as usize);
if string_offset > 0 {
password = password.split_at(string_offset).1.to_string();
}
@@ -74,7 +74,7 @@
pub list: &'a mut StatefulList<T>,
}
fn network_info<'a>(net: &Network) -> (&'a str, String, &'a str) {
pub fn network_info<'a>(net: &Network) -> (&'a str, String, &'a str) {
let security = if net.is_psk {
"Psk"
} else if net.is_eap {
@@ -34,10 +34,10 @@
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[allow(dead_code)]
pub enum Urgency {
Normal,
Success,
#[allow(dead_code)]
Warning,
Critical,
}