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