index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff

Diffstat

-rw-r--r-- justfile 5
-rw-r--r-- src/cli.rs 2
-rw-r--r-- src/events.rs 11
-rw-r--r-- src/logger.rs 19
-rw-r--r-- src/main.rs 2
-rw-r--r-- src/timer.rs 1
-rw-r--r-- src/tui.rs 25
-rw-r--r-- src/ui/table.rs 8
-rw-r--r-- src/ui/toast.rs 9
 9 files changed, 46 insertions(+), 36 deletions(-)

diff --git a/justfile b/justfile
index 0a37b4a..1329c1e 100644
--- a/justfile
+++ b/justfile
@@ -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
diff --git a/src/cli.rs b/src/cli.rs
index c83ed1d..50696c0 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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,
}
diff --git a/src/events.rs b/src/events.rs
index 79ce521..cda2025 100644
--- a/src/events.rs
+++ b/src/events.rs
@@ -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,
                                })));
                            }
diff --git a/src/logger.rs b/src/logger.rs
index 2842174..1381f99 100644
--- a/src/logger.rs
+++ b/src/logger.rs
@@ -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(())
diff --git a/src/main.rs b/src/main.rs
index d0ed4a3..e94be6a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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);
diff --git a/src/timer.rs b/src/timer.rs
index 73be6c3..02f8ae1 100644
--- a/src/timer.rs
+++ b/src/timer.rs
@@ -9,7 +9,6 @@
    pub enabled: bool,
}

#[allow(dead_code)]
impl Timer {
    pub fn new(duration: Duration, on_finish: Action, enabled: bool) -> Self {
        Self {
diff --git a/src/tui.rs b/src/tui.rs
index bfb5d88..27293b2 100644
--- a/src/tui.rs
+++ b/src/tui.rs
@@ -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();
    }
}
diff --git a/src/ui/table.rs b/src/ui/table.rs
index 7992130..beeb136 100644
--- a/src/ui/table.rs
+++ b/src/ui/table.rs
@@ -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()
diff --git a/src/ui/toast.rs b/src/ui/toast.rs
index 4f453d5..9456d96 100644
--- a/src/ui/toast.rs
+++ b/src/ui/toast.rs
@@ -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,