rloc 0.1.0

A fast, modern Rust implementation of cloc (Count Lines of Code)
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
use clap::Parser;
use dashmap::DashSet;
use indicatif::{ParallelProgressIterator, ProgressBar, ProgressStyle};
use rayon::prelude::*;
use rloc::cli::Cli;
use rloc::diff;
use rloc::output::{self, render, OutputFormat};
use rloc::strip::{self, StripMode};
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::process::ExitCode;
use std::time::Instant;

fn main() -> ExitCode {
    match run() {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {}", e);
            ExitCode::FAILURE
        }
    }
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();

    if cli.show_lang {
        rloc::cli::show_languages();
        return Ok(());
    }

    if cli.show_ext {
        rloc::cli::show_extensions();
        return Ok(());
    }

    if let Some(ref path) = cli.read_lang_def {
        rloc::custom_langs::CustomLanguages::load(path)?;
    }

    if !cli.sum_reports.is_empty() {
        return sum_reports(&cli);
    }

    if cli.strip_comments.is_some() || cli.strip_code.is_some() {
        return run_strip(&cli);
    }

    if let Some(ref diff_path) = cli.diff {
        return run_diff(&cli, diff_path);
    }

    if cli.threads > 0 {
        rayon::ThreadPoolBuilder::new()
            .num_threads(cli.threads)
            .build_global()
            .ok();
    }

    let mut walker_config = cli.to_walker_config()?;
    let output_config = cli.to_output_config();

    let start = Instant::now();

    let temp_dir = if cli.extract_archives {
        let temp = std::env::temp_dir().join(format!("rloc-{}", std::process::id()));
        std::fs::create_dir_all(&temp)?;

        let mut extra_paths = Vec::new();
        for path in &walker_config.paths {
            if path.is_file() && rloc::archive::is_archive(path) {
                let archive_dest = temp.join(path.file_stem().unwrap_or_default());
                std::fs::create_dir_all(&archive_dest)?;
                if rloc::archive::extract_archive(path, &archive_dest).is_ok() {
                    extra_paths.push(archive_dest);
                }
            }
        }

        walker_config.paths.extend(extra_paths);
        Some(temp)
    } else {
        None
    };

    let files = rloc::walker::walk_files(&walker_config);

    if files.is_empty() {
        if !cli.quiet {
            eprintln!("No source files found.");
        }
        return Ok(());
    }

    let file_count = files.len();
    let skip_uniqueness = walker_config.skip_uniqueness;
    let seen_hashes: DashSet<u64> = DashSet::new();

    let progress = if cli.quiet || output_config.format != OutputFormat::Table {
        ProgressBar::hidden()
    } else {
        let pb = ProgressBar::new(file_count as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({per_sec})")
                .unwrap()
                .progress_chars("=>-"),
        );
        pb
    };

    let file_stats: Vec<_> = files
        .into_par_iter()
        .progress_with(progress.clone())
        .filter_map(|entry| {
            if !skip_uniqueness {
                if let Ok(hash) = rloc::counter::compute_file_hash(&entry.path) {
                    if !seen_hashes.insert(hash) {
                        return None;
                    }
                }
            }

            match rloc::counter::count_lines(&entry.path, entry.language) {
                Ok(stats) if stats.total() > 0 => Some(stats),
                Ok(_) => None,
                Err(e) => {
                    if cli.verbose > 0 {
                        eprintln!("warning: {}: {}", entry.path.display(), e);
                    }
                    None
                }
            }
        })
        .collect();

    progress.finish_and_clear();

    let elapsed = start.elapsed();
    let summary = rloc::stats::Summary::from_file_stats(file_stats).with_elapsed(elapsed);

    if let Some(output_path) = cli.output_path() {
        let file = File::create(output_path)?;
        let mut writer = BufWriter::new(file);
        render_to_writer(&summary, &output_config, &mut writer)?;
        writer.flush()?;
    } else {
        render(&summary, &output_config)?;
    }

    if let Some(temp) = temp_dir {
        let _ = std::fs::remove_dir_all(temp);
    }

    Ok(())
}

