1# shellcheck disable=SC2034,SC2153,SC2086,SC2155
 2
 3# Above line is because shellcheck doesn't support zsh, per
 4# https://github.com/koalaman/shellcheck/wiki/SC1071, and the ignore: param in
 5# ludeeus/action-shellcheck only supports _directories_, not _files_. So
 6# instead, we manually add any error the shellcheck step finds in the file to
 7# the above line ...
 8
 9# Source this in your ~/.zshrc
10autoload -U add-zsh-hook
11
12export ATUIN_SESSION=$(atuin uuid)
13export ATUIN_HISTORY="atuin history list"
14
15_atuin_preexec() {
16    local id
17    id=$(atuin history start -- "$1")
18    export ATUIN_HISTORY_ID="$id"
19}
20
21_atuin_precmd() {
22    local EXIT="$?"
23
24    [[ -z "${ATUIN_HISTORY_ID}" ]] && return
25
26    (RUST_LOG=error atuin history end --exit $EXIT -- $ATUIN_HISTORY_ID &) >/dev/null 2>&1
27}
28
29_atuin_search() {
30    emulate -L zsh
31    zle -I
32
33    # Switch to cursor mode, then back to application
34    echoti rmkx
35    # swap stderr and stdout, so that the tui stuff works
36    # TODO: not this
37    # shellcheck disable=SC2048
38    output=$(RUST_LOG=error atuin search $* -i -- $BUFFER 3>&1 1>&2 2>&3)
39    echoti smkx
40
41    if [[ -n $output ]]; then
42        RBUFFER=""
43        LBUFFER=$output
44    fi
45
46    zle reset-prompt
47}
48
49add-zsh-hook preexec _atuin_preexec
50add-zsh-hook precmd _atuin_precmd
51
52zle -N _atuin_search_widget _atuin_search
53
54bindkey '^r' _atuin_search_widget