Skip to main content

git_commit_helper_cli/
terminal_format.rs

1// 终端彩色输出工具模块
2// 用于统一管理ANSI颜色和结构化输出
3
4use std::io::{self, Write};
5
6pub struct Style;
7
8impl Style {
9    pub const RESET: &'static str = "\x1b[0m";
10    pub const BOLD: &'static str = "\x1b[1m";
11    pub const BLUE: &'static str = "\x1b[34m";
12    pub const BRIGHT_BLUE: &'static str = "\x1b[94m";
13    pub const BRIGHT_GREEN: &'static str = "\x1b[92m";
14    pub const BRIGHT_YELLOW: &'static str = "\x1b[93m";
15    pub const BRIGHT_RED: &'static str = "\x1b[91m";
16    pub const GRAY: &'static str = "\x1b[90m";
17
18    // 分隔线
19    pub fn separator() -> String {
20        format!("{}{}───────────────────────────────────────────────{}\n", Self::GRAY, Self::BOLD, Self::RESET)
21    }
22
23    // 标题
24    pub fn title(text: &str) -> String {
25        format!("{}{}{}{}{}", Self::BRIGHT_BLUE, Self::BOLD, text, Self::RESET, "\n")
26    }
27
28    // 绿色内容(如翻译/成功)
29    pub fn green(text: &str) -> String {
30        format!("{}{}{}{}", Self::BRIGHT_GREEN, text, Self::RESET, "\n")
31    }
32
33    // 蓝色内容(如描述)
34    pub fn blue(text: &str) -> String {
35        format!("{}{}{}{}", Self::BLUE, text, Self::RESET, "\n")
36    }
37
38    // 黄色内容(如警告/代码审查报告标题)
39    pub fn yellow(text: &str) -> String {
40        format!("{}{}{}{}", Self::BRIGHT_YELLOW, text, Self::RESET, "\n")
41    }
42
43    // 红色内容(如错误)
44    pub fn red(text: &str) -> String {
45        format!("{}{}{}{}", Self::BRIGHT_RED, text, Self::RESET, "\n")
46    }
47
48    // 普通正文
49    pub fn plain(text: &str) -> String {
50        format!("{}{}", text, "\n")
51    }
52}
53
54/// 进度提示工具函数
55/// 示例:print_progress("正在请求 github.com 获取PR内容", Some(30));
56///      print_progress("正在请求 github.com 获取PR内容", Some(100));
57///      print_progress("正在请求 api.openai.com 进行代码审查", None);
58pub fn print_progress(msg: &str, percent: Option<u8>) {
59    // 构造进度文本
60    let progress = match percent {
61        Some(p) => format!(" {}%...", p),
62        None => " ...".to_string(),
63    };
64    // \r回到行首,补足空格清除残留
65    let text = format!("\r{}{}{}", msg, progress, "      ");
66    print!("{}", text);
67    io::stdout().flush().ok();
68}