Skip to content

Commit 5eaf195

Browse files
authored
添加进度动画指示器并升级版本至0.4.0 (#17)
* feat: 添加进度动画和随机提示消息 引入indicatif和rand依赖,创建动画模块显示进度条和随机提示消息,提升用户体验 Signed-off-by: jinlong <jinlong@tencent.com> * chore: 更新版本号至0.4.0 更新Cargo.toml、Cargo.lock和README文档中的版本号,从0.3.0升级到0.4.0。 Signed-off-by: jinlong <jinlong@tencent.com> --------- Signed-off-by: jinlong <jinlong@tencent.com>
1 parent c1e2e63 commit 5eaf195

6 files changed

Lines changed: 150 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 63 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fastcommit"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
description = "AI-based command line tool to quickly generate standardized commit messages."
55
edition = "2021"
66
authors = ["longjin <fslongjin@vip.qq.com>"]
@@ -22,4 +22,6 @@ reqwest = { version = "0.12.9", features = ["json"] }
2222
serde = { version = "1.0.218", features = ["derive"] }
2323
serde_json = "1.0.134"
2424
tokio = { version = "1.43.0", features = ["full"] }
25+
rand = "0.8.5"
26+
indicatif = "0.17.8"
2527
toml = "0.8.20"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can install `fastcommit` using the following method:
1010

1111
```bash
1212
# Install using cargo
13-
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.3.0
13+
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.4.0
1414
```
1515

1616
## Usage

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
```bash
1010
# 使用 cargo 安装
11-
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.3.0
11+
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.4.0
1212
```
1313

1414
## 使用

src/animation.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use indicatif::{ProgressBar, ProgressStyle};
2+
use rand::seq::SliceRandom;
3+
use std::time::Duration;
4+
5+
const RANDOM_PHRASES: &[&str] = &[
6+
"🤔 正在思考如何优雅地描述这次变更...",
7+
"✨ 魔法正在发生,请稍候...",
8+
"🚀 加速代码提交中...",
9+
"🧠 AI大脑正在疯狂运转...",
10+
"🌈 生成彩虹般绚丽的提交信息...",
11+
"⚡ 闪电般快速分析代码...",
12+
"🎨 为你的代码添加艺术气息...",
13+
"🕺 Gee Kee Tai May! Oh Baby! ",
14+
"💃 Gee Kee 实在是太美! ",
15+
"🎪 代码马戏团表演中...",
16+
"🍵 喝杯茶,马上就好...",
17+
"🎸 为代码变更谱写乐章...",
18+
"🏎️ 极速代码分析中...",
19+
"🎭 戏剧化地描述你的变更...",
20+
"🌌 在代码宇宙中探索ing...",
21+
];
22+
23+
pub struct Spinner {
24+
pb: ProgressBar,
25+
}
26+
27+
impl Spinner {
28+
pub fn new() -> Self {
29+
let pb = ProgressBar::new_spinner();
30+
pb.set_style(
31+
ProgressStyle::default_spinner()
32+
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])
33+
.template("{spinner:.cyan} {msg:.dim}")
34+
.unwrap(),
35+
);
36+
pb.enable_steady_tick(Duration::from_millis(100));
37+
38+
Self { pb }
39+
}
40+
41+
pub async fn start_with_random_messages(&self) {
42+
let pb = self.pb.clone();
43+
tokio::spawn(async move {
44+
let mut interval = tokio::time::interval(Duration::from_secs(2));
45+
loop {
46+
interval.tick().await;
47+
let mut rng = rand::thread_rng();
48+
if let Some(phrase) = RANDOM_PHRASES.choose(&mut rng) {
49+
pb.set_message(phrase.to_string());
50+
}
51+
}
52+
});
53+
}
54+
55+
#[allow(dead_code)]
56+
pub fn finish_with_message(&self, message: &str) {
57+
self.pb.finish_with_message(message.to_string());
58+
}
59+
60+
pub fn finish(&self) {
61+
self.pb.finish_and_clear();
62+
}
63+
}
64+
65+
impl Drop for Spinner {
66+
fn drop(&mut self) {
67+
self.finish();
68+
}
69+
}

src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clap::Parser;
22
use log::error;
33

4+
mod animation;
45
mod cli;
56
mod config;
67
mod constants;
@@ -12,6 +13,11 @@ mod update_checker;
1213
#[tokio::main]
1314
async fn main() -> anyhow::Result<()> {
1415
env_logger::init();
16+
17+
// 启动spinner动画
18+
let spinner = animation::Spinner::new();
19+
spinner.start_with_random_messages().await;
20+
1521
let args = cli::Args::parse();
1622
let mut config = config::load_config().await?;
1723

@@ -36,16 +42,23 @@ async fn main() -> anyhow::Result<()> {
3642
// 1. --gb --m 同时:生成分支名 + 提交信息
3743
// 2. 仅 --gb:只生成分支名
3844
// 3. 默认(无 --gb 或仅 --m):生成提交信息
45+
3946
if args.generate_branch && args.generate_message {
4047
let (branch_name, msg) = generate::generate_both(&args, &config).await?;
48+
// 停止spinner动画
49+
spinner.finish();
4150
println!("Generated branch name: {}", branch_name);
4251
println!("{}", msg);
4352
} else if args.generate_branch {
4453
let branch_name = generate::generate_branch(&args, &config).await?;
54+
// 停止spinner动画
55+
spinner.finish();
4556
println!("Generated branch name: {}", branch_name);
4657
} else {
4758
// 包括:无参数 或 仅 --m
4859
let msg = generate::generate(&args, &config).await?;
60+
// 停止spinner动画
61+
spinner.finish();
4962
println!("{}", msg);
5063
}
5164
Ok(())

0 commit comments

Comments
 (0)