diff options
| author | 魏曹先生 <1992414357@qq.com> | 2025-12-09 12:30:42 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2025-12-09 12:30:42 +0800 |
| commit | c740e28bd851221f32dc3f48cd94ee78352bba93 (patch) | |
| tree | 2c8f8d068c39375e56d7621b47e115e1ee5096c4 | |
| parent | 1d1a1009ba795d70c0c06a2ffcc607c5704bf675 (diff) | |
Fix `jvv here` display for vault size calculation
Add timeout handling and proper file counting for virtual files Display
progress message when calculation takes too long Add error message for
size calculation failures
| -rw-r--r-- | locales/help_docs/en.yml | 11 | ||||
| -rw-r--r-- | locales/help_docs/zh-CN.yml | 5 | ||||
| -rw-r--r-- | src/bin/jvv.rs | 60 |
3 files changed, 64 insertions, 12 deletions
diff --git a/locales/help_docs/en.yml b/locales/help_docs/en.yml index 80facb0..780e0d4 100644 --- a/locales/help_docs/en.yml +++ b/locales/help_docs/en.yml @@ -79,6 +79,10 @@ jvv: You can use --port to set the listening port, use --no-log to disable log output + info: + here: + analyzing_size: Analyzing vault storage size... + fail: jvcs: "JustEnoughVCS Error: %{err}" no_vault_here: No vault found here @@ -99,6 +103,7 @@ jvv: If you wish to use this directory as a vault, please use jvv init --help to view related help vault_init_failed: Failed to initialize vault! + size_calc_error: Failed to calculate vault storage size! member_ids_failed: Failed to get member IDs! ref_sheet_not_found: Reference sheet `ref` not found, but it should exist! @@ -943,7 +948,7 @@ jv: jvii: hints: | [^S WRITE] [ESC EXIT] - + errors: no_file_path: "Error: No file path provided" editor_error: "Editor error: %{error}" @@ -955,12 +960,12 @@ jvii: alternate_screen_error: "Failed to enter alternate screen: %{error}" save_error: "Failed to save file: %{error}" render_error: "Render error: %{error}" - + messages: file_saved: "File saved successfully" unsaved_changes: "Unsaved changes! Press Ctrl+S to save or Esc again to exit" modified: " *" - + status: lines: " lines" diff --git a/locales/help_docs/zh-CN.yml b/locales/help_docs/zh-CN.yml index 3059283..2eb8bd4 100644 --- a/locales/help_docs/zh-CN.yml +++ b/locales/help_docs/zh-CN.yml @@ -76,6 +76,10 @@ jvv: jvv service listen - 在当前库中启动服务器,以接受客户端连接 您可以使用 --port 来设定监听的端口,使用 --no-log 禁用日志输出 + info: + here: + analyzing_size: 正在分析库存储大小... + fail: jvcs: JustEnoughVCS 错误:%{err} no_vault_here: 此处并没有找到库文件 @@ -96,6 +100,7 @@ jvv: 若您希望将该目录作为库,请使用 jvv init --help 查看相关帮助 vault_init_failed: 初始化库失败! + size_calc_error: 计算库存储大小失败! member_ids_failed: 获得成员 ID 失败! ref_sheet_not_found: 未找到参照表 `ref`,该参照表理应存在! diff --git a/src/bin/jvv.rs b/src/bin/jvv.rs index 3e32fa7..644a576 100644 --- a/src/bin/jvv.rs +++ b/src/bin/jvv.rs @@ -373,22 +373,64 @@ async fn jvv_here(_args: HereArgs) { }; // Get sheet count - let num_sheets = vault.sheets().await.iter().len(); + let num_sheets = vault.sheet_names().unwrap().iter().len(); - // Get virtual file count + // Get virtual file count and total size recursively let virtual_file_root = vault.virtual_file_storage_dir(); let mut num_vf = 0; let mut total_size = 0; - if let Ok(mut entries) = fs::read_dir(&virtual_file_root).await { - while let Ok(Some(entry)) = entries.next_entry().await { - if let Ok(metadata) = entry.metadata().await - && metadata.is_file() - { - num_vf += 1; - total_size += metadata.len(); + // Recursive function to calculate total size and count files with specific name + async fn calculate_total_size_and_vf_count( + path: std::path::PathBuf, + file_count: &mut u64, + total_size: &mut u64, + ) -> std::io::Result<()> { + let mut entries = fs::read_dir(&path).await?; + while let Some(entry) = entries.next_entry().await? { + let metadata = entry.metadata().await?; + if metadata.is_file() { + *total_size += metadata.len(); + // Check if file name matches SERVER_NAME_VF_META + if entry.file_name() == just_enough_vcs::vcs::constants::SERVER_NAME_VF_META { + *file_count += 1; + } + } else if metadata.is_dir() { + Box::pin(calculate_total_size_and_vf_count( + entry.path(), + file_count, + total_size, + )) + .await?; } } + Ok(()) + } + + // Calculate with timeout + let timeout_duration = std::time::Duration::from_millis(1200); + let size_result = tokio::time::timeout(timeout_duration, async { + calculate_total_size_and_vf_count(virtual_file_root.clone(), &mut num_vf, &mut total_size) + .await + }) + .await; + + match size_result { + Ok(Ok(_)) => { + // Calculation completed within timeout + } + Ok(Err(e)) => { + eprintln!( + "{}", + t!("jvv.fail.here.size_calc_error", error = e.to_string()).trim() + ); + return; + } + Err(_) => { + // Timeout occurred + println!("{}", t!("jvv.info.here.analyzing_size").trim()); + // Continue with partial calculation (num_vf and total_size as calculated so far) + } } // Get member count |
