26 lines
1015 B
Bash
26 lines
1015 B
Bash
|
|
#!/bin/sh
|
||
|
|
# History source: suggests the closest-looking command you've actually
|
||
|
|
# typed and moved on from before (thefuck's history.py idea). Reads
|
||
|
|
# HISTFILE directly since a separate process doesn't inherit the parent
|
||
|
|
# shell's in-memory history.
|
||
|
|
#
|
||
|
|
# Single awk pass over the tail of the file: on weak/emulated hardware
|
||
|
|
# every avoided fork+exec matters (measured: an unoptimized PATH walk in
|
||
|
|
# 40-pathfuzzy.sh took >30s on this box before being fixed; pipeline
|
||
|
|
# depth has the same tax, just smaller per-hop) -- a five-process
|
||
|
|
# tail|grep|grep|awk|tail chain does the same job as one awk script
|
||
|
|
# reading the file once.
|
||
|
|
cmd="$1"
|
||
|
|
first_word=$(printf '%s' "$cmd" | awk '{print $1}')
|
||
|
|
[ -n "$first_word" ] || exit 0
|
||
|
|
hf="${HISTFILE:-$HOME/.bash_history}"
|
||
|
|
[ -r "$hf" ] || exit 0
|
||
|
|
|
||
|
|
tail -n 300 "$hf" 2>/dev/null | awk -v first="$first_word" -v full="$cmd" '
|
||
|
|
index($0, first " ") == 1 && $0 != full && !seen[$0]++ { buf[++n] = $0 }
|
||
|
|
END {
|
||
|
|
start = (n > 5) ? n - 4 : 1
|
||
|
|
for (i = start; i <= n; i++) print buf[i]
|
||
|
|
}
|
||
|
|
'
|