fix: bugs
9 files changed, 46 insertions(+), 36 deletions(-)
@@ -4,8 +4,9 @@
run:
cargo run
debug:
NMRS_LOG=trace cargo run
debug $argument="":
@NMRS_LOG=trace cargo run -- $argument
@bat ~/.cache/nmrs-tui/nmrs-tui.log
check:
cargo check
@@ -3,7 +3,7 @@
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(long, action = ArgAction::SetTrue)]
#[arg(short, long, action = ArgAction::SetTrue)]
/// generate config file.
pub config: bool,
}
@@ -49,12 +49,9 @@
}
}
KeyCode::Char('r') => match tab {
Tabs::AvailableNetworks | Tabs::KnownNetworks => {
app.action.send(Action::Refresh);
}
_ => {}
},
KeyCode::Char('r') => {
app.action.send(Action::Refresh);
}
KeyCode::Enter | KeyCode::Char(' ') => match tab {
Tabs::KnownNetworks => {
@@ -160,7 +157,7 @@
_ = action_tx.send(Action::ShowToast(Box::new(ToastRequest {
title: None,
msg: "Failed to update the saved connection!".into(),
urgency: Urgency::Success,
urgency: Urgency::Critical,
duration: None,
})));
}
@@ -6,7 +6,18 @@
use std::path::PathBuf;
use std::{env, fs};
use crate::tui::Tui;
pub fn init() -> Result<()> {
let default_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
Tui::restore_terminal();
error!("APPLICATION PANIC: {}", panic_info);
logger().flush();
default_hook(panic_info);
}));
let level = match env::var("NMRS_LOG")
.unwrap_or_default()
.to_lowercase()
@@ -23,14 +34,6 @@
let log_file = File::create(&log_path).expect("Failed to create log file.");
CombinedLogger::init(vec![WriteLogger::new(level, Config::default(), log_file)]).unwrap();
let default_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
error!("APPLICATION PANIC: {}", panic_info);
logger().flush();
default_hook(panic_info);
}));
info!("Logger initialized successfully at {:?}", log_path);
Ok(())
@@ -43,8 +43,6 @@
// Start TUI
if let Err(e) = tui.run(&mut app).await {
tui.cleanup()?;
error!("Fatal error: {:#}", e);
logger().flush();
return Err(e);
@@ -9,7 +9,6 @@
pub enabled: bool,
}
#[allow(dead_code)]
impl Timer {
pub fn new(duration: Duration, on_finish: Action, enabled: bool) -> Self {
Self {
@@ -48,7 +48,7 @@
pub async fn run(&mut self, app: &mut App) -> anyhow::Result<()> {
let mut reader = EventStream::new();
let mut last_tick = time::Instant::now();
let tick_rate = Duration::from_secs(1);
let tick_rate = Duration::from_millis(100);
while !app.should_quit {
if self.should_render {
@@ -64,14 +64,14 @@
maybe_event = reader.next() => {
if let Some(Ok(Event::Key(key))) = maybe_event {
events::handle_events(app, key).await?;
debug!("Event: {:?}!", key);
trace!("Event: {:?}!", key);
self.should_render = true;
}
}
// Render when there no action or event
_ = time::sleep(tick_rate.saturating_sub(last_tick.elapsed())) => {
debug!("Ticks!");
trace!("Ticks!");
self.should_render = true;
}
}
@@ -84,7 +84,6 @@
ActionHandler::handle_actions(app).await?;
}
self.cleanup()?;
Ok(())
}
@@ -128,11 +127,17 @@
toast::draw(f, &app.config.ui.toast, &app.toasts);
}
// terminal cleanup
pub fn cleanup(&mut self) -> anyhow::Result<()> {
disable_raw_mode()?;
self.terminal.backend_mut().execute(LeaveAlternateScreen)?;
self.terminal.show_cursor()?;
Ok(())
pub fn restore_terminal() {
let _ = disable_raw_mode();
let _ = stdout().execute(LeaveAlternateScreen);
let _ = stdout().execute(crossterm::cursor::Show);
}
}
impl Drop for Tui {
fn drop(&mut self) {
let _ = disable_raw_mode();
let _ = self.terminal.backend_mut().execute(LeaveAlternateScreen);
let _ = self.terminal.show_cursor();
}
}
@@ -74,7 +74,7 @@
pub list: &'a mut StatefulList<T>,
}
fn network_info<'a>(net: Network) -> (&'a str, String, &'a str) {
fn network_info<'a>(net: &Network) -> (&'a str, String, &'a str) {
let security = if net.is_psk {
"Psk"
} else if net.is_eap {
@@ -99,7 +99,7 @@
};
(strength.to_string(), bars)
} else {
("Unknown".to_string(), "")
("Unknown".to_string(), "")
};
(security, strength, bars)
@@ -167,7 +167,7 @@
.items
.iter()
.map(|net| -> Row<'_> {
let (security, strength, bars) = network_info(net.clone());
let (security, strength, bars) = network_info(net);
let connected = app
.network_manager
@@ -212,7 +212,7 @@
.items
.iter()
.map(|net| -> Row<'_> {
let (security, strength, bars) = network_info(net.clone());
let (security, strength, bars) = network_info(net);
let cells: Vec<Line> = columns
.iter()
@@ -85,7 +85,14 @@
}
pub fn draw(f: &mut Frame, config: &ToastConfig, toasts: &[Toast]) {
let visible_height = f.area().height;
for (index, toast) in toasts.iter().enumerate() {
let top_margin = config.margin.top + (index as u16 * 4);
if top_margin >= visible_height - 4 {
break;
};
let area = popup::popup_rect(
f,
&PopupConfig {
@@ -93,7 +100,7 @@
height: 4, // 4 = borders(2) + title(1) + msg(1)
position: config.position,
margin: Margin {
top: config.margin.top + (index as u16 * 4),
top: top_margin,
bottom: config.margin.bottom,
left: config.margin.left,
right: config.margin.right,