From 693df6817a487933fa55794877fbf4a80ee104f3 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 16 Apr 2025 02:14:57 +0200 Subject: [PATCH] Tunnel tui functions are now implemented on Tunnel --- src/tui.rs | 8 +++--- src/tunnel/tui.rs | 68 ++++++++++++++++++++++++----------------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/tui.rs b/src/tui.rs index 18e78b8..9d157df 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -12,7 +12,7 @@ use ratatui::{ }; use unicode_width::UnicodeWidthStr; -use crate::tunnel::{self, Tunnel}; +use crate::tunnel::Tunnel; #[derive(Default)] pub struct Renderer { @@ -28,7 +28,7 @@ impl Renderer { // NOTE: This needs to be a separate function as the render functions can not be async pub async fn update(&mut self, tunnels: &[Tunnel], index: Option) { self.table_rows = futures::stream::iter(tunnels) - .then(tunnel::tui::to_row) + .then(Tunnel::to_row) .collect::>() .await; @@ -144,7 +144,7 @@ impl Renderer { } fn compute_widths(&mut self) -> Vec { - let table_header = tunnel::tui::header(); + let table_header = Tunnel::header(); std::iter::once(&table_header) .chain(&self.table_rows) .map(|row| row.iter().map(|cell| cell.width() as u16)) @@ -173,7 +173,7 @@ impl Renderer { .height(1) }); - let header = tunnel::tui::header() + let header = Tunnel::header() .iter() .cloned() .map(Cell::from) diff --git a/src/tunnel/tui.rs b/src/tunnel/tui.rs index a3771c3..dc423f7 100644 --- a/src/tunnel/tui.rs +++ b/src/tunnel/tui.rs @@ -5,37 +5,39 @@ use ratatui::text::Span; use super::{Tunnel, TunnelAccess}; -pub fn header() -> Vec> { - vec![ - "Name".into(), - "Access".into(), - "Port".into(), - "Address".into(), - "Conn".into(), - "Rx".into(), - "Tx".into(), - ] -} - -pub async fn to_row(tunnel: &Tunnel) -> Vec> { - let access = match tunnel.inner.access.read().await.deref() { - TunnelAccess::Private(owner) => owner.clone().yellow(), - TunnelAccess::Protected => "PROTECTED".blue(), - TunnelAccess::Public => "PUBLIC".green(), - }; - - let address = tunnel - .get_address() - .map(|address| format!("http://{address}").into()) - .unwrap_or("FAILED".red()); - - vec![ - tunnel.registry_entry.get_name().to_owned().into(), - access, - tunnel.inner.port.to_string().into(), - address, - tunnel.inner.stats.connections().to_string().into(), - tunnel.inner.stats.rx().to_string().into(), - tunnel.inner.stats.tx().to_string().into(), - ] +impl Tunnel { + pub fn header() -> Vec> { + vec![ + "Name".into(), + "Access".into(), + "Port".into(), + "Address".into(), + "Conn".into(), + "Rx".into(), + "Tx".into(), + ] + } + + pub async fn to_row(tunnel: &Tunnel) -> Vec> { + let access = match tunnel.inner.access.read().await.deref() { + TunnelAccess::Private(owner) => owner.clone().yellow(), + TunnelAccess::Protected => "PROTECTED".blue(), + TunnelAccess::Public => "PUBLIC".green(), + }; + + let address = tunnel + .get_address() + .map(|address| format!("http://{address}").into()) + .unwrap_or("FAILED".red()); + + vec![ + tunnel.registry_entry.get_name().to_owned().into(), + access, + tunnel.inner.port.to_string().into(), + address, + tunnel.inner.stats.connections().to_string().into(), + tunnel.inner.stats.rx().to_string().into(), + tunnel.inner.stats.tx().to_string().into(), + ] + } }