ziro 0.0.20

跨平台端口管理工具 - 快速查找和终止占用端口的进程
Documentation
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
//! 图标管理模块
//!
//! 提供跨平台的图标支持:优先 Unicode Emoji,其次窄字符符号,最后 ASCII 回退。

use std::env;

#[derive(Clone, Copy, Debug)]
enum IconMode {
    Unicode,
    Narrow,
    Ascii,
}

/// 图标管理器
pub struct Icons {
    mode: IconMode,
}

/// 三档图标(Unicode / 窄字符 / ASCII)
#[derive(Clone, Copy)]
pub struct IconGlyph {
    unicode: &'static str,
    narrow: &'static str,
    ascii: &'static str,
}

/// 预定义的安全图标
pub struct SafeIcons;

impl SafeIcons {
    /// 成功/完成标记
    pub const CHECK: IconGlyph = IconGlyph {
        unicode: "\u{2714}",
        narrow: "\u{2713}",
        ascii: "+",
    };

    /// 错误/失败标记
    pub const CROSS: IconGlyph = IconGlyph {
        unicode: "\u{2716}",
        narrow: "\u{00D7}",
        ascii: "x",
    };

    /// 闪电/端口相关
    pub const LIGHTNING: IconGlyph = IconGlyph {
        unicode: "\u{26A1}",
        narrow: "*",
        ascii: "*",
    };

    /// 搜索/查找
    pub const SEARCH: IconGlyph = IconGlyph {
        unicode: "\u{1F50D}",
        narrow: "?",
        ascii: "?",
    };

    /// 警告
    pub const WARNING: IconGlyph = IconGlyph {
        unicode: "\u{26A0}",
        narrow: "!",
        ascii: "!",
    };

    /// 火/强制终止
    pub const FIRE: IconGlyph = IconGlyph {
        unicode: "\u{1F525}",
        narrow: "!",
        ascii: "!",
    };

    /// 文件夹
    pub const FOLDER: IconGlyph = IconGlyph {
        unicode: "\u{1F4C2}",
        narrow: "[D]",
        ascii: "[D]",
    };

    /// 文件
    pub const FILE: IconGlyph = IconGlyph {
        unicode: "\u{1F4C4}",
        narrow: "[F]",
        ascii: "[F]",
    };

    /// 链接
    pub const LINK: IconGlyph = IconGlyph {
        unicode: "\u{1F517}",
        narrow: "->",
        ascii: "->",
    };
}

impl Default for Icons {
    fn default() -> Self {
        Self::new()
    }
}

impl Icons {
    /// 创建新的图标管理器实例
    pub fn new() -> Self {
        let mode = Self::detect_mode();
        Self { mode }
    }

    /// 检测终端/配置选择哪个图标档位
    fn detect_mode() -> IconMode {
        // 显式纯文本模式:ASCII
        if is_truthy_env("ZIRO_PLAIN") {
            return IconMode::Ascii;
        }

        // 强制 ASCII
        if is_truthy_env("ZIRO_ASCII_ICONS") {
            return IconMode::Ascii;
        }

        // 强制 Unicode
        if is_truthy_env("ZIRO_UNICODE_ICONS") {
            return IconMode::Unicode;
        }

        // 强制窄字符(单宽符号)
        if is_truthy_env("ZIRO_NARROW") {
            return IconMode::Narrow;
        }

        // 如果不是 UTF-8/65001,优先用 ASCII,避免乱码
        if is_likely_non_utf8() {
            return IconMode::Ascii;
        }

        // 基于终端能力的默认选择
        if Self::detect_unicode_support() {
            IconMode::Unicode
        } else {
            IconMode::Ascii
        }
    }