fn render_to_writer(
    summary: &rloc::stats::Summary,
    config: &output::OutputConfig,
    out: &mut impl Write,
) -> io::Result<()> {
    match config.format {
        OutputFormat::Table => {
            if !config.hide_rate {
                if let Some(elapsed) = summary.elapsed {
                    writeln!(out)?;
                    write!(
                        out,
                        "{} files processed in {:.3}s",
                        summary.total_files,
                        elapsed.as_secs_f64()
                    )?;
                    if let (Some(fps), Some(lps)) =
                        (summary.files_per_second(), summary.lines_per_second())
                    {
                        write!(out, " ({:.0} files/s, {:.0} lines/s)", fps, lps)?;
                    }
                    writeln!(out)?;
                }
            }

            writeln!(out)?;
            writeln!(out, "Language       Files    Blank  Comment     Code")?;
            writeln!(out, "─────────────────────────────────────────────────")?;

            for lang in &summary.languages {
                writeln!(
                    out,
                    "{:<14} {:>5} {:>8} {:>8} {:>8}",
                    lang.name, lang.files, lang.blanks, lang.comments, lang.code
                )?;
            }

            writeln!(out, "─────────────────────────────────────────────────")?;
            writeln!(
                out,
                "{:<14} {:>5} {:>8} {:>8} {:>8}",
                "SUM",
                summary.total_files,
                summary.total_blanks,
                summary.total_comments,
                summary.total_code
            )?;
            Ok(())
        }
        OutputFormat::Json => {
            let output = rloc::stats::JsonOutput::from(summary);
            let json = serde_json::to_string_pretty(&output).map_err(io::Error::other)?;
            writeln!(out, "{}", json)
        }
        OutputFormat::Csv => {
            let mut writer = csv::Writer::from_writer(out);
            writer.write_record(["Language", "Files", "Blank", "Comment", "Code"])?;
            for lang in &summary.languages {
                writer.write_record([
                    &lang.name,
                    &lang.files.to_string(),
                    &lang.blanks.to_string(),
                    &lang.comments.to_string(),
                    &lang.code.to_string(),
                ])?;
            }
            writer.write_record([
                "SUM",
                &summary.total_files.to_string(),
                &summary.total_blanks.to_string(),
                &summary.total_comments.to_string(),
                &summary.total_code.to_string(),
            ])?;
            writer.flush()?;
            Ok(())
        }
        OutputFormat::Yaml => {
            let output = rloc::stats::JsonOutput::from(summary);
            let yaml = serde_yaml::to_string(&output).map_err(io::Error::other)?;
            write!(out, "{}", yaml)
        }
        OutputFormat::Markdown => {
            writeln!(out, "| Language | Files | Blank | Comment | Code |")?;
            writeln!(out, "|----------|------:|------:|--------:|-----:|")?;
            for lang in &summary.languages {
                writeln!(
                    out,
                    "| {} | {} | {} | {} | {} |",
                    lang.name, lang.files, lang.blanks, lang.comments, lang.code
                )?;
            }
            writeln!(
                out,
                "| **SUM** | **{}** | **{}** | **{}** | **{}** |",
                summary.total_files,
                summary.total_blanks,
                summary.total_comments,
                summary.total_code
            )
        }
        OutputFormat::Sql => {
            writeln!(out, "CREATE TABLE t (")?;
            writeln!(out, "    Language TEXT,")?;
            writeln!(out, "    nFiles INTEGER,")?;
            writeln!(out, "    nBlank INTEGER,")?;
            writeln!(out, "    nComment INTEGER,")?;
            writeln!(out, "    nCode INTEGER")?;
            writeln!(out, ");")?;
            writeln!(out)?;

            for lang in &summary.languages {
                writeln!(
                    out,
                    "INSERT INTO t VALUES ('{}', {}, {}, {}, {});",
                    lang.name.replace('\'', "''"),
                    lang.files,
                    lang.blanks,
                    lang.comments,
                    lang.code
                )?;
            }

            writeln!(
                out,
                "INSERT INTO t VALUES ('SUM', {}, {}, {}, {});",
                summary.total_files,
                summary.total_blanks,
                summary.total_comments,
                summary.total_code
            )
        }
        OutputFormat::Xml => {
            writeln!(out, r#"<?xml version="1.0" encoding="UTF-8"?>"#)?;
            writeln!(out, "<results>")?;

            if let Some(elapsed) = summary.elapsed {
                writeln!(out, "  <header>")?;
                writeln!(out, "    <n_files>{}</n_files>", summary.total_files)?;
                writeln!(out, "    <n_lines>{}</n_lines>", summary.total_lines())?;
                writeln!(
                    out,
                    "    <elapsed_seconds>{:.3}</elapsed_seconds>",
                    elapsed.as_secs_f64()
                )?;
                writeln!(out, "  </header>")?;
            }

            writeln!(out, "  <languages>")?;
            for lang in &summary.languages {
                let escaped_name = lang
                    .name
                    .replace('&', "&amp;")
                    .replace('<', "&lt;")
                    .replace('>', "&gt;")
                    .replace('"', "&quot;");
                writeln!(out, "    <language name=\"{}\">", escaped_name)?;
                writeln!(out, "      <files>{}</files>", lang.files)?;
                writeln!(out, "      <blank>{}</blank>", lang.blanks)?;
                writeln!(out, "      <comment>{}</comment>", lang.comments)?;
                writeln!(out, "      <code>{}</code>", lang.code)?;
                writeln!(out, "    </language>")?;
            }
            writeln!(out, "  </languages>")?;

            writeln!(out, "  <total>")?;
            writeln!(out, "    <files>{}</files>", summary.total_files)?;
            writeln!(out, "    <blank>{}</blank>", summary.total_blanks)?;
            writeln!(out, "    <comment>{}</comment>", summary.total_comments)?;
            writeln!(out, "    <code>{}</code>", summary.total_code)?;
            writeln!(out, "  </total>")?;

            writeln!(out, "</results>")
        }
    }
}

fn sum_reports(cli: &Cli) -> Result<(), Box<dyn std::error::Error>> {
    use rloc::stats::JsonOutput;

    let mut reports = Vec::new();

    for path in &cli.sum_reports {
        let content = std::fs::read_to_string(path)
            .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
        let report: JsonOutput = serde_json::from_str(&content)
            .map_err(|e| format!("Failed to parse {}: {}", path.display(), e))?;
        reports.push(report);
    }

    let combined = JsonOutput::sum_reports(reports);
    let json = serde_json::to_string_pretty(&combined)?;
    println!("{}", json);

    Ok(())
}

fn run_strip(cli: &Cli) -> Result<(), Box<dyn std::error::Error>> {
    let walker_config = cli.to_walker_config()?;
    let files = rloc::walker::walk_files(&walker_config);

    let (mode, ext) = if let Some(ref ext) = cli.strip_comments {
        (StripMode::Comments, ext.as_str())
    } else if let Some(ref ext) = cli.strip_code {
        (StripMode::Code, ext.as_str())
    } else {
        return Err("No strip mode specified".into());
    };

    let mut processed = 0;
    let mut errors = 0;

    for entry in files {
        match strip::strip_file(
            &entry.path,
            entry.language,
            match mode {
                StripMode::Comments => StripMode::Comments,
                StripMode::Code => StripMode::Code,
            },
            ext,
        ) {
            Ok(()) => {
                processed += 1;
                if cli.verbose > 0 {
                    eprintln!("Stripped: {}", entry.path.display());
                }
            }
            Err(e) => {
                errors += 1;
                if cli.verbose > 0 {
                    eprintln!("Error stripping {}: {}", entry.path.display(), e);
                }
            }
        }
    }

    if !cli.quiet {
        eprintln!("Processed {} files ({} errors)", processed, errors);
    }

    Ok(())
}

fn run_diff(cli: &Cli, diff_path: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> {
    let config1 = cli.to_walker_config()?;
    let mut config2 = config1.clone();
    config2.paths = vec![diff_path.to_path_buf()];

    let result = diff::compute_diff(&config1, &config2, cli.verbose > 0);
    diff::render_diff(&result);

    Ok(())
}