Skip to content

Commit 7dfff84

Browse files
committed
analysis-stats: add UsizeWithUnderscore for readability of large numbers
1 parent ea5bf3a commit 7dfff84

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

crates/rust-analyzer/src/cli/analysis_stats.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! errors.
33
44
use std::{
5-
env,
5+
env, fmt,
66
time::{SystemTime, UNIX_EPOCH},
77
};
88

@@ -152,6 +152,11 @@ impl flags::AnalysisStats {
152152
let item_tree_time = item_tree_sw.elapsed();
153153

154154
eprintln!("Source stats:");
155+
let dep_loc = UsizeWithUnderscore(dep_loc);
156+
let deps_item_trees = UsizeWithUnderscore(deps_item_trees);
157+
let workspace_loc = UsizeWithUnderscore(workspace_loc);
158+
let workspace_item_trees = UsizeWithUnderscore(workspace_item_trees);
159+
155160
eprintln!(" dependency lines of code: {dep_loc}, item trees: {deps_item_trees}");
156161
eprintln!(" workspace lines of code: {workspace_loc}, item trees: {workspace_item_trees}");
157162

@@ -1243,6 +1248,32 @@ fn percentage(n: u64, total: u64) -> u64 {
12431248
(n * 100).checked_div(total).unwrap_or(100)
12441249
}
12451250

1251+
struct UsizeWithUnderscore(usize);
1252+
1253+
impl fmt::Display for UsizeWithUnderscore {
1254+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1255+
let num_str = self.0.to_string();
1256+
1257+
if num_str.len() <= 3 {
1258+
return write!(f, "{}", num_str);
1259+
}
1260+
1261+
let mut result = String::new();
1262+
let mut count = 0;
1263+
1264+
for ch in num_str.chars().rev() {
1265+
if count > 0 && count % 3 == 0 {
1266+
result.push('_');
1267+
}
1268+
result.push(ch);
1269+
count += 1;
1270+
}
1271+
1272+
let result = result.chars().rev().collect::<String>();
1273+
write!(f, "{}", result)
1274+
}
1275+
}
1276+
12461277
// FIXME(salsa-transition): bring this back whenever we implement
12471278
// Salsa's memory usage tracking to work with tracked functions.
12481279
// fn syntax_len(node: SyntaxNode) -> usize {

0 commit comments

Comments
 (0)