Skip to content

armgit / Wake

Public Imported from GitHub
0

All your AI agent sessions in one place — browse, search, resume. Rust + GPUI.

Rust

refactor(cli): index progress formatting lives in cli.rs, final line only ends

Corey Chiud98555e47e7a
diff --git a/crates/wake-core/src/bin/wake_cli.rs b/crates/wake-core/src/bin/wake_cli.rs
index 0db5abb..89b6def 100644
--- a/crates/wake-core/src/bin/wake_cli.rs
+++ b/crates/wake-core/src/bin/wake_cli.rs
@@ -99,9 +99,8 @@ fn run(db: &Option<PathBuf>, tool: &str, args: &serde_json::Value) -> ExitCode {
fn index(path: &Path) -> Result<String, tools::ToolError> {
// 进度只给终端看:全量扫描要几秒到几十秒,没有反馈像卡死;管道与 agent
// 调用时 stderr 保持安静(它是诊断通道,tests/cli.rs 也这么断言)
- let tty = TtyProgress;
let events: &dyn ScanEvents = if io::stderr().is_terminal() {
- &tty
+ &TtyProgress
} else {
&NullEvents
};
@@ -127,19 +126,13 @@ fn index(path: &Path) -> Result<String, tools::ToolError> {
))
}
-/// `index` 在终端上的进度:同一行原地刷新 `Indexing done/total`,终态换行收尾,
-/// 结果本身仍由 stdout 给。写失败一律忽略——进度条不是结果
+/// `index` 在终端上的进度:格式在 `cli::progress_line`(有单测),这里只负责
+/// "写到 stderr"。写失败一律忽略——进度条不是结果;stderr 无缓冲,不需要 flush
struct TtyProgress;
impl ScanEvents for TtyProgress {
fn on_progress(&self, p: &ScanProgress) {
- let mut err = io::stderr().lock();
- let _ = match (p.scanning, p.total) {
- (true, 0) => write!(err, "\rIndexing…"),
- (true, total) => write!(err, "\rIndexing {}/{total}…", p.done),
- (false, _) => writeln!(err, "\rIndexed {}/{} files.", p.done, p.total),
- };
- let _ = err.flush();
+ let _ = cli::progress_line(p, &mut io::stderr().lock());
}
fn on_sessions_changed(&self) {}
}
diff --git a/crates/wake-core/src/cli.rs b/crates/wake-core/src/cli.rs
index acce2f0..0ce3d8a 100644
--- a/crates/wake-core/src/cli.rs
+++ b/crates/wake-core/src/cli.rs
@@ -16,6 +16,7 @@ use serde_json::{Map, Value};
use crate::mcp::tools;
use crate::models::AgentId;
+use crate::scanner::ScanProgress;
use crate::services::terminal::sh_quote;
// ---------------------------------------------------------------- 命令表
@@ -334,6 +335,20 @@ pub fn emit(text: &str, out: &mut impl Write) -> std::io::Result<()> {
out.flush()
}
+/// `wake-cli index` 在终端上的进度行:同一行 `\r` 原地刷新,终态**只补一个换行**。
+/// 不在这里宣布 "Indexed …"——那句由 bin 用库里查出来的会话数在 stdout 说,进度
+/// 只知道文件数(墓碑/解析失败的文件也计 done),两个数字并排只会互相打架;而且
+/// 扫描收尾(parent_links / backfill)出错时 done 已经等于 total,先打一行成功再
+/// 跟一行错误是撒谎。总数未知的首帧只写 "Indexing…"。写到哪条流、写不写由调用方
+/// 定(bin 只在 stderr 是 TTY 时调);行只会变长(总数定了、done 单调增),不必补空格
+pub fn progress_line(p: &ScanProgress, out: &mut impl Write) -> std::io::Result<()> {
+ match (p.scanning, p.total) {
+ (true, 0) => write!(out, "\rIndexing…"),
+ (true, total) => write!(out, "\rIndexing {}/{total}…", p.done),
+ (false, _) => writeln!(out),
+ }
+}
+
// ---------------------------------------------------------------- 解析
fn err(message: String, with_usage: bool) -> CliError {
@@ -1176,6 +1191,28 @@ mod tests {
/// 下游关掉管道要浮成 Err 让 bin 去分类,不能 panic。真管道不好测(fixture
/// 转录塞得进 64K 管道缓冲,子进程往往先写完),这里用假 writer 卡行为
+ #[test]
+ fn progress_line_updates_in_place_and_only_ends_the_line() {
+ let step = |scanning, done, total, error: Option<&str>| ScanProgress {
+ scanning,
+ done,
+ total,
+ error: error.map(str::to_string),
+ };
+ let mut out = Vec::new();
+ progress_line(&step(true, 0, 0, None), &mut out).unwrap();
+ progress_line(&step(true, 3, 10, None), &mut out).unwrap();
+ progress_line(&step(false, 10, 10, None), &mut out).unwrap();
+ assert_eq!(
+ String::from_utf8(out).unwrap(),
+ "\rIndexing…\rIndexing 3/10…\n"
+ );
+ // 出错收场同样只是换行:错误由 cli::report 打,这里不抢着宣布任何结果
+ let mut out = Vec::new();
+ progress_line(&step(false, 4, 10, Some("boom")), &mut out).unwrap();
+ assert_eq!(out, b"\n");
+ }
+
#[test]
fn a_closed_pipe_surfaces_as_an_error_not_a_panic() {
struct FailWrite;