Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add format flag with json support #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add format flag with json support #126

wants to merge 1 commit into from

Conversation

Southclaws
Copy link

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 
━━━━┷━━━━━━━━┷━━━━━━━━━┷━━━━━━━┷━━━━━━━━

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant