howmany 3.0.0

A blazingly fast, intelligent code analysis tool with parallel processing, caching, and beautiful visualizations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::core::stats::aggregation::AggregatedStats;
use crate::core::stats::basic::ExtensionStats;
use crate::core::types::FileStats;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Filter options for CLI output
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FilterOptions {
    // Size filters
    pub min_lines: Option<usize>,
    pub max_lines: Option<usize>,
    pub min_size_bytes: Option<u64>,
    pub max_size_bytes: Option<u64>,

    // Complexity filters
    pub min_complexity: Option<f64>,
    pub max_complexity: Option<f64>,
    pub min_functions: Option<usize>,
    pub max_functions: Option<usize>,

    // Quality filters
    pub min_quality_score: Option<f64>,
    pub max_quality_score: Option<f64>,
    pub min_doc_ratio: Option<f64>,
    pub max_doc_ratio: Option<f64>,

    // Language/extension filters
    pub include_languages: Vec<String>,
    pub exclude_languages: Vec<String>,

    // Output customization
    pub show_complexity: bool,
    pub show_quality: bool,
    pub show_ratios: bool,
    pub show_size_info: bool,
    pub compact_output: bool,
}

/// One file's complexity, taken from the analysis the run already performed.
///
/// `--min-complexity` and `--min-functions` need this; nothing else does, so it
/// is derived from the function details rather than measured a second time.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct FileComplexity {
    pub function_count: usize,
    pub max_cyclomatic: f64,
    pub average_cyclomatic: f64,
}

impl FileComplexity {
    /// Group per-function details by the file they were found in.
    pub fn index(
        details: &[crate::core::stats::complexity::FunctionComplexityDetail],
    ) -> BTreeMap<String, FileComplexity> {
        let mut totals: BTreeMap<String, (usize, f64, f64)> = BTreeMap::new();
        for detail in details {
            let entry = totals
                .entry(detail.file_path.clone())
                .or_insert((0, 0.0, 0.0));
            let complexity = detail.cyclomatic_complexity as f64;
            entry.0 += 1;
            entry.1 += complexity;
            entry.2 = entry.2.max(complexity);
        }

        totals
            .into_iter()
            .map(|(path, (count, sum, max))| {
                (
                    path,
                    FileComplexity {
                        function_count: count,
                        max_cyclomatic: max,
                        average_cyclomatic: if count == 0 { 0.0 } else { sum / count as f64 },
                    },
                )
            })
            .collect()
    }
}

/// Filter for individual files
pub struct FileFilter {
    options: FilterOptions,
}

impl FileFilter {
    pub fn new(options: FilterOptions) -> Self {
        Self { options }
    }

    /// True when some option can only be decided per file.
    ///
    /// When nothing here is set, every file passes, and the aggregate totals the
    /// engine already computed are the answer -- retaining and re-summing one
    /// record per file would be pure overhead. On a 60k-file tree that is the
    /// difference between allocating 60k records and allocating none.
    pub fn needs_per_file_stats(options: &FilterOptions) -> bool {
        options.min_lines.is_some()
            || options.max_lines.is_some()
            || options.min_size_bytes.is_some()
            || options.max_size_bytes.is_some()
            || options.min_doc_ratio.is_some()
            || options.max_doc_ratio.is_some()
            || options.min_quality_score.is_some()
            || options.max_quality_score.is_some()
            || !options.include_languages.is_empty()
            || !options.exclude_languages.is_empty()
            || Self::needs_complexity(options)
    }

    /// True when a filter can only be decided from complexity analysis.
    ///
    /// Complexity costs a parse per file, so it is computed only when one of
    /// these options is actually set.
    pub fn needs_complexity(options: &FilterOptions) -> bool {
        options.min_complexity.is_some()
            || options.max_complexity.is_some()
            || options.min_functions.is_some()
            || options.max_functions.is_some()
    }

