Skip to content

Commit

Permalink
34 feat terminate connection in clashtui (#39)
Browse files Browse the repository at this point in the history
* add connection tab, with widget todo
rebuild webapi sub mod

* mark item under tools as deprecated

* finish function impl

* rm deprecated functions

* use popup color, fix cleared aera size, use arrow key to show status

* close #34, waitting for PR
rename feature into `connection-tab`

* avoid terminate a closed connection
  • Loading branch information
Jackhr-arch authored Sep 6, 2024
1 parent 8ff636f commit 0e9a58d
Show file tree
Hide file tree
Showing 30 changed files with 1,114 additions and 340 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,30 @@ bin-cli = [
"log4rs",
"encoding",
]
bin-tui = ["ratatui", "crossterm", "tokio", "bin-cli"]
bin-tui = ["ratatui", "crossterm", "tokio", "bin-cli", "connection-tab"]
template = []
connection-tab = ["ratatui/unstable-widget-ref"]

[[bin]]
name = "clashtui"
required-features = ["bin-tui"]

[dependencies]
ratatui = { version = "^0.28.0", default-features = false, features = [
"crossterm",
], optional = true }
log = "^0.4.22"
enumflags2 = { version = "^0.7.10", optional = true }
clap_complete = { version = "^4.5.7", optional = true }
anyhow = "^1.0.86"
is-root = { version = "0.1.3", optional = true }
serde = { version = "^1.0.208", features = ["derive"] }
minreq = { version = "^2.12.0", features = ["proxy", "https"] }
minreq = { version = "^2.12.0", features = [
"proxy",
"https",
"json-using-serde",
] }
serde_json = "^1.0.125"
serde_yaml = "0.9.34"
futures = { version = "0.3.30", default-features = false }
thiserror = "1.0.63"

[dependencies.tokio]
version = "^1.39.0"
Expand All @@ -60,6 +63,12 @@ default-features = false
features = ["events", "event-stream"]
optional = true

[dependencies.ratatui]
version = "^0.28.1"
default-features = false
features = ["crossterm"]
optional = true

[target.'cfg(windows)'.dependencies]
encoding = { version = "^0.2.33", optional = true }

Expand Down
11 changes: 8 additions & 3 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
# use for build arm64 in docker
# CROSS_CONTAINER_IN_CONTAINER = true

[tasks.rd]
alias = "rundev"
[tasks.rundev]
command = "cargo"
args = ["run", "--features", "bin-tui", "--bin", "clashtui"]

[tasks.ci]
dependencies = ["setup_mihomo", "ci-flow"]
dependencies = ["setup-mihomo", "ci-flow"]
script = ["rm mihomo", "rm Doc/clash_test/cache.db"]

[tasks.ci.windows]
# ignore this shit
dependencies = []
script = ""

[tasks.setup_mihomo.windows]
[tasks.setup-mihomo.windows]
script_runner = "pwsh"
script_extension = "ps1"
script = '''
curl --output mihomo.zip -L https://github.com/MetaCubeX/mihomo/releases/download/v1.18.4/mihomo-windows-amd64-v1.18.4.zip
7z x mihomo.zip
mihomo-windows-amd64.exe -d Doc/clash_test -f Doc/clash_test/config.yaml &
'''
[tasks.setup_mihomo.linux]
[tasks.setup-mihomo.linux]
script = '''
curl --output mihomo.gz -L https://github.com/MetaCubeX/mihomo/releases/download/v1.18.4/mihomo-linux-amd64-v1.18.4.gz
gunzip mihomo.gz
Expand Down
2 changes: 1 addition & 1 deletion src/backend/impl_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ClashBackend {
) -> anyhow::Result<Vec<String>> {
if profile.dtype.is_upgradable() {
// store (name,url) to be downloaded
let mut work_vec: Vec<(String, String)> = Vec::new();
let mut work_vec: Vec<(String, String)> = Vec::with_capacity(2);
match &profile.dtype {
// Imported file won't update, overwrite it if necessary
ProfileType::File => unreachable!(),
Expand Down
18 changes: 0 additions & 18 deletions src/backend/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,6 @@ pub fn extract_domain(url: &str) -> Option<&str> {
None
}

// pub fn bytes_to_readable(bytes: u64) -> String {
// const KILOBYTE: u64 = 1024;
// const MEGABYTE: u64 = KILOBYTE * 1024;
// const GIGABYTE: u64 = MEGABYTE * 1024;
// const TERABYTE: u64 = GIGABYTE * 1024;

// if bytes >= TERABYTE {
// format!("{:.2} TB", bytes as f64 / TERABYTE as f64)
// } else if bytes >= GIGABYTE {
// format!("{:.2} GB", bytes as f64 / GIGABYTE as f64)
// } else if bytes >= MEGABYTE {
// format!("{:.2} MB", bytes as f64 / MEGABYTE as f64)
// } else if bytes >= KILOBYTE {
// format!("{:.2} KB", bytes as f64 / KILOBYTE as f64)
// } else {
// format!("{} Bytes", bytes)
// }
// }
/*
pub fn timestamp_to_readable(timestamp: u64) -> String {
let duration = std::time::Duration::from_secs(timestamp);
Expand Down
4 changes: 3 additions & 1 deletion src/bin/clashtui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ fn main() {
#[tokio::main(flavor = "current_thread")]
async fn start_tui(backend: BackEnd) -> anyhow::Result<()> {
use tui::setup;
tui::Theme::load(None)?;
if let Err(_) = tui::Theme::load(None) {
log::error!("Global Theme has been loaded");
}
let app = tui::FrontEnd::new();
setup::setup()?;
// make terminal restore after panic
Expand Down
14 changes: 13 additions & 1 deletion src/bin/clashtui/tui/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ impl FrontEnd {
pub fn new() -> Self {
let service_tab = tabs::service::ServiceTab::new();
let profile_tab = tabs::profile::ProfileTab::new();
let tabs = vec![profile_tab.into(), service_tab.into()];
#[cfg(feature = "connection-tab")]
let connection_tab = tabs::connection::ConnctionTab::new();
let tabs = vec![
profile_tab.into(),
service_tab.into(),
#[cfg(feature = "connection-tab")]
connection_tab.into(),
];
Self {
tabs,
tab_index: 0,
Expand Down Expand Up @@ -137,9 +144,14 @@ impl FrontEnd {
CallBack::ProfileInit(..) => self.tabs[0].apply_backend_call(op),
#[cfg(feature = "template")]
CallBack::TemplateInit(_) => self.tabs[0].apply_backend_call(op),
// assume ConnctionTab is the third tab
#[cfg(feature = "connection-tab")]
CallBack::ConnctionInit(..) => self.tabs[2].apply_backend_call(op),
CallBack::ProfileCTL(_) | CallBack::ServiceCTL(_) => {
self.tabs[self.tab_index].apply_backend_call(op)
}
#[cfg(feature = "connection-tab")]
CallBack::ConnctionCTL(_) => self.tabs[self.tab_index].apply_backend_call(op),
},
Err(e) => match e {
tokio::sync::mpsc::error::TryRecvError::Empty => break,
Expand Down
2 changes: 2 additions & 0 deletions src/bin/clashtui/tui/frontend/consts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub const TAB_TITLE_SERVICE: &str = "ServiceCTL";
pub const TAB_TITLE_PROFILE: &str = "ProfileCTL";
#[cfg(feature = "connection-tab")]
pub const TAB_TITLE_CONNECTION: &str = "ConnectionCTL";
#[cfg(feature = "template")]
pub const TAB_TITLE_TEMPLATE: &str = "TemplateCTL";
8 changes: 7 additions & 1 deletion src/bin/clashtui/tui/frontend/key_bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! define_keys {
($(#[$attr:meta])*
$vis:vis enum $name: ident
{$(
$(##[cfg($prompt_cfg_attr:meta)])*
$(# #[cfg($prompt_cfg_attr:meta)])*
# $prompt:ident $(,)?
$(
$(#[doc = $doc:expr])*
Expand Down Expand Up @@ -103,6 +103,7 @@ macro_rules! define_keys {
[$(
$(#[cfg($prompt_cfg_attr)])*
concat!("# ",stringify!($prompt)),
$(#[cfg($prompt_cfg_attr)])*
$(
$(#[cfg($cfg_attr)])*
(concat!($($ch)? $($chs)?, ":" $(, $doc)*)),
Expand All @@ -119,6 +120,7 @@ macro_rules! define_keys {
<[()]>::len(&[$(
$(#[cfg($prompt_cfg_attr)])*
replace_expr!($prompt, ()),
$(#[cfg($prompt_cfg_attr)])*
$(
$(#[cfg($cfg_attr)])*
replace_expr!($variant, ()),
Expand Down Expand Up @@ -159,6 +161,10 @@ define_keys! {
# Template
/// Switch to profile sub tab
TemplateSwitch(KeyCode::Char('p')),
# #[cfg(feature = "connection-tab")]
# Connction
/// Terminate all running connections
ConnKillAll(KeyCode::Char('c')),
# Global
/// Edit this
Edit(KeyCode::Char('e')),
Expand Down
8 changes: 8 additions & 0 deletions src/bin/clashtui/tui/frontend/tabs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "connection-tab")]
pub mod connection;
pub mod profile;
pub mod service;
#[macro_use]
Expand All @@ -11,6 +13,8 @@ use crossterm::event::KeyEvent;
use ratatui::prelude as Ra;
use Ra::{Frame, Rect};

#[cfg(feature = "connection-tab")]
use connection::ConnctionTab;
use profile::ProfileTab;
use service::ServiceTab;

Expand All @@ -28,6 +32,8 @@ build_tabs!(
enum Tabs {
Profile(ProfileTab),
Service(ServiceTab),
#[cfg(feature = "connection-tab")]
Connection(ConnctionTab),
}
);
/// a wrapper for [`Tabs`]
Expand All @@ -41,6 +47,8 @@ impl std::fmt::Display for TabContainer {
match self.0 {
Tabs::Profile(_) => consts::TAB_TITLE_PROFILE,
Tabs::Service(_) => consts::TAB_TITLE_SERVICE,
#[cfg(feature = "connection-tab")]
Tabs::Connection(_) => consts::TAB_TITLE_CONNECTION,
}
)
}
Expand Down
Loading

0 comments on commit 0e9a58d

Please sign in to comment.