Skip to content

Commit 5ad27fd

Browse files
committed
analysis-stats: add UsizeWithUnderscore for readability of large numbers
1 parent 756c424 commit 5ad27fd

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
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

@@ -156,6 +156,11 @@ impl flags::AnalysisStats {
156156
let item_tree_time = item_tree_sw.elapsed();
157157

158158
eprintln!("Source stats:");
159+
let dep_loc = UsizeWithUnderscore(dep_loc);
160+
let deps_item_trees = UsizeWithUnderscore(deps_item_trees);
161+
let workspace_loc = UsizeWithUnderscore(workspace_loc);
162+
let workspace_item_trees = UsizeWithUnderscore(workspace_item_trees);
163+
159164
eprintln!(" dependency lines of code: {dep_loc}, item trees: {deps_item_trees}");
160165
eprintln!(" workspace lines of code: {workspace_loc}, item trees: {workspace_item_trees}");
161166

@@ -1250,6 +1255,30 @@ fn percentage(n: u64, total: u64) -> u64 {
12501255
(n * 100).checked_div(total).unwrap_or(100)
12511256
}
12521257

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

0 commit comments

Comments
 (0)