ziro 0.0.11

跨平台端口管理工具 - 快速查找和终止占用端口的进程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::file::FileInfo;
use crate::icons::icons;
use crate::port::PortInfo;
use anyhow::Result;
use colored::*;
use inquire::{Confirm, MultiSelect};

/// 显示端口未被占用的消息
pub fn display_port_not_found(port: u16) {
    println!("{}", format!("端口 {port} 未被占用").yellow());
}

/// 显示多个端口信息(交互式选择)
pub fn select_processes_to_kill(port_infos: Vec<PortInfo>) -> Result<Vec<PortInfo>> {
    if port_infos.is_empty() {
        println!("{}", "未找到任何占用指定端口的进程".yellow());
        return Ok(vec![]);
    }

    let options: Vec<String> = port_infos
        .iter()
        .map(|info| {
            format!(
                "端口 {} - {} (PID: {}) - {}",
                info.port,
                info.process.name,
                info.process.pid,
                info.process.cmd.join(" ")
            )
        })
        .collect();

    // 默认全选(使用索引数组)
    let defaults: Vec<usize> = (0..options.len()).collect();

    let selected = MultiSelect::new("选择要终止的进程:", options)
        .with_default(&defaults)
        .prompt()?;

    // 找出被选中的进程
    let mut result = Vec::new();
    for selection in selected {
        for info in &port_infos {
            let expected = format!(
                "端口 {} - {} (PID: {}) - {}",
                info.port,
                info.process.name,
                info.process.pid,
                info.process.cmd.join(" ")
            );
            if selection == expected {
                result.push(info.clone());
                break;
            }
        }
    }

    if result.is_empty() {
        println!("{}", "未选择任何进程".yellow());
        return Ok(vec![]);
    }

    // 确认操作
    let confirm = Confirm::new("确认终止这些进程?")
        .with_default(false)
        .prompt()?;

    if confirm {
        Ok(result)
    } else {
        println!("{}", "操作已取消".yellow());
        Ok(vec![])
    }
}

/// 显示终止结果
pub fn display_kill_results(results: &[(u32, Result<()>)]) {
    for (pid, result) in results {
        match result {
            Ok(()) => println!(
                "{} {}",
                icons().check().green(),
                format!("成功终止进程 {pid}").green()
            ),
            Err(e) => println!(
                "{} {}: {}",
                icons().cross().red(),
                format!("无法终止进程 {pid}").red(),
                e
            ),
        }
    }
}

