Added the ability to close a tunnel from the tui

This commit is contained in:
Dreaded_X 2025-04-12 23:30:53 +02:00
parent 4fa885843f
commit 15f382b654
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
4 changed files with 30 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use std::{io::Write, iter::once};
use std::{cmp::min, io::Write, iter::once};
use clap::Parser as _;
use ratatui::{Terminal, TerminalOptions, Viewport, layout::Rect, prelude::CrosstermBackend};
@ -116,6 +116,25 @@ impl Handler {
warn!("User not set");
}
}
Input::Delete => {
let Some(selected) = self.selected else {
return Ok(false);
};
if selected >= self.tunnels.len() {
warn!("Trying to delete tunnel out of bounds");
return Ok(false);
}
let tunnel = self.tunnels.remove(selected);
self.all_tunnels.remove_tunnel(tunnel).await;
if self.tunnels.is_empty() {
self.selected = None;
} else {
self.selected = Some(min(self.tunnels.len() - 1, selected));
}
}
Input::CtrlP => {
self.set_access_selection(TunnelAccess::Protected).await;
}
@ -321,7 +340,9 @@ impl Drop for Handler {
let mut all_tunnels = self.all_tunnels.clone();
tokio::spawn(async move {
all_tunnels.remove_tunnels(&tunnels).await;
for tunnel in tunnels {
all_tunnels.remove_tunnel(tunnel).await;
}
});
}
}

View File

@ -5,6 +5,7 @@ pub enum Input {
Char(char),
Up,
Down,
Delete,
Esc,
Enter,
CtrlP,
@ -18,6 +19,7 @@ impl From<&[u8]> for Input {
[27] => Input::Esc,
[27, 91, 65] => Input::Up,
[27, 91, 66] => Input::Down,
[27, 91, 51, 126] => Input::Delete,
[13] => Input::Enter,
// NOTE: Actual char is DLE, this happens to map to ctrl-p
[16] => Input::CtrlP,

View File

@ -42,6 +42,7 @@ impl Renderer {
command("esc", "deselect"),
command("↓/j", "move down"),
command("↑/k", "move up"),
command("del", "close tunnel"),
vec![],
command("p", "make private"),
command("ctrl-p", "make protected"),

View File

@ -130,15 +130,13 @@ impl Tunnels {
tunnel
}
pub async fn remove_tunnels(&mut self, tunnels: &[Tunnel]) {
pub async fn remove_tunnel(&mut self, tunnel: Tunnel) {
let mut all_tunnels = self.tunnels.write().await;
for tunnel in tunnels {
if let Some(address) = tunnel.get_address() {
trace!(tunnel.name, "Removing tunnel");
all_tunnels.remove(&address);
}
}
}
}
impl Service<Request<Incoming>> for Tunnels {