vamchale/polyglot

bug fixes + polish

By Vanessa McHale <vanessa.mchale@reconfigure.io> on December 2, 2017
This patch is not signed. Learn here why and how to sign your patches.
6auTyMjS2JoYwbuFN1YrkNundqzGY8u1TPG9XNzKKpjHbk3V53vE51N5X4gnZqwfCEXYMATJRrUvaBfjxeiRjKVy

This patch is in the following branches:

master
In file README.md
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
# polyglot

[![Build Status](https://travis-ci.org/vmchale/polyglot.svg?branch=master)](https://travis-ci.org/vmchale/polyglot)

`poly` is a command-line tool that determines project contents.

<img alt="Screenshot of sample output" src=https://github.com/vmchale/polyglot/raw/master/screenshot.png>

## Features

Reasons to use polyglot:

  * Fast: 217 ms to run on the entire GHC codebase
  * Accurate: won't confuse Coq and Verilog
  * Pretty: magenta output

Reasons not to use polyglot:

  * It's written in ATS
  * No regex-based exclusions
  * Command-line parser is finicky

### Benchmarks

On the Rust repo:

<table>
  <tr>
    <th>Tool</th>
    <th>Language</th>
    <th>Time</th>
  </tr>
  <tr>
    <td><code>polyglot</code></td>
    <td>ATS</td>
    <td>218.3 ms</td>
  </tr>
  <tr>
    <td><code>loc</code></td>
    <td>Rust</td>
    <td>139.5 ms</td>
  </tr>
  <tr>
    <td><code>tokei</code></td>
    <td>Rust</td>
    <td>333.3 ms</td>
  </tr>
  <tr>
    <td><code>cloc</code></td>
    <td>Perl</td>
    <td>15.51 s</td>
  </tr>
  <tr>
    <td><code>linguist</code></td>
    <td>Ruby</td>
    <td>16.21 s</td>
  </tr>
</table>

### Heuristics

Polyglot distinguishes itself from `tokei` and `loc` by being able to disambiguate file names.
Thus, `poly` will not confuse Happy for Yacc (for instance).

## Installation

The easiest way to install is to use the installation script, like so:

```bash
curl -sSl https://raw.githubusercontent.com/vmchale/polyglot/master/bash/install.sh | bash -s
```

You may need to add `$HOME/.local/bin` to your `PATH` and
`$HOME/.local/share/man/man1` to your `MANPATH`.

You can optionally install [compleat](https://github.com/mbrubeck/compleat) for
shell completions as well.

## Building

If you install [stack](http://haskellstack.org/), [pandoc](http://pandoc.org/) and
[patscc](http://www.ats-lang.org/Downloads.html), you can install `poly` with

```bash
 $ ./shake.hs install
```


You may have to add `$HOME/.local/bin` to your `PATH` and
`$HOME/.local/share/man/man1` to your `MANPATH`.

You can optionally install [compleat](https://github.com/mbrubeck/compleat) for
shell completions as well.

### Documentation

You can view manpages for `poly` with

```
 $ man poly
```
In file shake.hs
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

#!/usr/bin/env stack
-- stack runghc --resolver nightly-2017-11-16 --package shake --install-ghc
-- stack runghc --resolver nightly-2017-12-01 --package shake --install-ghc

{-# LANGUAGE ScopedTypeVariables #-}

import           Data.Maybe        (fromMaybe)
import           Data.Monoid
import           Development.Shake
import           System.Exit       (ExitCode (..))

{-# ANN module "HLint: ignore Reduce duplication" #-}

main :: IO ()
main = shakeArgs shakeOptions { shakeFiles=".shake" } $ do
    want [ "target/poly"
         , "man/poly.1"
         -- , "target/poly-s390x-linux-gnu"
         -- , "target/poly-aarch64-linux-gnu"
         -- , "target/poly-sparc64-linux-gnu"
         -- , "target/poly-arm-linux-gnueabihf"
         -- , "target/poly-arm-linux-gnueabi"
         ]

    "lint" ~> do
        cmd_ "shellcheck --exclude SC2155 bash/install.sh"
        cmd "hlint shake.hs"

    "man/poly.1" %> \_ -> do
        need ["man/MANPAGE.md"]
        cmd ["pandoc", "man/MANPAGE.md", "-s", "-t", "man", "-o", "man/poly.1"]

    "build" %> \_ -> do
        need ["shake.hs"]
        cmd_ ["mkdir", "-p", ".shake"]
        command_ [Cwd ".shake"] "cp" ["../shake.hs", "."]
        command [Cwd ".shake"] "ghc" ["-O", "shake.hs", "-o", "../build", "-Wall", "-Werror", "-Wincomplete-uni-patterns", "-Wincomplete-record-updates"]
948

949
950
951
952

953
954
955
956
957
958
959
960
961
962
963
964


965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153








    val acc_r = ref<command_line>(acc)
    val () = acc_r->excludes := list_cons(s, acc.excludes)
    val () = if is_flag(s)
      then (println!("Error: flag " + s + " found where a directory name was expected") ; exit(0) ; ())
      else acc_r->excludes := list_cons(s, acc.excludes)
  in
    !acc_r // TODO don't allow any bad input through.
    !acc_r
  end

fun process(s: string, acc: command_line, is_first: bool) : command_line =
  let
    val acc_r = ref<command_line>(acc)
    val () = 
      if is_flag(s)
        then
          case+ s of
            | "--help" => acc_r->help := true
            | "-h" => acc_r->help := true
            | "--table" => acc_r->table := true // TODO fail when double flags
            | "-t" => acc_r->table := true
            | "--table" => if not(acc.table) then
              acc_r->table := true
              else (println!("Error: flag " + s + " cannot appear twice") ; exit(0) ; ())
            | "-t" => if not(acc.table) then
              acc_r->table := true
              else (println!("Error: flag " + s + " cannot appear twice") ; exit(0) ; ())
            | "--version" => acc_r->version := true
            | "-V" => acc_r->version := true
            | "-e" => (println!("Error: flag " + s + " must be followed by an argument") ; exit(0) ; ())
            | "--exclude" => (println!("Error: flag " + s + " must be followed by an argument") ; exit(0) ; ())
            | _ => (println!("Error: flag " + s + " not recognized") ; exit(0) ; ())
        else
          if not(is_first) then
            acc_r->includes := list_cons(s, acc.includes)
          else
            ()
  in
    !acc_r
  end

fnx get_cli
  { n : int | n >= 1 }
  { m : nat | m < n }
  .<n-m>.
  ( argc: int n
  , argv: !argv(n)
  , current: int m
  , prev_is_exclude: bool
  , acc: command_line
  ) : command_line =
  let
    var arg = argv[current]
  in
    if current < argc - 1 then
      if arg != "--exclude" && arg != "-e" then
        let 
          val c = get_cli(argc, argv, current + 1, false, acc)
        in
          if prev_is_exclude && current != 0 then
            process_excludes(arg, c)
          else if current != 0 then
            process(arg, c, current = 0)
          else
            c
        end
      else
        let 
          val c = get_cli(argc, argv, current + 1, true, acc)
        in
          c
        end
    else
      if prev_is_exclude then
        process_excludes(arg, acc)
      else
        process(arg, acc, current = 0)
  end

fun version(): void =
  println!("polygot version 0.3.7\nCopyright (c) 2017 Vanessa McHale")

fun help(): void = 
print("polyglot - Count lines of code quickly.

USAGE: poly [DIRECTORY] ... [OPTION] ...

FLAGS:
    -V, --version            show version information
    -h, --help               display this help and exit
    -e, --exclude            exclude a directory
    -t, --table              display results in a table

When no directory is provided poly will execute in the
current directory.

Bug reports and updates: nest.pijul.com/vamchale/polyglot\n")

fun default_head(ls: List0(string)) : string =
  case+ ls of
    | list_cons(x, _) => x
    | list_nil() => "."

fun empty_file(): file =
  let
    var f = @{ files = 0
             , lines = 0
             } : file
  in
    f
  end

implement main0 (argc, argv) =
  let
    val cli = @{ version = false
               , help = false
               , table = false
               , excludes = list_nil()
               , includes = list_nil()
               } : command_line
    val parsed = get_cli(argc, argv, 0, false, cli)
    var isc = @{ rust = empty_file()
               , haskell = empty_file()
               , ats = empty_file()
               , python = empty_file()
               , vimscript = empty_file()
               , elm = empty_file()
               , idris = empty_file()
               , madlang = empty_file()
               , tex = empty_file()
               , markdown = empty_file()
               , yaml = empty_file()
               , toml = empty_file()
               , cabal = empty_file()
               , happy = empty_file()
               , alex = empty_file()
               , go = empty_file()
               , html = empty_file()
               , css = empty_file()
               , verilog = empty_file()
               , vhdl = empty_file()
               , c = empty_file()
               , purescript = empty_file()
               , futhark = empty_file()
               , brainfuck = empty_file()
               , ruby = empty_file()
               , julia = empty_file()
               , perl = empty_file()
               , ocaml = empty_file()
               , agda = empty_file()
               , cobol = empty_file()
               , tcl = empty_file()
               , r = empty_file()
               , lua = empty_file()
               , cpp = empty_file()
               , lalrpop = empty_file()
               , header = empty_file()
               , sixten = empty_file()
               , dhall = empty_file()
               , ipkg = empty_file()
               , makefile = empty_file()
               , justfile = empty_file()
               , ion = empty_file()
               , bash = empty_file()
               , hamlet = empty_file()
               , cassius = empty_file()
               , lucius = empty_file()
               , julius = empty_file()
               , mercury = empty_file()
               , yacc = empty_file()
               , lex = empty_file()
               , coq = empty_file()
               , jupyter = empty_file()
               , java = empty_file()
               , scala = empty_file()
               , erlang = empty_file()
               , elixir = empty_file()
               , pony = empty_file()
               , clojure = empty_file()
               , cabal_project = empty_file()
               , assembly = empty_file()
               , nix = empty_file()
               , php = empty_file()
               , javascript = empty_file()
               , kotlin = empty_file()
               , fsharp = empty_file()
               , fortran = empty_file()
               , swift = empty_file()
               , csharp = empty_file()
               , nim = empty_file()
               , cpp_header = empty_file()
               , elisp = empty_file()
               , plaintext = empty_file()
               , rakefile = empty_file()
               , llvm = empty_file()
               , autoconf = empty_file()
               , batch = empty_file()
               , powershell = empty_file()
               , m4 = empty_file()
               , objective_c = empty_file()
               , automake = empty_file()
               } : source_contents
  in
    if parsed.help
      then
        ( help() ; exit(0) )
    else if parsed.version
      then
        ( version() ; exit(0) )
    else
      if parsed.table
        then
          print(make_table(step_stream(isc, default_head(parsed.includes), default_head(parsed.includes), parsed.excludes))) // TODO fold over these values and whatnot.
      else
      else
      else
      else
        print(make_output(step_stream(isc, default_head(parsed.includes), default_head(parsed.includes), parsed.excludes)))