feat: add search capabilities with fzf+rg

This commit is contained in:
Viktor Varland 2025-06-16 13:48:40 +02:00
parent f538b45e84
commit 1dc3e8842f
Signed by: varl
GPG key ID: 7459F0B410115EE8
3 changed files with 87 additions and 32 deletions

View file

@ -2,29 +2,37 @@
## zsh ## zsh
```
cd $HOME/dev/vlv
git clone git.meatbag.se:vlv/shell-history.git shell-history
mkdir histdb
```
`~/.zshrc`: `~/.zshrc`:
``` ```
SHISTDB="$HOME/dev/vlv/histdb"
SHIST="$HOME/dev/vlv/shell-history"
autoload -Uz compinit promptinit add-zsh-hook autoload -Uz compinit promptinit add-zsh-hook
_history() { histdb_dir="$HOME/dev/vlv/histdb"
if test "$(id -u)" -ne 0; then histcmd_dir="$HOME/dev/vlv/shell-history"
echo "$(date --utc --iso-8601=seconds) $(hostname) $(pwd) $(fc -ln -1)" >> \
$SHISTDB/zsh-history-$(date "+%Y-%m-%d").log;
fi
}
add-zsh-hook precmd _history
_history-sync() { _add_history() {
if [[ -d "$SHISTDB" ]]; then if test "$(id -u)" -ne 0; then
() { echo "$(date --utc --iso-8601=seconds) $(hostname) $(pwd) $(fc -ln -1)" >> $histdb_dir/zsh-history-$(date "+%Y-%m-%d").log;
pushd -q "$SHISTDB"
source "$SHIST/sync.sh"
popd -q
}
fi fi
} }
add-zsh-hook precmd _add_history
hsup() {
() {
source $histcmd_dir/sync.sh "$histdb_dir"
}
}
hs() {
() {
source $histcmd_dir/search.sh "$histdb_dir" "$@"
}
}
``` ```

36
search.sh Executable file
View file

@ -0,0 +1,36 @@
#!/bin/zsh
histdb="$1"
shift
copy_cmd=
case "$XDG_SESSION_TYPE" in
x11) copy_cmd="xsel --clipboard" ;;
wayland) copy_cmd="wl-copy --trim-newline" ;;
*) echo "Session type not detected."; return ;;
esac
if [[ -d "$histdb" ]]; then
pushd -q "$histdb"
RG_PREFIX="rg --no-filename --no-line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
entry="$(
fzf --ansi --disabled --query "$INITIAL_QUERY" \
--bind "start:reload:$RG_PREFIX {q}" \
--bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
--delimiter :
)"
entry_cmd="$(<<<$entry awk -F' ' '{ print substr($0, index($0, $4)) }')"
if [[ "$entry" ]]; then
eval $copy_cmd <<< "$entry_cmd" >/dev/null
echo "Copied to clipboard." >&2
fi
popd -q
else
echo "\$histdb does not exist at: \"$histdb\" "
fi

11
sync.sh
View file

@ -1,5 +1,11 @@
#!/bin/zsh #!/bin/zsh
histdb="$1"
shift
if [[ -d "$histdb" ]]; then
pushd -q "$histdb"
git add zsh-history-*.log git add zsh-history-*.log
git commit --all --message "$(hostname): update history" git commit --all --message "$(hostname): update history"
@ -21,3 +27,8 @@ for f in ./zsh-history-*.log; do
done done
git push git push
popd -q
else
echo "\$histdb does not exist at: \"$histdb\" "
fi