    /// True when `complexity` satisfies the complexity and function filters.
    ///
    /// A file with no analysable functions has no complexity to compare, so it
    /// fails any lower bound and passes any upper one.
    pub fn passes_complexity_filter(&self, complexity: Option<&FileComplexity>) -> bool {
        let measured = complexity.copied().unwrap_or_default();

        let within = |value: f64, min: Option<f64>, max: Option<f64>| {
            min.is_none_or(|m| value >= m) && max.is_none_or(|m| value <= m)
        };

        within(
            measured.max_cyclomatic,
            self.options.min_complexity,
            self.options.max_complexity,
        ) && self
            .options
            .min_functions
            .is_none_or(|m| measured.function_count >= m)
            && self
                .options
                .max_functions
                .is_none_or(|m| measured.function_count <= m)
    }

    pub fn passes_filter(&self, file_path: &str, file_stats: &FileStats) -> bool {
        // Size filters
        if let Some(min_lines) = self.options.min_lines {
            if file_stats.total_lines < min_lines {
                return false;
            }
        }

        if let Some(max_lines) = self.options.max_lines {
            if file_stats.total_lines > max_lines {
                return false;
            }
        }

        if let Some(min_size) = self.options.min_size_bytes {
            if file_stats.file_size < min_size {
                return false;
            }
        }

        if let Some(max_size) = self.options.max_size_bytes {
            if file_stats.file_size > max_size {
                return false;
            }
        }

        // Language/extension filters
        let extension = std::path::Path::new(file_path)
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("no_ext")
            .to_lowercase();

        if !self.options.include_languages.is_empty()
            && !self
                .options
                .include_languages
                .iter()
                .any(|lang| lang.to_lowercase() == extension)
        {
            return false;
        }

        if !self.options.exclude_languages.is_empty()
            && self
                .options
                .exclude_languages
                .iter()
                .any(|lang| lang.to_lowercase() == extension)
        {
            return false;
        }

        // Documentation ratio filter
        if let Some(min_doc_ratio) = self.options.min_doc_ratio {
            let doc_ratio = if file_stats.total_lines > 0 {
                file_stats.doc_lines as f64 / file_stats.total_lines as f64
            } else {
                0.0
            };
            if doc_ratio < min_doc_ratio {
                return false;
            }
        }

        if let Some(max_doc_ratio) = self.options.max_doc_ratio {
            let doc_ratio = if file_stats.total_lines > 0 {
                file_stats.doc_lines as f64 / file_stats.total_lines as f64
            } else {
                0.0
            };
            if doc_ratio > max_doc_ratio {
                return false;
            }
        }

        // `--min-quality` / `--max-quality` used to be accepted and then
        // ignored. The score is the same one the project report shows, computed
        // for this file alone: pure arithmetic over line counts, no extra IO.
        if self.options.min_quality_score.is_some() || self.options.max_quality_score.is_some() {
            let score = Self::quality_score(file_stats);
            if self
                .options
                .min_quality_score
                .is_some_and(|min| score < min)
                || self
                    .options
                    .max_quality_score
                    .is_some_and(|max| score > max)
            {
                return false;
            }
        }

        true
    }

    /// The overall quality score for a single file, on the report's 0-100 scale.
    fn quality_score(file_stats: &FileStats) -> f64 {
        crate::core::stats::ratios::RatioStatsCalculator::new()
            .calculate_ratio_stats(file_stats)
            .map(|ratios| ratios.quality_metrics.overall_quality_score)
            .unwrap_or(0.0)
    }
}

/// Project-level filter for aggregated stats
pub struct ProjectFilter {
    options: FilterOptions,
}

impl ProjectFilter {
    pub fn new(options: FilterOptions) -> Self {
        Self { options }
    }

