Skip to content

Commit

Permalink
perf(bash/preexec): cache the results of tput
Browse files Browse the repository at this point in the history
With the current implementation, we spwan 10 processes of the tput
command at most every time we perform `enter_accept`.  In this patch,
to reduce the delay, we separate the related code into a function and
cache the results of the tput commands.
  • Loading branch information
akinomyoga committed Feb 4, 2024
1 parent 5d778f3 commit 7cd575b
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions atuin/src/shell/atuin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,36 @@ else
}
fi


# The shell function `__atuin_clear_prompt N` outputs terminal control
# sequences to clear the contents of the current and N previous lines. After
# clearing, the cursor is placed at the beginning of the N-th previous line.
__atuin_clear_prompt_cache=()
__atuin_clear_prompt() {
local offset=$1
if [[ ! ${__atuin_clear_prompt_cache[offset]+set} ]]; then
if [[ ! ${__atuin_clear_prompt_cache[0]+set} ]]; then
__atuin_clear_prompt_cache[0]=$'\r'$(tput el 2>/dev/null || tput ce 2>/dev/null)
fi
if ((offset > 0)); then
__atuin_clear_prompt_cache[offset]=${__atuin_clear_prompt_cache[0]}$(
tput cuu "$offset" 2>/dev/null || tput UP "$offset" 2>/dev/null
tput dl "$offset" 2>/dev/null || tput DL "$offset" 2>/dev/null
tput il "$offset" 2>/dev/null || tput AL "$offset" 2>/dev/null
)
fi
fi
printf '%s' "${__atuin_clear_prompt_cache[offset]}"
}

__atuin_accept_line() {
local __atuin_command=$1

# Reprint the prompt, accounting for multiple lines
local __atuin_prompt __atuin_prompt_offset
__atuin_evaluate_prompt
local __atuin_clear_prompt
__atuin_clear_prompt=$'\r'$(tput el 2>/dev/null || tput ce 2>/dev/null)
if ((__atuin_prompt_offset > 0)); then
__atuin_clear_prompt+=$(
tput cuu "$__atuin_prompt_offset" 2>/dev/null || tput UP "$__atuin_prompt_offset" 2>/dev/null
tput dl "$__atuin_prompt_offset" 2>/dev/null || tput DL "$__atuin_prompt_offset" 2>/dev/null
tput il "$__atuin_prompt_offset" 2>/dev/null || tput AL "$__atuin_prompt_offset" 2>/dev/null
)
fi
printf '%s\n' "$__atuin_clear_prompt$__atuin_prompt$__atuin_command"
__atuin_clear_prompt "$__atuin_prompt_offset"
printf '%s\n' "$__atuin_prompt$__atuin_command"

# Add it to the bash history
history -s "$__atuin_command"
Expand Down Expand Up @@ -164,7 +178,8 @@ __atuin_accept_line() {
# so to work for a multiline prompt we need to print it ourselves,
# then go to the beginning of the last line.
__atuin_evaluate_prompt
printf '%s\r%s' "$__atuin_prompt" "$(tput el 2>/dev/null || tput ce 2>/dev/null)"
printf '%s' "$__atuin_prompt"
__atuin_clear_prompt 0
}

__atuin_history() {
Expand Down

0 comments on commit 7cd575b

Please sign in to comment.