XAMPPRocky / tokei

Count your code, quickly.
Other
11.02k stars 533 forks source link

Feature request: a percent column #230

Open quodlibetor opened 6 years ago

quodlibetor commented 6 years ago

It would be nice to be able view the ratio of different kinds of code in a project, to get an overview.

BrainMaestro commented 6 years ago

@Aaronepower I can implement this if you are fine with including the feature

XAMPPRocky commented 6 years ago

@BrainMaestro Go ahead.

XAMPPRocky commented 5 years ago

So the more I've thought about this the more I think that having the absolute numbers is really not necessary as it it's likely that someone will run tokei twice to get absolutes and relatives. So just printing percentages would be an ideal solution.

NightMachinery commented 4 years ago

@XAMPPRocky commented on Oct 21, 2018, 3:45 PM GMT+3:30:

So the more I've thought about this the more I think that having the absolute numbers is really not necessary as it it's likely that someone will run tokei twice to get absolutes and relatives. So just printing percentages would be an ideal solution.

I have written a zsh script to convert from tokei's absolute output to relative:

function tokei-percent() {
    # tokei 12.0.4
    local t="$(tokei)"
    local headers=($(<<<$t gsed -n 2p))
    local totals=( $(<<<$t tac | gsed -n 2p) )
    local lines=( "${(@f)$(<<<$t gsed 1,2d)}" )

    local line field fields i newfield lastfield newline=()
    for line in "$lines[@]" ; do
        if [[ "$line" =~ '(^-*$)|(Total)' ]] ; then
            continue
        fi
        fields=( $(print -r -- $line) )
        i=2
        lastfield=()
        for field in "$fields[@]" ; do
            if [[ "$field" =~ '^\d+$' ]] ; then
                newfield=${"$(( field * 100 / ${totals[$i]}. ))"}
                newfield="$(printf "%.1f" "$newfield")"
                test -n "$lastfield[*]" && {
                    newline+=$lastfield[*]
                    lastfield=()
                }
                newline+=$newfield
                i=$((i+1))
            else
                lastfield+=$field
            fi
        done
    done
    table-print "$headers[@]" -- "$newline[@]"
}

table-print () {
    perl -MText::ASCIITable -e '
    $t = Text::ASCIITable->new({drawRowLine => 1});
    while (defined($c = shift @ARGV) and $c ne "--") {
      push @header, $c;
      $cols++
    }
    $t->setCols(@header);
    $rows = @ARGV / $cols;
    for ($i = 0; $i < $rows; $i++) {
      for ($j = 0; $j < $cols; $j++) {
        $cell[$i][$j] = $ARGV[$i * $cols + $j]
      }
    }
    $t->addRow(\@cell);
    print $t' -- "$@"
}
# needs `cpanm Text::ASCIITable`

Sample output:

.---------------------------------------------------------.
| Language     | Files | Lines | Code | Comments | Blanks |
+--------------+-------+-------+------+----------+--------+
| ActionScript |   0.9 |   0.3 |  0.4 |      0.0 |    0.3 |
+--------------+-------+-------+------+----------+--------+
| BASH         |   8.0 |   7.3 |  7.3 |      7.1 |    7.9 |
+--------------+-------+-------+------+----------+--------+
| INI          |   0.5 |   0.8 |  0.8 |      0.5 |    1.4 |
+--------------+-------+-------+------+----------+--------+
| JavaScript   |   3.3 |   2.0 |  1.5 |      3.8 |    5.0 |
+--------------+-------+-------+------+----------+--------+
| JSON         |   0.5 |   0.2 |  0.2 |      0.0 |    0.0 |
+--------------+-------+-------+------+----------+--------+
| Python       |  11.7 |  25.4 | 24.3 |     25.0 |   38.6 |
+--------------+-------+-------+------+----------+--------+
| Ruby         |   0.5 |   0.1 |  0.1 |      0.1 |    0.0 |
+--------------+-------+-------+------+----------+--------+
| Rust         |   0.9 |   0.5 |  0.4 |      1.4 |    1.1 |
+--------------+-------+-------+------+----------+--------+
| Shell        |   0.5 |   1.1 |  1.1 |      1.8 |    0.1 |
+--------------+-------+-------+------+----------+--------+
| Plain Text   |   1.4 |   0.6 |  0.0 |      4.4 |    0.0 |
+--------------+-------+-------+------+----------+--------+
| Zsh          |  71.8 |  61.7 | 64.0 |     56.1 |   45.6 |
'--------------+-------+-------+------+----------+--------'
.----------------------------------------------------------.
| Language      | Files | Lines | Code | Comments | Blanks |
+---------------+-------+-------+------+----------+--------+
| CSS           |   3.9 |  64.0 | 64.9 |     30.9 |   76.3 |
+---------------+-------+-------+------+----------+--------+
| Dhall         |   0.2 |   0.0 |  0.0 |      0.0 |    0.0 |
+---------------+-------+-------+------+----------+--------+
| Emacs Lisp    |   0.2 |   0.0 |  0.0 |      0.3 |    0.0 |
+---------------+-------+-------+------+----------+--------+
| JavaScript    |   1.2 |   5.8 |  6.3 |      5.0 |    1.4 |
+---------------+-------+-------+------+----------+--------+
| Markdown      |  91.9 |   4.8 |  0.0 |     60.0 |   17.6 |
+---------------+-------+-------+------+----------+--------+
| Org           |   1.4 |   0.2 |  0.2 |      0.0 |    0.4 |
+---------------+-------+-------+------+----------+--------+
| Plain Text    |   0.6 |   0.1 |  0.0 |      1.2 |    0.2 |
+---------------+-------+-------+------+----------+--------+
| HTML          |   0.4 |  24.9 | 28.5 |      2.4 |    4.0 |
+---------------+-------+-------+------+----------+--------+
| |- CSS        |   0.2 |   0.0 |  0.0 |      0.0 |    0.0 |
+---------------+-------+-------+------+----------+--------+
| |- JavaScript |   0.2 |   0.1 |  0.1 |      0.3 |    0.2 |
'---------------+-------+-------+------+----------+--------'