IMOTK232APTO5HACVM2LWJXLF77PYVDGWPCMFBX3NMQUWRAFCUOAC
function srch_top
set NUM 3
set PRINT_COUNT False
set RG_ARGS
for arg in $argv
switch $arg
case '--top-*'
set NUM (echo $arg | sed 's/--top-//')
if not test $NUM -gt 0 ^ /dev/null
error_message "srch_top error: $arg must contain a number"
return 1
end
case --count -c
set PRINT_COUNT True
case '*'
set RG_ARGS $RG_ARGS $arg
end
end
set PY_SCRIPT "
import sys
from heapq import nlargest
files = sys.stdin.read().split()
key_func = lambda line: int(line.split(':').pop())
for l in nlargest($NUM, files, key=key_func):
path, count = l.split(':')
print(f'{count}: {path}')
"
if eval $PRINT_COUNT
rg -c $RG_ARGS | python3 -c $PY_SCRIPT
else
rg -c $RG_ARGS | python3 -c $PY_SCRIPT | sed 's/^.*: //'
end
end
function srch
set RG_ARGS
set N_LARGEST 0
for arg in $argv
switch $arg
case '-n*'
set -l num (echo $arg | rg -o '\d+')
if test $num -gt 0
set RG_ARGS $RG_ARGS "-c"
set N_LARGEST $num
end
case '*'
set RG_ARGS $RG_ARGS $arg
end
end
set PY_SCRIPT "
from heapq import nlargest
from sys import stdin
from sys import stdout
for match_count in nlargest($N_LARGEST, stdin.readlines(), key=lambda l: l.split(':')[-1]):
print(match_count.strip())
"
if test $N_LARGEST -gt 0
rg $RG_ARGS | python -c "$PY_SCRIPT"
else
rg $RG_ARGS
end
end
function srch() {
RG_ARGS=""
N_LARGEST=0
for arg in $@
do
case "$arg" in
-n*)
num=$(echo "$arg" | rg -o '\d+')
if ((nag > 0)); then
RG_ARGS="$RG_ARGS -c" # add count flag to rg
N_LARGEST=$num
fi
;;
*)
RG_ARGS="$RG_ARGS $arg"
esac
done
PY_SCRIPT="
from heapq import nlargest
from sys import stdin
from sys import stdout
for match_count in nlargest($N_LARGEST, stdin.readlines(), key=lambda l: l.split(':')[-1]):
print(match_count.strip())
"
if ((N_LARGEST > 0)); then
rg $RG_ARGS | python -c "$PY_SCRIPT";
else
rg $RG_ARGS
fi
}