Skip to content

Commit

Permalink
init MacOs support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackhr-arch committed Jun 27, 2024
1 parent 25dccf2 commit 6e65e6a
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 16 deletions.
10 changes: 10 additions & 0 deletions clashtui/backend/src/backend/impl_clashsrv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ClashBackend;
#[allow(unused_imports)] // currently, only [`SwitchMode`] is impl on macos
use crate::utils::ipc::{self, exec};
use crate::utils::ClashSrvOp;
use std::io::Error;
Expand Down Expand Up @@ -54,6 +55,15 @@ impl ClashBackend {
)),
}
}
#[cfg(target_os = "macos")]
pub fn clash_srv_ctl(&self, op: ClashSrvOp) -> Result<String, Error> {
match op {
_ => Err(Error::new(
std::io::ErrorKind::NotFound,
"No Support Action",
)),
}
}
#[cfg(target_os = "windows")]
pub fn clash_srv_ctl(&self, op: ClashSrvOp) -> Result<String, Error> {
//let exe_dir = std::env::current_exe()
Expand Down
2 changes: 1 addition & 1 deletion clashtui/backend/src/backend/impl_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl ClashBackend {
);
#[cfg(target_os = "windows")]
return ipc::exec("cmd", vec!["/C", cmd.as_str()]);
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
ipc::exec("sh", vec!["-c", cmd.as_str()])
}

Expand Down
41 changes: 41 additions & 0 deletions clashtui/backend/src/utils/ipc_macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::process::{Command, Output, Stdio};

use std::io::Result;

pub fn exec(pgm: &str, args: Vec<&str>) -> Result<String> {
log::debug!("IPC: {} {:?}", pgm, args);
let output = Command::new(pgm).args(args).output()?;
string_process_output(output)
}
#[allow(unused)]
pub fn spawn(pgm: &str, args: Vec<&str>) -> Result<()> {
log::debug!("SPW: {} {:?}", pgm, args);
// Just ignore the output, otherwise the ui might be broken
Command::new(pgm)
.stderr(Stdio::null())
.stdout(Stdio::null())
.args(args)
.spawn()?;
Ok(())
}

