diff --git a/Cargo.lock b/Cargo.lock index 4896fb0..1a010ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -233,6 +233,19 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "console" +version = "0.15.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8" +dependencies = [ + "encode_unicode", + "libc", + "once_cell", + "unicode-width", + "windows-sys 0.59.0", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -290,6 +303,12 @@ dependencies = [ "syn", ] +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -340,16 +359,18 @@ dependencies = [ [[package]] name = "fastcommit" -version = "0.3.0" +version = "0.4.0" dependencies = [ "anyhow", "chrono", "clap", "dirs", "env_logger", + "indicatif", "lazy_static", "log", "openai_api_rust", + "rand", "regex", "reqwest", "serde", @@ -795,6 +816,19 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indicatif" +version = "0.17.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "183b3088984b400f4cfac3620d5e076c84da5364016b4f49473de574b2586235" +dependencies = [ + "console", + "number_prefix", + "portable-atomic", + "unicode-width", + "web-time", +] + [[package]] name = "ipnet" version = "2.11.0" @@ -931,6 +965,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + [[package]] name = "object" version = "0.36.7" @@ -1056,6 +1096,12 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" +[[package]] +name = "portable-atomic" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -1693,6 +1739,12 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "unicode-width" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c" + [[package]] name = "untrusted" version = "0.9.0" @@ -1857,6 +1909,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki-roots" version = "0.26.8" diff --git a/Cargo.toml b/Cargo.toml index 6a99740..d40d4cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fastcommit" -version = "0.3.0" +version = "0.4.0" description = "AI-based command line tool to quickly generate standardized commit messages." edition = "2021" authors = ["longjin "] @@ -22,4 +22,6 @@ reqwest = { version = "0.12.9", features = ["json"] } serde = { version = "1.0.218", features = ["derive"] } serde_json = "1.0.134" tokio = { version = "1.43.0", features = ["full"] } +rand = "0.8.5" +indicatif = "0.17.8" toml = "0.8.20" diff --git a/README.md b/README.md index ecd3dfe..954bce3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ You can install `fastcommit` using the following method: ```bash # Install using cargo -cargo install --git https://github.com/fslongjin/fastcommit --tag v0.3.0 +cargo install --git https://github.com/fslongjin/fastcommit --tag v0.4.0 ``` ## Usage diff --git a/README_CN.md b/README_CN.md index 4d88c7c..aaf8102 100644 --- a/README_CN.md +++ b/README_CN.md @@ -8,7 +8,7 @@ ```bash # 使用 cargo 安装 -cargo install --git https://github.com/fslongjin/fastcommit --tag v0.3.0 +cargo install --git https://github.com/fslongjin/fastcommit --tag v0.4.0 ``` ## 使用 diff --git a/src/animation.rs b/src/animation.rs new file mode 100644 index 0000000..7046806 --- /dev/null +++ b/src/animation.rs @@ -0,0 +1,69 @@ +use indicatif::{ProgressBar, ProgressStyle}; +use rand::seq::SliceRandom; +use std::time::Duration; + +const RANDOM_PHRASES: &[&str] = &[ + "🤔 正在思考如何优雅地描述这次变更...", + "✨ 魔法正在发生,请稍候...", + "🚀 加速代码提交中...", + "🧠 AI大脑正在疯狂运转...", + "🌈 生成彩虹般绚丽的提交信息...", + "⚡ 闪电般快速分析代码...", + "🎨 为你的代码添加艺术气息...", + "🕺 Gee Kee Tai May! Oh Baby! ", + "💃 Gee Kee 实在是太美! ", + "🎪 代码马戏团表演中...", + "🍵 喝杯茶,马上就好...", + "🎸 为代码变更谱写乐章...", + "🏎️ 极速代码分析中...", + "🎭 戏剧化地描述你的变更...", + "🌌 在代码宇宙中探索ing...", +]; + +pub struct Spinner { + pb: ProgressBar, +} + +impl Spinner { + pub fn new() -> Self { + let pb = ProgressBar::new_spinner(); + pb.set_style( + ProgressStyle::default_spinner() + .tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]) + .template("{spinner:.cyan} {msg:.dim}") + .unwrap(), + ); + pb.enable_steady_tick(Duration::from_millis(100)); + + Self { pb } + } + + pub async fn start_with_random_messages(&self) { + let pb = self.pb.clone(); + tokio::spawn(async move { + let mut interval = tokio::time::interval(Duration::from_secs(2)); + loop { + interval.tick().await; + let mut rng = rand::thread_rng(); + if let Some(phrase) = RANDOM_PHRASES.choose(&mut rng) { + pb.set_message(phrase.to_string()); + } + } + }); + } + + #[allow(dead_code)] + pub fn finish_with_message(&self, message: &str) { + self.pb.finish_with_message(message.to_string()); + } + + pub fn finish(&self) { + self.pb.finish_and_clear(); + } +} + +impl Drop for Spinner { + fn drop(&mut self) { + self.finish(); + } +} diff --git a/src/main.rs b/src/main.rs index 1c4131b..712b62e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; use log::error; +mod animation; mod cli; mod config; mod constants; @@ -12,6 +13,11 @@ mod update_checker; #[tokio::main] async fn main() -> anyhow::Result<()> { env_logger::init(); + + // 启动spinner动画 + let spinner = animation::Spinner::new(); + spinner.start_with_random_messages().await; + let args = cli::Args::parse(); let mut config = config::load_config().await?; @@ -36,16 +42,23 @@ async fn main() -> anyhow::Result<()> { // 1. --gb --m 同时:生成分支名 + 提交信息 // 2. 仅 --gb:只生成分支名 // 3. 默认(无 --gb 或仅 --m):生成提交信息 + if args.generate_branch && args.generate_message { let (branch_name, msg) = generate::generate_both(&args, &config).await?; + // 停止spinner动画 + spinner.finish(); println!("Generated branch name: {}", branch_name); println!("{}", msg); } else if args.generate_branch { let branch_name = generate::generate_branch(&args, &config).await?; + // 停止spinner动画 + spinner.finish(); println!("Generated branch name: {}", branch_name); } else { // 包括:无参数 或 仅 --m let msg = generate::generate(&args, &config).await?; + // 停止spinner动画 + spinner.finish(); println!("{}", msg); } Ok(())