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).
|
|
|
|
|
2021-04-29 14:16:25 +00:00
|
|
|
zmodload zsh/parameter # Needed to access jobstates variable for STARSHIP_JOBS_COUNT
|
|
|
|
|
2021-01-23 10:25:41 +00:00
|
|
|
# Defines a function `__starship_get_time` that sets the time since epoch in millis in STARSHIP_CAPTURED_TIME.
|
|
|
|
if [[ $ZSH_VERSION == ([1-4]*) ]]; then
|
|
|
|
# ZSH <= 5; Does not have a built-in variable so we will rely on Starship's inbuilt time function.
|
|
|
|
__starship_get_time() {
|
|
|
|
STARSHIP_CAPTURED_TIME=$(::STARSHIP:: time)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
zmodload zsh/datetime
|
|
|
|
zmodload zsh/mathfunc
|
|
|
|
__starship_get_time() {
|
2021-03-15 10:39:35 +00:00
|
|
|
(( STARSHIP_CAPTURED_TIME = int(rint(EPOCHREALTIME * 1000)) ))
|
2021-01-23 10:25:41 +00:00
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
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 $?
|
2021-07-28 16:26:00 +00:00
|
|
|
STARSHIP_CMD_STATUS=$? STARSHIP_PIPE_STATUS=(${pipestatus[@]})
|
2019-09-11 00:31:08 +00:00
|
|
|
|
2020-06-09 16:52:29 +00:00
|
|
|
# Compute cmd_duration, if we have a time to consume, otherwise clear the
|
|
|
|
# previous duration
|
2021-03-15 10:39:35 +00:00
|
|
|
if (( ${+STARSHIP_START_TIME} )); then
|
|
|
|
__starship_get_time && (( STARSHIP_DURATION = STARSHIP_CAPTURED_TIME - STARSHIP_START_TIME ))
|
2019-09-11 00:31:08 +00:00
|
|
|
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
|
2021-04-29 14:16:25 +00:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
STARSHIP_JOBS_COUNT=${#jobstates}
|
2019-09-11 00:31:08 +00:00
|
|
|
}
|
2020-05-28 16:21:36 +00:00
|
|
|
starship_preexec() {
|
2021-01-23 10:25:41 +00:00
|
|
|
__starship_get_time && STARSHIP_START_TIME=$STARSHIP_CAPTURED_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)
|
2021-03-15 10:39:35 +00:00
|
|
|
(( ! ${+precmd_functions} )) && precmd_functions=()
|
|
|
|
(( ! ${+preexec_functions} )) && preexec_functions=()
|
2019-09-11 00:31:08 +00:00
|
|
|
|
|
|
|
# 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
|
2021-09-09 20:19:01 +00:00
|
|
|
if [[ -z ${preexec_functions[(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() {
|
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.
|
|
|
|
# zle-keymap-select is a special widget so it'll be "user:fnName" or nothing. Let's get fnName only.
|
2021-05-12 20:14:48 +00:00
|
|
|
__starship_preserved_zle_keymap_select=${widgets[zle-keymap-select]#user:}
|
|
|
|
if [[ -z $__starship_preserved_zle_keymap_select ]]; then
|
2020-10-27 18:01:52 +00:00
|
|
|
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() {
|
2021-05-12 20:14:48 +00:00
|
|
|
$__starship_preserved_zle_keymap_select "$@";
|
2020-10-27 18:01:52 +00:00
|
|
|
starship_zle-keymap-select "$@";
|
|
|
|
}
|
|
|
|
zle -N zle-keymap-select starship_zle-keymap-select-wrapped;
|
|
|
|
fi
|
|
|
|
|
2021-01-23 10:25:41 +00:00
|
|
|
__starship_get_time && STARSHIP_START_TIME=$STARSHIP_CAPTURED_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
|
2020-11-07 15:30:08 +00:00
|
|
|
STARSHIP_SESSION_KEY="$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM"; # Random generates a number b/w 0 - 32767
|
|
|
|
STARSHIP_SESSION_KEY="${STARSHIP_SESSION_KEY}0000000000000000" # Pad it to 16+ chars.
|
|
|
|
export STARSHIP_SESSION_KEY=${STARSHIP_SESSION_KEY:0:16}; # Trim to 16-digits if excess.
|
2021-03-15 10:39:35 +00:00
|
|
|
|
|
|
|
VIRTUAL_ENV_DISABLE_PROMPT=1
|
|
|
|
|
2021-04-29 14:16:25 +00:00
|
|
|
setopt promptsubst
|
2021-10-09 17:13:49 +00:00
|
|
|
|
|
|
|
PROMPT='$(::STARSHIP:: prompt --terminal-width="$COLUMNS" --keymap="${KEYMAP:-}" --status="$STARSHIP_CMD_STATUS" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --cmd-duration="${STARSHIP_DURATION:-}" --jobs="$STARSHIP_JOBS_COUNT")'
|
|
|
|
RPROMPT='$(::STARSHIP:: prompt --right --terminal-width="$COLUMNS" --keymap="${KEYMAP:-}" --status="$STARSHIP_CMD_STATUS" --pipestatus="${STARSHIP_PIPE_STATUS[*]}" --cmd-duration="${STARSHIP_DURATION:-}" --jobs="$STARSHIP_JOBS_COUNT")'
|
2021-10-06 21:36:28 +00:00
|
|
|
|