fn string_process_output(output: Output) -> Result<String> {
let stdout_str = String::from_utf8(output.stdout).unwrap();
let stderr_str = String::from_utf8(output.stderr).unwrap();

let result_str = format!(
r#"
Status:
{}
Stdout:
{}
Stderr:
{}
"#,
output.status, stdout_str, stderr_str
);

Ok(result_str)
}
8 changes: 8 additions & 0 deletions clashtui/backend/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) mod config;
#[cfg_attr(target_os = "windows", path = "ipc_windows.rs")]
#[cfg_attr(target_os = "linux", path = "ipc_linux.rs")]
#[cfg_attr(target_os = "macos", path = "ipc_macos.rs")]
pub(crate) mod ipc;
mod state;
#[allow(clippy::module_inception)]
Expand Down Expand Up @@ -51,6 +52,13 @@ define_enum!(
SwitchMode
]
);
#[cfg(target_os = "macos")]
define_enum!(
pub ClashSrvOp,
[
SwitchMode
]
);
#[cfg(target_os = "windows")]
define_enum!(
pub ClashSrvOp,
Expand Down
2 changes: 1 addition & 1 deletion clashtui/backend/src/utils/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl core::fmt::Display for State {
.as_ref()
.map_or("Unknown".to_string(), |v| format!("{}", v)),
);
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
let status_str = write!(
f,
"Profile: {} Mode: {} Tun: {} Help: ?",
Expand Down
3 changes: 1 addition & 2 deletions clashtui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ fn main() {
println!("cargo:rerun-if-changed=../.git/refs/heads/dev");
println!("cargo:rerun-if-changed=build.rs",);

if env::var("CLASHTUI_VERSION").is_ok() {
} else {
if env::var("CLASHTUI_VERSION").is_err() {
println!("cargo:rustc-env=CLASHTUI_VERSION={}", get_version());
}
}
4 changes: 3 additions & 1 deletion clashtui/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct PackedArgCommand(ArgCommand);
#[cfg_attr(debug_assertions, derive(Debug))]
enum ArgCommand {
Profile(Profile),
#[cfg(any(target_os = "linux", target_os = "windows"))]
Service(Service),
Mode(Mode),
}
Expand Down Expand Up @@ -156,6 +157,7 @@ pub fn handle_cli(
todo!()
}
},
#[cfg(any(target_os = "linux", target_os = "windows"))]
ArgCommand::Service(Service { command }) => match command {
ServiceCommand::Restart(ServiceRestart { soft }) => {
if soft {
Expand All @@ -170,7 +172,7 @@ pub fn handle_cli(
backend.clash_srv_ctl(crate::utils::ClashSrvOp::StopClashService)
}
},
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
ArgCommand::Mode(Mode { command }) => match command {
ModeCommand::Rule => Ok(backend
.update_state(None, Some(api::Mode::Rule.into()))
Expand Down
2 changes: 2 additions & 0 deletions clashtui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ fn load_app_dir(flags: &mut Flags<Flag>) -> std::path::PathBuf {
.or_else(|_| env::var("HOME").map(|home| format!("{}/.config/clashtui", home)));
#[cfg(target_os = "windows")]
let config_dir_str = env::var("APPDATA").map(|appdata| format!("{}/clashtui", appdata));
#[cfg(target_os = "macos")]
let config_dir_str = env::var("HOME").map(|home| format!("{}/.config/clashtui", home));
PathBuf::from(&config_dir_str.expect("Err loading global config dir"))
}
};
Expand Down
14 changes: 9 additions & 5 deletions clashtui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ impl App {
const TICK_RATE: u64 = 250;
use core::time::Duration;
if flags.contains(Flag::FirstInit) {
self.popup_txt_msg("Welcome to ClashTui(forked)!".to_string());
self.popup_txt_msg(
"Please go to Config Tab to set configs so that program can work properly"
.to_string(),
);
self.popup_txt_msg("Welcome to ClashTui!".to_string());
self.popup_txt_msg(format!(
"Please go to {} to set configs so that program can work properly",
self.util
.home_dir
.join("config.yaml")
.to_str()
.expect(backend::const_err::ERR_PATH_UTF_8)
));
};
err_track
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions clashtui/src/tui/impl_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ fn spawn_open(cmd: &str, path: &Path) -> std::io::Result<()> {
let opendir_cmd_with_path = cmd.replace("%s", path.to_str().unwrap_or(""));
#[cfg(target_os = "windows")]
return spawn("cmd", vec!["/C", opendir_cmd_with_path.as_str()]);
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
spawn("sh", vec!["-c", opendir_cmd_with_path.as_str()])
} else {
#[cfg(target_os = "windows")]
return spawn("cmd", vec!["/C", "start", path.to_str().unwrap_or("")]);
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
spawn("xdg-open", vec![path.to_str().unwrap_or("")])
}
}
7 changes: 6 additions & 1 deletion clashtui/src/tui/tabs/clashsrvctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ impl ClashSrvCtlTab {
operations.set_items(vec![
#[cfg(target_os = "linux")]
ClashSrvOp::SetPermission.into(),
#[cfg(any(target_os = "linux", target_os = "windows"))]
ClashSrvOp::StartClashService.into(),
#[cfg(any(target_os = "linux", target_os = "windows"))]
ClashSrvOp::StopClashService.into(),
ClashSrvOp::SwitchMode.into(),
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -106,6 +108,8 @@ impl super::TabEvent for ClashSrvCtlTab {
// override `Enter`
event_state = if &Keys::Select == key {
let op = ClashSrvOp::from(self.main_list.selected().unwrap().as_str());
#[allow(irrefutable_let_patterns)]
// currently, only [`SwitchMode`] is impl on macos
if let ClashSrvOp::SwitchMode = op {
self.mode_selector.show();
} else {
Expand Down Expand Up @@ -133,6 +137,7 @@ impl super::TabEvent for ClashSrvCtlTab {
self.state.borrow_mut().set_sysproxy(cur);
self.hide_msgpopup();
}
#[allow(unreachable_patterns)] // currently, only [`SwitchMode`] is impl on macos
_ => match self.util.clash_srv_ctl(op) {
Ok(output) => {
self.popup_list_msg(output.lines().map(|line| line.trim().to_string()));
Expand All @@ -142,7 +147,7 @@ impl super::TabEvent for ClashSrvCtlTab {
}
},
}
self.state.borrow_mut().refresh();
self.state.borrow_mut().refresh();
}
}
fn draw(&mut self, f: &mut ratatui::prelude::Frame, area: ratatui::prelude::Rect) {
Expand Down
1 change: 1 addition & 0 deletions clashtui/src/tui/tabs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(refining_impl_trait)]
mod clashsrvctl;
mod profile;
mod profile_input;
Expand Down
6 changes: 3 additions & 3 deletions clashtui/src/utils/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl State {
st: ct.update_state(None, None, None),
ct,
};
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
Self {
st: ct.update_state(None, None),
ct,
Expand All @@ -34,7 +34,7 @@ impl State {
{
self.st = self.ct.update_state(Some(profile), None, None)
}
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
self.st = self.ct.update_state(Some(profile), None)
}
Expand All @@ -44,7 +44,7 @@ impl State {
{
self.st = self.ct.update_state(None, Some(mode), None)
}
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
self.st = self.ct.update_state(None, Some(mode))
}
Expand Down

0 comments on commit 6e65e6a

Please sign in to comment.