2019-09-11 00:31:08 +00:00
|
|
|
# ZSH has a quirk where `preexec` is only run if a command is actually run (i.e
|
|
|
|
# pressing ENTER at an empty command line will not cause preexec to fire). This
|
|
|
|
# can cause timing issues, as a user who presses "ENTER" without running a command
|
|
|
|
# will see the time to the start of the last command, which may be very large.
|
|
|
|
|
|
|
|
# To fix this, we create STARSHIP_START_TIME upon preexec() firing, and destroy it
|
|
|
|
# after drawing the prompt. This ensures that the timing for one command is only
|
|
|
|
# ever drawn once (for the prompt immediately after it is run).
|
|
|
|
|
|
|
|
zmodload zsh/parameter # Needed to access jobstates variable for NUM_JOBS
|
|
|
|
|
2020-06-09 16:52:29 +00:00
|
|
|
starship_render() {
|
|
|
|
# Use length of jobstates array as number of jobs. Expansion fails inside
|
|
|
|
# quotes so we set it here and then use the value later on.
|
|
|
|
NUM_JOBS=$#jobstates
|
|
|
|
PROMPT="$(::STARSHIP:: prompt --keymap="${KEYMAP-}" --status=$STATUS --cmd-duration=${STARSHIP_DURATION-} --jobs="$NUM_JOBS")"
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:31:08 +00:00
|
|
|
# Will be run before every prompt draw
|
|
|
|
starship_precmd() {
|
|
|
|
# Save the status, because commands in this pipeline will change $?
|
|
|
|
STATUS=$?
|
|
|
|
|
2020-06-09 16:52:29 +00:00
|
|
|
# Compute cmd_duration, if we have a time to consume, otherwise clear the
|
|
|
|
# previous duration
|
2020-05-28 16:21:36 +00:00
|
|
|
if [[ -n "${STARSHIP_START_TIME+1}" ]]; then
|
2019-12-19 22:38:06 +00:00
|
|
|
STARSHIP_END_TIME=$(::STARSHIP:: time)
|
2019-09-11 00:31:08 +00:00
|
|
|
STARSHIP_DURATION=$((STARSHIP_END_TIME - STARSHIP_START_TIME))
|
|
|
|
unset STARSHIP_START_TIME
|
|
|
|
else
|
2020-06-09 16:52:29 +00:00
|
|
|
unset STARSHIP_DURATION
|
2019-09-11 00:31:08 +00:00
|
|
|
fi
|
2020-06-09 16:52:29 +00:00
|
|
|
|
|
|
|
# Render the updated prompt
|
|
|
|
starship_render
|
2019-09-11 00:31:08 +00:00
|
|
|
}
|
2020-05-28 16:21:36 +00:00
|
|
|
starship_preexec() {
|
2019-12-19 22:38:06 +00:00
|
|
|
STARSHIP_START_TIME=$(::STARSHIP:: time)
|
2019-09-11 00:31:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# If precmd/preexec arrays are not already set, set them. If we don't do this,
|
|
|
|
# the code to detect whether starship_precmd is already in precmd_functions will
|
|
|
|
# fail because the array doesn't exist (and same for starship_preexec)
|
|
|
|
[[ -z "${precmd_functions+1}" ]] && precmd_functions=()
|
|
|
|
[[ -z "${preexec_functions+1}" ]] && preexec_functions=()
|
|
|
|
|
|
|
|
# If starship precmd/preexec functions are already hooked, don't double-hook them
|
|
|
|
# to avoid unnecessary performance degradation in nested shells
|
2020-09-14 06:30:57 +00:00
|
|
|
if [[ -z ${precmd_functions[(re)starship_precmd]} ]]; then
|
2019-09-11 00:31:08 +00:00
|
|
|
precmd_functions+=(starship_precmd)
|
|
|
|
fi
|
2020-09-14 06:30:57 +00:00
|
|
|
if [[ -z ${preexec_function[(re)starship_preexec]} ]]; then
|
2019-09-11 00:31:08 +00:00
|
|
|
preexec_functions+=(starship_preexec)
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set up a function to redraw the prompt if the user switches vi modes
|
2020-10-27 18:01:52 +00:00
|
|
|
starship_zle-keymap-select() {
|
2020-06-09 16:52:29 +00:00
|
|
|
starship_render
|
2019-09-11 00:31:08 +00:00
|
|
|
zle reset-prompt
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:01:52 +00:00
|
|
|
## Check for existing keymap-select widget.
|
|
|
|
local existing_keymap_select_fn=$widgets[zle-keymap-select];
|
|
|
|
# zle-keymap-select is a special widget so it'll be "user:fnName" or nothing. Let's get fnName only.
|
|
|
|
existing_keymap_select_fn=${existing_keymap_select_fn//user:};
|
|
|
|
if [[ -z ${existing_keymap_select_fn} ]]; then
|
|
|
|
zle -N zle-keymap-select starship_zle-keymap-select;
|
|
|
|
else
|
|
|
|
# Define a wrapper fn to call the original widget fn and then Starship's.
|
|
|
|
starship_zle-keymap-select-wrapped() {
|
|
|
|
${existing_keymap_select_fn} "$@";
|
|
|
|
starship_zle-keymap-select "$@";
|
|
|
|
}
|
|
|
|
zle -N zle-keymap-select starship_zle-keymap-select-wrapped;
|
|
|
|
fi
|
|
|
|
|
2019-12-19 22:38:06 +00:00
|
|
|
STARSHIP_START_TIME=$(::STARSHIP:: time)
|
2019-09-11 00:31:08 +00:00
|
|
|
export STARSHIP_SHELL="zsh"
|
2020-09-28 20:38:50 +00:00
|
|
|
|
|
|
|
# Set up the session key that will be used to store logs
|
|
|
|
export STARSHIP_SESSION_KEY=$(::STARSHIP:: session)
|