    /// Filter extensions based on criteria
    pub fn filter_extensions(
        &self,
        stats_by_extension: &BTreeMap<String, ExtensionStats>,
    ) -> BTreeMap<String, ExtensionStats> {
        let mut filtered = BTreeMap::new();

        for (ext, stats) in stats_by_extension {
            // Language filters
            if !self.options.include_languages.is_empty()
                && !self
                    .options
                    .include_languages
                    .iter()
                    .any(|lang| lang.to_lowercase() == ext.to_lowercase())
            {
                continue;
            }

            if !self.options.exclude_languages.is_empty()
                && self
                    .options
                    .exclude_languages
                    .iter()
                    .any(|lang| lang.to_lowercase() == ext.to_lowercase())
            {
                continue;
            }

            // Size filters
            if let Some(min_lines) = self.options.min_lines {
                if stats.total_lines < min_lines {
                    continue;
                }
            }

            if let Some(max_lines) = self.options.max_lines {
                if stats.total_lines > max_lines {
                    continue;
                }
            }

            if let Some(min_size) = self.options.min_size_bytes {
                if stats.total_size < min_size {
                    continue;
                }
            }

            if let Some(max_size) = self.options.max_size_bytes {
                if stats.total_size > max_size {
                    continue;
                }
            }

            filtered.insert(ext.clone(), stats.clone());
        }

        filtered
    }
}

/// Utility functions for filter parsing
pub struct FilterParser;

impl FilterParser {
    /// Parse a size such as `1024`, `1KB`, `500mb` or `2GB` into bytes.
    ///
    /// A value that is not a size returns `None` so the caller can report it.
    /// Negative and non-finite inputs are rejected rather than saturating to
    /// zero, which silently turned `--min-size -5` into "no minimum".
    pub fn parse_size(size_str: &str) -> Option<u64> {
        const UNITS: [(&str, u64); 4] = [
            ("KB", 1024),
            ("MB", 1024 * 1024),
            ("GB", 1024 * 1024 * 1024),
            ("B", 1),
        ];

        let upper = size_str.trim().to_uppercase();
        let (number, multiplier) = UNITS
            .iter()
            .find_map(|(suffix, factor)| Some((upper.strip_suffix(suffix)?, *factor)))
            .unwrap_or((upper.as_str(), 1));

        let value: f64 = number.trim().parse().ok()?;
        (value.is_finite() && value >= 0.0).then_some((value * multiplier as f64) as u64)
    }

    /// Parse a comma-separated list of languages into lowercase keys.
    ///
    /// Filtering compares against lowercase file extensions, so `--languages
    /// Rust,Python` has to arrive here in the same case or it matches nothing.
    pub fn parse_languages(lang_str: &str) -> Vec<String> {
        lang_str
            .split(',')
            .map(|s| s.trim().to_lowercase())
            .filter(|s| !s.is_empty())
            .collect()
    }
}

/// Format filtered output with additional information
pub struct FilteredOutputFormatter;

impl FilteredOutputFormatter {
    pub fn format_enhanced_cli_output(
        aggregated_stats: &AggregatedStats,
        options: &FilterOptions,
    ) -> String {
        let mut output = String::new();

        // Basic counts
        output.push_str(&format!(
            "{} files, {} lines",
            aggregated_stats.basic.total_files, aggregated_stats.basic.total_lines
        ));

        if options.show_size_info {
            let size_mb = aggregated_stats.basic.total_size as f64 / (1024.0 * 1024.0);
            output.push_str(&format!(", {:.1} MB", size_mb));
        }

        if options.show_complexity && aggregated_stats.complexity.function_count > 0 {
            output.push_str(&format!(
                ", {:.1} avg complexity",
                aggregated_stats.complexity.cyclomatic_complexity
            ));
        }

        if options.show_quality {
            output.push_str(&format!(
                ", {:.1}/100 quality",
                aggregated_stats
                    .ratios
                    .quality_metrics
                    .overall_quality_score
            ));
        }

        if options.show_ratios {
            output.push_str(&format!(
                ", {:.1}% code",
                aggregated_stats.ratios.code_ratio * 100.0
            ));
            if aggregated_stats.ratios.comment_ratio > 0.0 {
                output.push_str(&format!(
                    ", {:.1}% comments",
                    aggregated_stats.ratios.comment_ratio * 100.0
                ));
            }
        }

        output
    }
}