Skip to content

Commit 3ad9539

Browse files
committed
refactor: 清理冗余代码并统一 clipboard 到 core
- host.rs: 合并 capacity_check_with_grace_delay 双 cfg 分支为单函数 - core: 新增 clipboard feature(非默认),统一 clipboard_copy 实现 - cli/tui: 移除各自的 clipboard 代码,改用 sculk_core::clipboard - TunnelConfig: 新增 initial_retries 字段(默认 3),取代硬编码常量 - lib.rs: 删除未使用的 VERSION 常量 - tui: Profile 加载失败时在日志面板显示错误信息
1 parent 67fc49a commit 3ad9539

12 files changed

Lines changed: 24 additions & 81 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
sculk-core = { workspace = true, features = ["persist"] }
7+
sculk-core = { workspace = true, features = ["persist", "clipboard"] }
88
anyhow.workspace = true
9-
arboard.workspace = true
109
clap.workspace = true
1110
tokio.workspace = true
1211
tracing.workspace = true

cli/src/main.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async fn run_command(cli: Cli) -> anyhow::Result<()> {
138138
let quoted = format!("\"{ticket_str}\"");
139139
println!("Ticket: {quoted}");
140140

141-
if clipboard_copy(&quoted) {
141+
if sculk_core::clipboard::clipboard_copy(&quoted) {
142142
println!("(Copied to clipboard)");
143143
}
144144

@@ -258,49 +258,6 @@ fn print_event(event: &TunnelEvent) {
258258
}
259259
}
260260

261-
/// 复制文本到系统剪贴板。
262-
///
263-
/// Linux Wayland 下优先使用 `wl-copy`(fork 后台进程持有内容),
264-
/// 其他平台回退到 arboard。
265-
fn clipboard_copy(text: &str) -> bool {
266-
#[cfg(target_os = "linux")]
267-
{
268-
use std::io::Write;
269-
use std::process::{Command, Stdio};
270-
271-
if std::env::var_os("WAYLAND_DISPLAY").is_some()
272-
&& let Ok(mut child) = Command::new("wl-copy")
273-
.stdin(Stdio::piped())
274-
.stdout(Stdio::null())
275-
.stderr(Stdio::null())
276-
.spawn()
277-
{
278-
if let Some(mut stdin) = child.stdin.take() {
279-
let _ = stdin.write_all(text.as_bytes());
280-
}
281-
return child.wait().is_ok_and(|s| s.success());
282-
}
283-
284-
if std::env::var_os("DISPLAY").is_some()
285-
&& let Ok(mut child) = Command::new("xclip")
286-
.args(["-selection", "clipboard"])
287-
.stdin(Stdio::piped())
288-
.stdout(Stdio::null())
289-
.stderr(Stdio::null())
290-
.spawn()
291-
{
292-
if let Some(mut stdin) = child.stdin.take() {
293-
let _ = stdin.write_all(text.as_bytes());
294-
}
295-
return child.wait().is_ok_and(|s| s.success());
296-
}
297-
}
298-
299-
arboard::Clipboard::new()
300-
.and_then(|mut cb| cb.set_text(text))
301-
.is_ok()
302-
}
303-
304261
#[cfg(test)]
305262
mod tests {
306263
use clap::Parser;

core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2024"
66
[features]
77
default = []
88
ci = []
9+
clipboard = ["dep:arboard"]
910
persist = ["dep:dirs", "dep:rand", "dep:serde", "dep:toml"]
1011

1112
[dependencies]
@@ -14,6 +15,7 @@ iroh.workspace = true
1415
tokio.workspace = true
1516
tracing.workspace = true
1617
url.workspace = true
18+
arboard = { workspace = true, optional = true }
1719
dirs = { workspace = true, optional = true }
1820
rand = { workspace = true, optional = true }
1921
serde = { workspace = true, optional = true }
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! 系统剪贴板写入。
22
3-
/// 复制文本到系统剪贴板。
3+
/// 将文本写入系统剪贴板,成功返回 `true`。
4+
///
5+
/// Linux Wayland 下优先调用 `wl-copy`,X11 下调用 `xclip`,其余平台使用 `arboard`。
46
pub fn clipboard_copy(text: &str) -> bool {
57
#[cfg(target_os = "linux")]
68
{

core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
//! - `TunnelConfig::password` 是应用层校验,不替代传输层加密。
5252
//! - `join` 侧是否自动重连由 `max_retries` 控制。
5353
54+
#[cfg(feature = "clipboard")]
55+
pub mod clipboard;
5456
#[cfg(feature = "persist")]
5557
pub mod persist;
5658
pub mod tunnel;
5759

58-
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
59-
6060
/// Minecraft 服务端标准端口。
6161
pub const DEFAULT_MC_PORT: u16 = 25565;
6262

core/src/tunnel/event.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ pub struct TunnelConfig {
1414
pub event_delay: Duration,
1515
/// 连接密码,`None` 表示不校验。
1616
pub password: Option<String>,
17-
/// 最大重连次数(join 侧):`None` 无限,`Some(0)` 关闭重连。
17+
/// 最大重连次数(join 侧):`None` 无限,`Some(0)` 关闭重连。
1818
pub max_retries: Option<u32>,
19+
/// 首次连接的重试上限(join 侧),默认 3 次。
20+
pub initial_retries: u32,
1921
/// 重连初始退避(仅 join 侧)。
2022
pub base_backoff: Duration,
2123
/// 重连最大退避(仅 join 侧)。
@@ -30,6 +32,7 @@ impl Default for TunnelConfig {
3032
event_delay: Duration::ZERO,
3133
password: None,
3234
max_retries: None,
35+
initial_retries: 3,
3336
base_backoff: Duration::from_millis(500),
3437
max_backoff: Duration::from_secs(30),
3538
max_players: None,

core/src/tunnel/iroh/host.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,11 @@ async fn capacity_check_with_grace(
132132
capacity_check_with_grace_delay(sessions, incoming_id, max_players, FULL_RECHECK_DELAY).await
133133
}
134134

135-
#[cfg(test)]
136-
async fn capacity_check_with_grace_delay(
137-
sessions: Arc<Mutex<HostSessions>>,
138-
incoming_id: EndpointId,
139-
max_players: Option<u32>,
140-
recheck_delay: Duration,
141-
) -> bool {
142-
capacity_check_with_grace_delay_impl(sessions, incoming_id, max_players, recheck_delay).await
143-
}
144-
145-
#[cfg(not(test))]
146135
async fn capacity_check_with_grace_delay(
147136
sessions: Arc<Mutex<HostSessions>>,
148137
incoming_id: EndpointId,
149138
max_players: Option<u32>,
150139
recheck_delay: Duration,
151-
) -> bool {
152-
capacity_check_with_grace_delay_impl(sessions, incoming_id, max_players, recheck_delay).await
153-
}
154-
155-
async fn capacity_check_with_grace_delay_impl(
156-
sessions: Arc<Mutex<HostSessions>>,
157-
incoming_id: EndpointId,
158-
max_players: Option<u32>,
159-
recheck_delay: Duration,
160140
) -> bool {
161141
let Some(max) = max_players else {
162142
return true;

core/src/tunnel/iroh/join.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use super::auth::auth_send;
66
use super::monitor::spawn_path_monitor;
77
use super::transport::bridge;
88

9-
const DEFAULT_INITIAL_RETRIES: u32 = 3;
10-
119
/// Join 侧重连 supervisor。
1210
#[allow(clippy::too_many_arguments)]
1311
pub(super) async fn reconnect_supervisor(
@@ -138,7 +136,7 @@ pub(super) async fn connect_with_retry(
138136
config: &TunnelConfig,
139137
tx: &mpsc::Sender<TunnelEvent>,
140138
) -> anyhow::Result<Connection> {
141-
let max = DEFAULT_INITIAL_RETRIES;
139+
let max = config.initial_retries;
142140
let mut last_err = None;
143141

144142
for attempt in 0..=max {

tui/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
sculk-core = { workspace = true, features = ["persist"] }
7+
sculk-core = { workspace = true, features = ["persist", "clipboard"] }
88
anyhow.workspace = true
9-
arboard.workspace = true
109
tokio.workspace = true
1110
tracing.workspace = true
1211
tracing-subscriber.workspace = true

0 commit comments

Comments
 (0)