From 2640c18a20b28cc81b5f6d07e31b055af296c014 Mon Sep 17 00:00:00 2001 From: AlDanial Date: Fri, 2 Feb 2024 11:47:31 -0800 Subject: [PATCH] negative --fmt values retain the JSON file, #794 --- Unix/cloc | 16 +++++++++++----- cloc | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Unix/cloc b/Unix/cloc index 02710bd3..527bb006 100755 --- a/Unix/cloc +++ b/Unix/cloc @@ -616,8 +616,12 @@ Usage: $script [options] | | encodings(\":all\")), \"\\n\"' --fmt= Alternate text output format where is a number - from 1 to 5. 'total lines' means the sum of code, - comment, and blank lines. + from 1 to 5, or -1 to -5. 'total lines' means the + sum of code, comment, and blank lines. Negative + values are the same as the positive values but retain, + instead of deleting, the intermediate JSON file that + is written. The JSON file name is randomly generated + unless --out/--report-file is given. The formats are: 1: by language (same as cloc default output) 2: by language with an extra column for total lines 3: by file with language @@ -1001,9 +1005,11 @@ $opt_by_file = 1 if defined $opt_by_file_by_lang; if ($opt_fmt) { $opt_by_file = 1; $opt_json = 1; + if (!defined $opt_report_file) { my $fh; ($fh, $opt_report_file) = tempfile(UNLINK => 0, DIR => ".", SUFFIX => ".json" ); $fh->close; # will be opened later by the JSON writer + } } my $CLOC_XSL = "cloc.xsl"; # created with --xsl $CLOC_XSL = "cloc-diff.xsl" if $opt_diff; @@ -2193,8 +2199,8 @@ if ($opt_report_file) { write_file($opt_report_file, {}, @Lines_Out); if ($opt_fmt) { my ($file_len, $lang_len, $header, %contents) = load_json($opt_report_file); - print_format_n($opt_fmt, $file_len, $lang_len, $header, %contents); - unlink $opt_report_file; + unlink $opt_report_file unless $opt_fmt < 0; + print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents); } } else { print "\n" unless $opt_quiet; @@ -6797,7 +6803,7 @@ sub write_file { # {{{1 $OUT->close; if (can_read($file)) { - print "Wrote $file" unless $opt_quiet or $opt_fmt; + print "Wrote $file" unless $opt_quiet or $opt_fmt > 0; print ", $CLOC_XSL" if $opt_xsl and $opt_xsl eq $CLOC_XSL; print "\n" unless $opt_quiet; } diff --git a/cloc b/cloc index 40042cba..b3a89d95 100755 --- a/cloc +++ b/cloc @@ -641,8 +641,12 @@ Usage: $script [options] | | encodings(\":all\")), \"\\n\"' --fmt= Alternate text output format where is a number - from 1 to 5. 'total lines' means the sum of code, - comment, and blank lines. + from 1 to 5, or -1 to -5. 'total lines' means the + sum of code, comment, and blank lines. Negative + values are the same as the positive values but retain, + instead of deleting, the intermediate JSON file that + is written. The JSON file name is randomly generated + unless --out/--report-file is given. The formats are: 1: by language (same as cloc default output) 2: by language with an extra column for total lines 3: by file with language @@ -1016,9 +1020,11 @@ $opt_by_file = 1 if defined $opt_by_file_by_lang; if ($opt_fmt) { $opt_by_file = 1; $opt_json = 1; + if (!defined $opt_report_file) { my $fh; ($fh, $opt_report_file) = tempfile(UNLINK => 0, DIR => ".", SUFFIX => ".json" ); $fh->close; # will be opened later by the JSON writer + } } my $CLOC_XSL = "cloc.xsl"; # created with --xsl $CLOC_XSL = "cloc-diff.xsl" if $opt_diff; @@ -2208,8 +2214,8 @@ if ($opt_report_file) { write_file($opt_report_file, {}, @Lines_Out); if ($opt_fmt) { my ($file_len, $lang_len, $header, %contents) = load_json($opt_report_file); - print_format_n($opt_fmt, $file_len, $lang_len, $header, %contents); - unlink $opt_report_file; + unlink $opt_report_file unless $opt_fmt < 0; + print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents); } } else { print "\n" unless $opt_quiet; @@ -6812,7 +6818,7 @@ sub write_file { # {{{1 $OUT->close; if (can_read($file)) { - print "Wrote $file" unless $opt_quiet or $opt_fmt; + print "Wrote $file" unless $opt_quiet or $opt_fmt > 0; print ", $CLOC_XSL" if $opt_xsl and $opt_xsl eq $CLOC_XSL; print "\n" unless $opt_quiet; }