    /// 检测终端是否支持 Unicode emoji
    fn detect_unicode_support() -> bool {
        // 首先检查明确的语言环境设置
        if let Ok(locale) = env::var("LC_ALL").or_else(|_| env::var("LANG"))
            && (locale.to_lowercase().contains("utf-8") || locale.contains("65001"))
        {
            return true;
        }

        // 检查终端类型
        if let Ok(term) = env::var("TERM") {
            let term = term.to_lowercase();

            // 明确支持 Unicode 的现代终端
            if term.contains("xterm")
                || term.contains("screen")
                || term.contains("tmux")
                || term.contains("alacritty")
                || term.contains("kitty")
                || term.contains("iterm")
                || term.contains("gnome")
                || term.contains("konsole")
                || term.contains("rxvt")
                || term.contains("st")
            {
                return true;
            }

            // 对于 Windows 特有的终端类型,需要更仔细的判断
            if cfg!(target_os = "windows") {
                if term.contains("cygwin") || term.contains("msys") || term.contains("mingw") {
                    // 这些终端通常支持 Unicode
                    return true;
                } else if term.contains("win32")
                    || term.contains("conhost")
                    || term.contains("dumb")
                {
                    // 保守策略:传统 Windows 控制台可能不支持 Unicode emoji
                    return false;
                }
            }
        }

        // Windows 特定检测
        if cfg!(target_os = "windows") {
            // Windows Terminal 检测
            if let Ok(wt_session) = env::var("WT_SESSION") {
                return !wt_session.is_empty();
            }

            // 检查终端程序
            if let Ok(term_program) = env::var("TERM_PROGRAM") {
                let term_program = term_program.to_lowercase();
                if [
                    "vscode",
                    "hyper",
                    "terminus",
                    "windowsterminal",
                    "wt",
                    "warp",
                    "warpterminal",
                ]
                .contains(&term_program.as_str())
                {
                    return true;
                }
            }

            // 检查 Shell 环境(Git Bash, WSL 等)
            if let Ok(shell) = env::var("SHELL") {
                if shell.contains("bash") || shell.contains("zsh") || shell.contains("fish") {
                    return true;
                }
            }

            // 检查 Windows Terminal 安装路径
            if let Ok(program_files) = env::var("ProgramFiles") {
                let wt_path = std::path::Path::new(&program_files)
                    .join("WindowsApps")
                    .join("Microsoft.WindowsTerminal");
                if wt_path.exists() {
                    return true;
                }
            }

            // 检查本地应用数据中的 Windows Terminal
            if let Ok(local_app_data) = env::var("LOCALAPPDATA") {
                let wt_path = std::path::Path::new(&local_app_data)
                    .join("Microsoft")
                    .join("WindowsApps");
                if wt_path.exists() && wt_path.join("Microsoft.WindowsTerminal").exists() {
                    return true;
                }
            }

            // 检查增强终端支持
            if env::var("ConEmuANSI").is_ok() || env::var("ANSICON").is_ok() {
                return true;
            }

            // 默认情况下,现代 Windows 系统倾向于支持 Unicode
            // 除非明确检测到传统控制台
            if let Ok(term) = env::var("TERM") {
                if !term.is_empty() && !term.contains("win32") && !term.contains("conhost") {
                    return true;
                }
            }
        }

        // 非 Windows 系统的默认行为
        #[cfg(not(target_os = "windows"))]
        {
            // 现代 Unix/Linux 系统几乎都支持 Unicode
            true
        }

        #[cfg(target_os = "windows")]
        {
            // Windows 的默认行为:如果有 TERM 变量,通常支持 Unicode
            env::var("TERM").is_ok() && !env::var("TERM").unwrap_or_default().is_empty()
        }
    }

    pub fn check(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::CHECK, self.mode)
    }

    pub fn cross(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::CROSS, self.mode)
    }

    pub fn lightning(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::LIGHTNING, self.mode)
    }

    pub fn search(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::SEARCH, self.mode)
    }

    pub fn warning(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::WARNING, self.mode)
    }

    pub fn fire(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::FIRE, self.mode)
    }

    pub fn folder(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::FOLDER, self.mode)
    }

    pub fn file(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::FILE, self.mode)
    }

    pub fn link(&self) -> StyledEmoji {
        StyledEmoji::new(SafeIcons::LINK, self.mode)
    }
}

