This commit is contained in:
Julian Freeman
2026-03-30 23:06:38 -04:00
parent a7b5955540
commit 7f2dde8c51

View File

@@ -202,14 +202,16 @@ pub fn run() {
let size_re = Regex::new(r"([\d\.]+)\s*[a-zA-Z]+\s*/\s*([\d\.]+)\s*[a-zA-Z]+").unwrap();
while let Some(task) = rx.recv().await {
let id = task.id;
let version = task.version;
let task_id = task.id.clone();
let task_version = task.version.clone();
let use_manifest = task.use_manifest;
let manifest_url = task.manifest_url;
let manifest_url = task.manifest_url.clone();
let log_id = format!("install-{}", id);
let log_id = format!("install-{}", task_id);
// 1. 发送正在安装状态
let _ = handle.emit("install-status", InstallProgress {
id: id.clone(),
id: task_id.clone(),
status: "installing".to_string(),
progress: 0.0,
});
@@ -221,26 +223,25 @@ pub fn run() {
let url = manifest_url.unwrap();
args.push("--manifest".to_string());
args.push(url.clone());
display_cmd = format!("Winget Install (Manifest): {} from {}", id, url);
display_cmd = format!("Winget Install (Manifest): {} from {}", task_id, url);
} else {
args.push("--id".to_string());
args.push(id.clone());
args.push(task_id.clone());
args.push("-e".to_string());
if let Some(v) = &version {
if let Some(v) = &task_version {
if !v.is_empty() {
args.push("--version".to_string());
args.push(v.clone());
}
}
display_cmd = match &version {
Some(v) if !v.is_empty() => format!("Winget Install: {} (v{})", id, v),
_ => format!("Winget Install: {}", id),
display_cmd = match &task_version {
Some(v) if !v.is_empty() => format!("Winget Install: {} (v{})", task_id, v),
_ => format!("Winget Install: {}", task_id),
};
}
// 共同的静默与协议参数
args.extend([
"--silent".to_string(),
"--accept-package-agreements".to_string(),
@@ -250,8 +251,8 @@ pub fn run() {
emit_log(&handle, &log_id, &display_cmd, "Starting...", "info");
let id_for_progress = id.clone();
let h = handle.clone();
let current_id = task_id.clone();
let child = Command::new("winget")
.args(&args)
@@ -264,31 +265,29 @@ pub fn run() {
Ok(mut child_proc) => {
if let Some(stdout) = child_proc.stdout.take() {
let reader = BufReader::new(stdout);
// 使用 split(b'\r') 是为了捕捉 PowerShell 中的动态进度行
for line_res in reader.split(b'\r') {
if let Ok(line_bytes) = line_res {
let line_str = String::from_utf8_lossy(&line_bytes).to_string();
let clean_line = line_str.trim();
if clean_line.is_empty() { continue; }
let mut is_progress = false;
if let Some(caps) = perc_re.captures(clean_line) {
if let Ok(p_val) = caps[1].parse::<f32>() {
let _ = h.emit("install-status", InstallProgress {
id: id_for_progress.clone(),
id: current_id.clone(),
status: "installing".to_string(),
progress: p_val / 100.0,
});
is_progress = true;
}
}
else if let Some(caps) = size_re.captures(clean_line) {
} else if let Some(caps) = size_re.captures(clean_line) {
let current = caps[1].parse::<f32>().unwrap_or(0.0);
let total = caps[2].parse::<f32>().unwrap_or(1.0);
if total > 0.0 {
let _ = h.emit("install-status", InstallProgress {
id: id_for_progress.clone(),
id: current_id.clone(),
status: "installing".to_string(),
progress: (current / total).min(1.0),
});
@@ -296,14 +295,12 @@ pub fn run() {
}
}
// 净化日志:过滤进度行、单字符动画行以及退格符
if !is_progress && clean_line.chars().count() > 1 {
emit_log(&h, &log_id, "", clean_line, "info");
}
}
}
}
let exit_status = child_proc.wait().map(|s| s.success()).unwrap_or(false);
if exit_status { "success" } else { "error" }
},
@@ -313,8 +310,9 @@ pub fn run() {
}
};
// 2. 发送最终完成/失败状态
let _ = handle.emit("install-status", InstallProgress {
id: id.clone(),
id: task_id.clone(),
status: status_result.to_string(),
progress: 1.0,
});