cgag / loc

Count lines of code quickly.
MIT License
2.29k stars 126 forks source link

add format flag with json support #126

Open Southclaws opened 4 years ago

Southclaws commented 4 years ago

A small addition that adds a --format flag with two options: table which is the existing functionality and json which formats the output as a JSON object to facilitate automation and integration with other tools.

Here's a small demo of why I did this, running the tool on itself:

> cargo run -- --format=json
   Compiling loc v0.5.0 (/Users/barnabykeene/Desktop/loc)
    Finished dev [unoptimized + debuginfo] target(s) in 4.34s
     Running `target/debug/loc --format=json`
{"Bourne Shell":{"files":3,"count":{"code":59,"comment":7,"blank":18,"lines":84}},"Haskell":{"files":1,"count":{"code":2,"comment":8,"blank":4,"lines":14}},"Terraform":{"files":1,"count":{"code":65,"comment":11,"blank":13,"lines":89}},"Ada":{"files":2,"count":{"code":32,"comment":9,"blank":12,"lines":53}},"Solidity":{"files":1,"count":{"code":10,"comment":3,"blank":3,"lines":16}},"Lua":{"files":2,"count":{"code":169351,"comment":193544,"blank":24193,"lines":387088}},"Groovy":{"files":1,"count":{"code":6,"comment":10,"blank":1,"lines":17}},"OCaml":{"files":1,"count":{"code":3,"comment":6,"blank":4,"lines":13}},"PowerShell":{"files":2,"count":{"code":15,"comment":9,"blank":8,"lines":32}},"Ruby":{"files":1,"count":{"code":2,"comment":2,"blank":0,"lines":4}},"Handlebars":{"files":1,"count":{"code":2,"comment":2,"blank":0,"lines":4}},"Rust":{"files":4,"count":{"code":1260,"comment":60,"blank":134,"lines":1454}},"YAML":{"files":1,"count":{"code":57,"comment":19,"blank":13,"lines":89}},"Python":{"files":1,"count":{"code":2,"comment":2,"blank":2,"lines":6}},"Toml":{"files":1,"count":{"code":24,"comment":0,"blank":4,"lines":28}},"C":{"files":4,"count":{"code":470,"comment":155,"blank":75,"lines":700}},"Markdown":{"files":2,"count":{"code":280,"comment":0,"blank":42,"lines":322}},"Gherkin":{"files":1,"count":{"code":8,"comment":2,"blank":2,"lines":12}},"Zig":{"files":1,"count":{"code":5,"comment":2,"blank":2,"lines":9}},"Reason":{"files":1,"count":{"code":3,"comment":6,"blank":4,"lines":13}},"Nix":{"files":1,"count":{"code":3,"comment":3,"blank":2,"lines":8}}}

Pure JSON is ugly, I considered making it pretty but decided it would be pointless since this feature is intended for piping to other tools for automation etc.

Such as jq:

> cargo run -- --format=json | jq
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/loc --format=json`
{
  "Solidity": {
    "files": 1,
    "count": {
      "code": 10,
      "comment": 3,
      "blank": 3,
      "lines": 16
    }
  },
  "C": {
    "files": 4,
    "count": {
      "code": 470,
      "comment": 155,
      "blank": 75,
      "lines": 700
    }
  },
...

Also, if you're using NuShell then you can play with the data a bit more!

> cargo run -- --format=json | from-json --flatten | pivot | get Column1.count
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/loc --format=json`
━━━━┯━━━━━━━━┯━━━━━━━━━┯━━━━━━━┯━━━━━━━━
 #  │ code   │ comment │ blank │ lines 
────┼────────┼─────────┼───────┼────────
  0 │    280 │       0 │    42 │    322 
  1 │     57 │      19 │    13 │     89 
  2 │     10 │       3 │     3 │     16 
  3 │      8 │       2 │     2 │     12 
  4 │      5 │       2 │     2 │      9 
  5 │      2 │       2 │     0 │      4 
  6 │   1260 │      60 │   134 │   1454 
  7 │      3 │       6 │     4 │     13 
  8 │      6 │      10 │     1 │     17 
  9 │    470 │     155 │    75 │    700 
 10 │      2 │       8 │     4 │     14 
 11 │ 169351 │  193544 │ 24193 │ 387088 
 12 │     59 │       7 │    18 │     84 
 13 │     32 │       9 │    12 │     53 
 14 │      3 │       3 │     2 │      8 
 15 │      2 │       2 │     0 │      4 
 16 │      2 │       2 │     2 │      6 
 17 │     24 │       0 │     4 │     28 
 18 │     65 │      11 │    13 │     89 
 19 │     15 │       9 │     8 │     32 
 20 │      3 │       6 │     4 │     13 
━━━━┷━━━━━━━━┷━━━━━━━━━┷━━━━━━━┷━━━━━━━━