diff --git a/README.md b/README.md index 57f3d6a..e4079db 100644 --- a/README.md +++ b/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" "$@" + } +} + ``` diff --git a/search.sh b/search.sh new file mode 100755 index 0000000..f479c17 --- /dev/null +++ b/search.sh @@ -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 diff --git a/sync.sh b/sync.sh index 5433876..39e0fd9 100755 --- a/sync.sh +++ b/sync.sh @@ -1,23 +1,34 @@ #!/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 - tmpfile=$(mktemp) - exec 3>"$tmpfile" - exec 4<"$tmpfile" - rm "$tmpfile" + git add zsh-history-*.log + git commit --all --message "$(hostname): update history" - grep --invert-match "^[<,>,=]\{5,\}" $f >&3 - <&4 tee $f + git pull --no-edit --no-rebase - sort --output="$f" "$f" + for f in ./zsh-history-*.log; do + tmpfile=$(mktemp) + exec 3>"$tmpfile" + exec 4<"$tmpfile" + rm "$tmpfile" - git add $f - git commit -m "$(hostname): resolve merge ${f}" -done + grep --invert-match "^[<,>,=]\{5,\}" $f >&3 + <&4 tee $f -git push + sort --output="$f" "$f" + + git add $f + git commit -m "$(hostname): resolve merge ${f}" + done + + git push + + popd -q +else + echo "\$histdb does not exist at: \"$histdb\" " +fi