Skip to content

Commit 4bba567

Browse files
committed
Ctrl+R with merged zsh history
1 parent 3b26a59 commit 4bba567

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

jobs/all/cache_zsh_history.job

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -o pipefail
4+
5+
evry 1 day -cache-zsh-history && {
6+
printlog 'cache_zsh_history: updating zsh history cache...'
7+
hpi query my.zsh.history -s --order-key dt --reverse | jq '.command' | unique | sponge "${HOME}/.cache/ctrl_r_zsh_history" || send-error 'couldnt update zsh history'
8+
}

scripts/fc-all-zsh-history

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -o pipefail
4+
5+
{
6+
parse-zsh-history --reverse | jq '.command' | unique
7+
cat "${HOME}/.cache/ctrl_r_zsh_history"
8+
} | unique

scripts/fzf_history_widget.zsh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# override the CTRL+R widget using my hpi zsh history
2+
# combines/merges (w/ a cache) zsh history from all my machines
3+
4+
fzf-history-widget() {
5+
local selected num
6+
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2>/dev/null
7+
selected="$(fc-all-zsh-history | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |
8+
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore ${FZF_CTRL_R_OPTS} --query=${(qqq)LBUFFER} +m" $(__fzfcmd))"
9+
local ret=$?
10+
if [[ -n "$selected" ]]; then
11+
# use jq -r to expand newlines
12+
BUFFER="$(jq -r <<<"$selected")"
13+
fi
14+
zle end-of-line
15+
zle reset-prompt
16+
return $ret
17+
}

scripts/parse-zsh-history

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import os
5+
from pathlib import Path
6+
from typing import Optional
7+
8+
import click
9+
from my.zsh import _parse_file
10+
from my.core.serialize import dumps
11+
from more_itertools import always_reversible
12+
13+
14+
default: Path = Path(os.environ.get("ZDOTDIR", Path.home())) / ".zsh_history"
15+
16+
17+
@click.command()
18+
@click.option("--reverse/--no-reverse", required=False, is_flag=True, default=False)
19+
@click.argument(
20+
"ZSH_HIST_FILE",
21+
type=click.Path(exists=True, path_type=Path),
22+
required=True,
23+
default=default,
24+
)
25+
def main(reverse: bool, zsh_hist_file: Path) -> None:
26+
"""
27+
Parses your current zsh history file as JSON
28+
"""
29+
res = _parse_file(zsh_hist_file)
30+
if reverse:
31+
res = always_reversible(res)
32+
for item in res:
33+
sys.stdout.write(dumps(item))
34+
sys.stdout.write("\n")
35+
sys.stdout.flush()
36+
37+
38+
if __name__ == "__main__":
39+
main()

scripts/zsh-fzf

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)