feat: add search capabilities with fzf+rg
This commit is contained in:
parent
f538b45e84
commit
1dc3e8842f
42
README.md
42
README.md
|
|
@ -2,29 +2,37 @@
|
|||
|
||||
## zsh
|
||||
|
||||
```
|
||||
cd $HOME/dev/vlv
|
||||
git clone git.meatbag.se:vlv/shell-history.git shell-history
|
||||
mkdir histdb
|
||||
```
|
||||
|
||||
`~/.zshrc`:
|
||||
|
||||
```
|
||||
SHISTDB="$HOME/dev/vlv/histdb"
|
||||
SHIST="$HOME/dev/vlv/shell-history"
|
||||
|
||||
autoload -Uz compinit promptinit add-zsh-hook
|
||||
|
||||
_history() {
|
||||
if test "$(id -u)" -ne 0; then
|
||||
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
|
||||
histdb_dir="$HOME/dev/vlv/histdb"
|
||||
histcmd_dir="$HOME/dev/vlv/shell-history"
|
||||
|
||||
_history-sync() {
|
||||
if [[ -d "$SHISTDB" ]]; then
|
||||
() {
|
||||
pushd -q "$SHISTDB"
|
||||
source "$SHIST/sync.sh"
|
||||
popd -q
|
||||
}
|
||||
_add_history() {
|
||||
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;
|
||||
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
36
search.sh
Executable 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
|
||||
23
sync.sh
23
sync.sh
|
|
@ -1,11 +1,17 @@
|
|||
#!/bin/zsh
|
||||
|
||||
git add zsh-history-*.log
|
||||
git commit --all --message "$(hostname): update history"
|
||||
histdb="$1"
|
||||
shift
|
||||
|
||||
git pull --no-edit --no-rebase
|
||||
if [[ -d "$histdb" ]]; then
|
||||
pushd -q "$histdb"
|
||||
|
||||
for f in ./zsh-history-*.log; do
|
||||
git add zsh-history-*.log
|
||||
git commit --all --message "$(hostname): update history"
|
||||
|
||||
git pull --no-edit --no-rebase
|
||||
|
||||
for f in ./zsh-history-*.log; do
|
||||
tmpfile=$(mktemp)
|
||||
exec 3>"$tmpfile"
|
||||
exec 4<"$tmpfile"
|
||||
|
|
@ -18,6 +24,11 @@ for f in ./zsh-history-*.log; do
|
|||
|
||||
git add $f
|
||||
git commit -m "$(hostname): resolve merge ${f}"
|
||||
done
|
||||
done
|
||||
|
||||
git push
|
||||
git push
|
||||
|
||||
popd -q
|
||||
else
|
||||
echo "\$histdb does not exist at: \"$histdb\" "
|
||||
fi
|
||||
|
|
|
|||
Loading…
Reference in a new issue