fn is_truthy_env(key: &str) -> bool {
    if let Ok(v) = env::var(key) {
        let v = v.to_lowercase();
        return matches!(v.as_str(), "1" | "true" | "yes" | "on");
    }
    false
}

fn is_likely_non_utf8() -> bool {
    if cfg!(target_os = "windows") {
        // Windows Terminal 或现代终端通常支持 Unicode
        if env::var("WT_SESSION")
            .map(|v| !v.is_empty())
            .unwrap_or(false)
        {
            return false;
        }

        // 检查终端程序
        if let Ok(term_program) = env::var("TERM_PROGRAM") {
            let term_program = term_program.to_lowercase();
            if [
                "vscode",
                "hyper",
                "terminus",
                "windowsterminal",
                "wt",
                "warp",
                "warpterminal",
            ]
            .contains(&term_program.as_str())
            {
                return false;
            }
        }

        // LANG/LC_ALL 包含 utf-8 时认为可用
        let locale = env::var("LC_ALL")
            .or_else(|_| env::var("LANG"))
            .unwrap_or_default()
            .to_lowercase();
        if locale.contains("utf-8") || locale.contains("65001") {
            return false;
        }

        // 检查 TERM 变量
        if let Ok(term) = env::var("TERM") {
            let term = term.to_lowercase();
            // 现代终端类型 - 更积极的识别
            if term.contains("xterm")
                || term.contains("screen")
                || term.contains("tmux")
                || term.contains("alacritty")
                || term.contains("kitty")
                || term.contains("iterm")
                || term.contains("gnome")
                || term.contains("konsole")
            {
                return false;
            }
            // 传统 Windows 控制台
            if term.contains("win32") || term.contains("conhost") || term.contains("dumb") {
                return true;
            }
        }

        // 检查增强终端支持
        if env::var("ConEmuANSI").is_ok() || env::var("ANSICON").is_ok() {
            return false;
        }

        // 检查是否在 Git Bash、WSL 等环境中
        if let Ok(shell) = env::var("SHELL") {
            if shell.contains("bash") || shell.contains("zsh") || shell.contains("fish") {
                return false;
            }
        }

        // 检查 Windows Terminal 安装路径
        if let Ok(program_files) = env::var("ProgramFiles") {
            let wt_path = std::path::Path::new(&program_files)
                .join("WindowsApps")
                .join("Microsoft.WindowsTerminal");
            if wt_path.exists() {
                return false;
            }
        }

        // 改进的回退策略:只有在明确检测到传统控制台时才认为是非 UTF-8
        // 其他情况(包括空 TERM 变量)都倾向于支持 Unicode
        return false;
    }

    // 非 Windows:检查 LANG/LC_ALL 是否包含 UTF-8
    let locale = env::var("LC_ALL")
        .or_else(|_| env::var("LANG"))
        .unwrap_or_default()
        .to_lowercase();

    // 如果没有明确的 locale 信息,保守地认为支持 UTF-8
    if locale.is_empty() {
        return false;
    }

    !locale.contains("utf-8")
}

/// 带样式的图标包装器
pub struct StyledEmoji {
    glyph: IconGlyph,
    mode: IconMode,
}

impl StyledEmoji {
    fn new(glyph: IconGlyph, mode: IconMode) -> Self {
        Self { glyph, mode }
    }

    pub fn as_str(&self) -> &str {
        match self.mode {
            IconMode::Unicode => self.glyph.unicode,
            IconMode::Narrow => self.glyph.narrow,
            IconMode::Ascii => self.glyph.ascii,
        }
    }
}

impl std::fmt::Display for StyledEmoji {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// 获取图标管理器实例
pub fn icons() -> Icons {
    Icons::new()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_icon_creation() {
        let icons = Icons::new();
        let check = icons.check();
        assert!(!check.as_str().is_empty());
    }
}