diff options
author | Jesse Luehrs <doy@tozt.net> | 2024-02-27 22:29:09 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2024-02-27 22:29:09 -0500 |
commit | d5e72ac1b3d1aacec097c1d41decbe748c035f06 (patch) | |
tree | e75f879e5cdf3b51eb23ab321eb6e8ec2ce6ea9a /bin/.bin/history-stats | |
parent | 0179eebc614a5668048b510cb7a0776354c16dc3 (diff) | |
download | conf-d5e72ac1b3d1aacec097c1d41decbe748c035f06.tar.gz conf-d5e72ac1b3d1aacec097c1d41decbe748c035f06.zip |
rearrange some more things
Diffstat (limited to 'bin/.bin/history-stats')
-rwxr-xr-x | bin/.bin/history-stats | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/bin/.bin/history-stats b/bin/.bin/history-stats new file mode 100755 index 0000000..0fead1f --- /dev/null +++ b/bin/.bin/history-stats @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Getopt::Long; + +my $history_file = $ENV{SHELL} =~ m{/zsh} ? '.zsh_history' : '.bash_history'; + +open my $fh, '<', "$ENV{HOME}/$history_file" + or die "couldn't open $ENV{HOME}/$history_file: $!"; +my $n = 10; +my $subcommand; +GetOptions( + 'n=i' => \$n, + 'subcommand=s' => \$subcommand, +); + +my %cmds; +my %sudo_cmds; + +while (<$fh>) { + chomp; + + # strip zsh history timestamps + s/^[^;]*;// if /^:/; + + s/^\s*(.*?)\s*$/$1/; + my @words = split /\s+/; + next unless @words; + shift @words while @words && $words[0] =~ /^[A-Z][A-Z0-9_]*=/; + next unless @words; + my $sudo; + if ($words[0] eq 'sudo') { + $sudo = 1; + shift @words; + } + if ($subcommand) { + next unless $words[0] eq $subcommand; + shift @words; + shift @words while @words && $words[0] =~ /^-/; + } + $cmds{($words[0] || '')}++; + $sudo_cmds{$words[0]}++ if $sudo; +} + +for my $cmd (sort { $cmds{$b} <=> $cmds{$a} } keys %cmds) { + printf '%5d %s', $cmds{$cmd}, $subcommand ? "$subcommand $cmd" : $cmd; + printf ' (%d under sudo)', $sudo_cmds{$cmd} if $sudo_cmds{$cmd}; + print "\n"; + last unless --$n; +} |