/// 截断字符串到指定长度
fn truncate_string(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

/// 显示错误信息
pub fn display_error(error: &anyhow::Error) {
    eprintln!("{} {}", "错误:".red().bold(), error);
}

/// 树形结构展示多个端口信息
pub fn display_ports_tree(ports: &[u16], port_infos: Vec<PortInfo>) {
    if ports.is_empty() {
        return;
    }

    println!(
        "{} {}",
        icons().lightning().cyan(),
        "端口查询结果".cyan().bold()
    );
    println!();

    // 创建端口到进程信息的映射
    let mut port_map = std::collections::HashMap::new();
    for info in port_infos {
        port_map.insert(info.port, info);
    }

    let total = ports.len();
    for (index, &port) in ports.iter().enumerate() {
        let is_last = index == total - 1;
        let branch = if is_last { "└─" } else { "├─" };
        let continuation = if is_last { "   " } else { "" };

        if let Some(info) = port_map.get(&port) {
            // 端口被占用
            println!(
                "{} {} {}",
                branch,
                format!("{port}").yellow().bold(),
                icons().check().green()
            );

            // 进程信息
            println!(
                "{}├─ {}: {} ({})",
                continuation,
                "进程".cyan(),
                info.process.name.green(),
                format!("{}", info.process.pid).bright_black()
            );

            // 命令
            let cmd = truncate_string(&info.process.cmd.join(" "), 60);
            println!(
                "{}├─ {}: {}",
                continuation,
                "命令".cyan(),
                cmd.bright_black()
            );

            // 资源使用
            println!(
                "{}└─ {}: {} CPU, {} 内存",
                continuation,
                "资源".cyan(),
                format!("{:.1}%", info.process.cpu_usage).magenta(),
                format!("{} MB", info.process.memory / 1024 / 1024).magenta()
            );
        } else {
            // 端口空闲
            println!(
                "{} {} {} {}",
                branch,
                format!("{port}").yellow().bold(),
                icons().cross().red(),
                "(空闲)".bright_black()
            );
        }

        if !is_last {
            println!("{continuation}");
        }
    }
}

/// 树形结构展示所有端口占用情况(用于 list 命令)
pub fn display_ports_tree_all(port_infos: Vec<PortInfo>) {
    if port_infos.is_empty() {
        println!("{}", "当前没有端口被占用".yellow());
        return;
    }

    println!(
        "{} {} {}",
        icons().lightning().cyan(),
        "端口占用情况".cyan().bold(),
        format!("(共 {} 个)", port_infos.len()).bright_black()
    );
    println!();

    let total = port_infos.len();
    for (index, info) in port_infos.iter().enumerate() {
        let is_last = index == total - 1;
        let branch = if is_last { "└─" } else { "├─" };
        let continuation = if is_last { "   " } else { "" };

        // 端口号和状态
        println!(
            "{} {} {}",
            branch,
            format!("{}", info.port).yellow().bold(),
            icons().check().green()
        );

        // 进程信息
        println!(
            "{}├─ {}: {} ({})",
            continuation,
            "进程".cyan(),
            info.process.name.green(),
            format!("{}", info.process.pid).bright_black()
        );

        // 命令
        let cmd = truncate_string(&info.process.cmd.join(" "), 60);
        println!(
            "{}├─ {}: {}",
            continuation,
            "命令".cyan(),
            cmd.bright_black()
        );

        // 资源使用
        println!(
            "{}└─ {}: {} CPU, {} 内存",
            continuation,
            "资源".cyan(),
            format!("{:.1}%", info.process.cpu_usage).magenta(),
            format!("{} MB", info.process.memory / 1024 / 1024).magenta()
        );

        if !is_last {
            println!("{continuation}");
        }
    }
}

/// 显示删除预览
pub fn display_deletion_preview(files: &[FileInfo]) {
    let total_size: u64 = files.iter().map(|f| f.size).sum();
    let (file_count, dir_count) = files.iter().fold((0, 0), |(files, dirs), f| {
        if f.is_dir {
            (files, dirs + 1)
        } else {
            (files + 1, dirs)
        }
    });

    println!(
        "{} {} {} {}",
        "统计:".cyan().bold(),
        format!("{file_count} 个文件").green(),
        format!("{dir_count} 个目录").blue(),
        format!("总大小: {}", crate::file::format_size(total_size)).yellow()
    );
    println!();

    // 显示文件列表预览
    let total = files.len().min(10); // 最多显示10个项目
    for file in files.iter().take(total) {
        let icon = if file.is_dir {
            icons().folder().to_string()
        } else if file.is_symlink {
            icons().link().to_string()
        } else {
            icons().file().to_string()
        };

        let size_str = if !file.is_dir && !file.is_symlink {
            format!(" ({})", crate::file::format_size(file.size))
        } else {
            String::new()
        };

        let file_type = if file.is_dir {
            "目录".blue()
        } else if file.is_symlink {
            "符号链接".magenta()
        } else {
            "文件".green()
        };

        println!(
            "  {} {} {}{}",
            icon,
            file.path.display(),
            file_type,
            size_str.bright_black()
        );
    }

    if files.len() > 10 {
        println!("  ... 还有 {} 个项目", files.len() - 10);
    }

    println!();
}

/// 确认删除操作
pub fn confirm_deletion(files: &[FileInfo], force: bool, dry_run: bool) -> Result<bool> {
    if dry_run {
        println!(
            "{} {}",
            icons().search().blue(),
            "预览模式 - 不会实际删除文件".blue().bold()
        );
        display_deletion_preview(files);
        return Ok(true);
    }

    if force {
        return Ok(true);
    }

    println!(
        "{} {}",
        icons().warning().red(),
        "即将删除以下内容".red().bold()
    );
    display_deletion_preview(files);

    let confirm = Confirm::new("确认删除这些内容?此操作不可撤销!")
        .with_default(false)
        .with_help_message("使用 --force 参数可以跳过此确认")
        .prompt()?;

    Ok(confirm)
}

/// 显示删除结果
pub fn display_removal_results(
    results: &[(std::path::PathBuf, Result<()>)],
    dry_run: bool,
    verbose: bool,
) {
    let action = if dry_run { "预览删除" } else { "删除" };
    let (success_count, error_count) =
        results
            .iter()
            .fold((0, 0), |(success, error), (_, result)| {
                if result.is_ok() {
                    (success + 1, error)
                } else {
                    (success, error + 1)
                }
            });

    // 如果不是 verbose 模式,只显示汇总信息
    if !verbose {
        println!(
            "{} {} {}",
            "操作完成".cyan().bold(),
            format!("成功: {success_count}").green(),
            format!("失败: {error_count}").red()
        );

        // 只有在错误模式下才显示失败的文件
        if error_count > 0 {
            for (path, result) in results {
                if let Err(e) = result {
                    println!(
                        "{} {} {}",
                        icons().cross().red(),
                        format!("无法删除 {}", path.display()).red(),
                        e
                    );
                }
            }
        }
        return;
    }

    // Verbose 模式:显示所有详细信息
    println!(
        "{} {} {}",
        "操作完成".cyan().bold(),
        format!("成功: {success_count}").green(),
        format!("失败: {error_count}").red()
    );

    for (path, result) in results {
        match result {
            Ok(()) => println!(
                "{} {}",
                icons().check().green(),
                format!("{} {}", action, path.display()).bright_black()
            ),
            Err(e) => println!(
                "{} {} {}",
                icons().cross().red(),
                format!("无法删除 {}", path.display()).red(),
                e
            ),
        }
    }
}

/// 显示强制终止结果
pub fn display_kill_results_force(port_infos: &[PortInfo], results: &[(u32, Result<()>)]) {
    println!("{} {}", icons().fire().red(), "强制终止进程".red().bold());
    println!();

    // 首先显示要终止的进程信息
    println!("{}", "目标进程:".cyan().bold());
    for info in port_infos {
        println!(
            "  端口 {} - {} (PID: {})",
            info.port.to_string().yellow(),
            info.process.name.green(),
            format!("{}", info.process.pid).bright_black()
        );
    }
    println!();

    // 显示终止结果
    println!("{}", "终止结果:".cyan().bold());
    let mut success_count = 0;
    let mut error_count = 0;

    for (pid, result) in results {
        match result {
            Ok(()) => {
                success_count += 1;
                println!(
                    "{} {}",
                    icons().check().green(),
                    format!("成功强制终止进程 {pid}").green()
                );
            }
            Err(e) => {
                error_count += 1;
                println!(
                    "{} {}: {}",
                    icons().cross().red(),
                    format!("无法强制终止进程 {pid}").red(),
                    e
                );
            }
        }
    }

    println!();
    println!(
        "{} {} {}",
        "强制终止完成".cyan().bold(),
        format!("成功: {success_count}").green(),
        format!("失败: {error_count}").red()
    );
}