tokei icon indicating copy to clipboard operation
tokei copied to clipboard

Feature request: a percent column

Open quodlibetor opened this issue 7 years ago • 4 comments

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

quodlibetor avatar Jun 01 '18 18:06 quodlibetor

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

BrainMaestro avatar Sep 04 '18 15:09 BrainMaestro

@BrainMaestro Go ahead.

XAMPPRocky avatar Sep 04 '18 22:09 XAMPPRocky

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.

XAMPPRocky avatar Oct 21 '18 12:10 XAMPPRocky

@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 |
'---------------+-------+-------+------+----------+--------'

NightMachinery avatar Aug 06 '20 12:08 NightMachinery