AlDanial / cloc

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.
GNU General Public License v2.0
19.75k stars 1.02k forks source link

corrupt output when piped to less, more, etc. #725

Closed mcandre closed 1 year ago

mcandre commented 1 year ago

Describe the bug

The output mangles when cloc is piped to other text processing commands, such as the less and more pagers.

cloc should disable certain outputs when the current shell context is not a tty.

cloc; OS; OS version

To Reproduce

cloc . | less

Expected result

The output should look nearly the same in the pager as it does without a pager.

Observed

$ cloc . | less
     100 files^M     200 files^M     300 files^M     400 files^M     500 files^M     600 files^M     700 files^M     800 files^M     900 files^M    1000 files^M    1100 files^M    1200 files^M    1300 files^M    1400 files^M    1500 files^M    1600 files^M    1700 files^M    1800 files^M    1900 files^M    2000 files^M    2100 files^M    2200 files^M    2300 files^M    2400 files^M    2500 files^M    2600 files^M    2700 files^M    2800 files^M    2900 files^M    3000 files^M    3100 files^M    3200 files^M    3300 files^M    3400 files^M    3500 files^M    3600 files^M    3700 files^M^M    3800 files^M    3900 files^M    4000 files^M    4100 files^M    4200 files^M    4300 files^M    4400 files^M    4500 files^M    4600 files^M    4700 files^M    4800 files^M    4900 files^M    5000 files^M    5100 files^M    5200 files^M    5300 files^M    5400 files^M    5500 files^M    5600 files^M    5700 files^M    5800 files^M    5826 text files.
classified 4393 files^MDuplicate file check 4393 files (2228 known unique)^MUnique:      100 files                                          ^MUnique:      200 files                                          ^MUnique:      300 files                                          ^MUnique:      400 files                                          ^MUnique:      500 files                                          ^MUnique:      600 files                                          ^MUnique:      700 files                                          ^MUnique:      800 files                                          ^MUnique:      900 files                                          ^MUnique:     1000 files                                          ^MUnique:     1100 files                                          ^MUnique:     1200 files                                          ^MUnique:     1300 files                                          ^MUnique:     1400 files                                          ^MUnique:     1500 files                                          ^MUnique:     1600 files                                          ^MUnique:     1700 files                                          ^MUnique:     1800 files                                          ^MUnique:     1900 files                                          ^MUnique:     2000 files                                          ^MUnique:     2100 files                                          ^MUnique:     2200 files                                          ^M    3288 unique files.                              
Counting:  100^MCounting:  200^MCounting:  300^MCounting:  400^MCounting:  500^MCounting:  600^MCounting:  700^MCounting:  800^MCounting:  900^MCounting:  1000^MCounting:  1100^MCounting:  1200^MCounting:  1300^MCounting:  1400^MCounting:  1500^MCounting:  1600^MCounting:  1700^MCounting:  1800^^MCounting:  1900^MCounting:  2000^MCounting:  2100^MCounting:  2200^MCoun:

Additional context

The option --progress-rate 0 cleans up most of the mess. A !isatty() check would do well to automatically set progress rate to zero.

$ cloc --progress-rate 0 . | less
    5826 text files.
classified 4393 files^M    3288 unique files.                              
    7896 files ignored.

github.com/AlDanial/cloc v 1.96  T=2.19 s (1501.2 files/s, 299114.5 lines/s)
-----------------------------------------------------------------------------------
Language                         files          blank        comment           code
-----------------------------------------------------------------------------------
Rust                              1233          27561          74088         439009
Go                                 237           5418           7690          31814
Markdown                           234           5676              1          14635
XML                                 31            219             23           7241
Text                                40            135              0           5873
HTML                                69            240              0           5173
C++                                 24           1078            558           4708
TOML                                55            410            455           2673
Haskell                             93            736            119           2254
D                                  241            682             77           2164
Bourne Shell                       209            356            313           1598
make                               142            300             17           1140
JSON                               363              9              0            916
Lisp                                23            161            243            882
Python                              15            160            301            528
YAML                                22             38             95            498
CMake                                6             88              5            490
zsh                                 14            184           1029            468
C/C++ Header                         8            216            668            391
C                                    5             72              1            374
Bourne Again Shell                  32             80             43            336
JavaScript                          35             29              2            316
Dockerfile                          11              0              0            296
Scheme                              10             47             17            175
Arduino Sketch                       1             10              7            161
MATLAB                              10             40             20            110
Coq                                  5             42             29            107
SVG                                  7              0              0             90
Erlang                               6             17             39             68
Verilog-SystemVerilog                6              8              0             60
TNSDL                                2              7              0             56
Assembly                             5             22             14             54
:

That leaves classified 4393 files^M as the last bit of corruption.

Tried --hide-rate, with no effect.

Update

As a workaround, I am using the --quiet flag. This works well, and I would like to see it become the default behavior whenever the shell is not a tty.

AlDanial commented 1 year ago

setting --quiet when STDOUT isn't a terminal is a good idea