Skip to content

Commit

Permalink
feat: Add is_async util
Browse files Browse the repository at this point in the history
  • Loading branch information
denysdovhan committed Aug 10, 2021
1 parent 370df5e commit d539ee5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 6 additions & 3 deletions lib/hooks.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spaceship_exec_time_stop() {
unset SPACESHIP_EXEC_TIME_start
}

# A hook before every command
prompt_spaceship_precmd() {
# Retrieve exit code of last command to use in exit_code
# Must be captured before any other command in prompt is executed
Expand All @@ -32,7 +33,7 @@ prompt_spaceship_precmd() {
spaceship_exec_time_stop

# Restarts the async worker, in order to get an update-to-date shell environment
if [[ "${SPACESHIP[async]}" == true ]]; then
if $(spaceship::is_async); then
async_stop_worker "spaceship"
async_start_worker "spaceship" #-n
# setopt before call register to avoid callback by async_worker_eval
Expand All @@ -44,18 +45,20 @@ prompt_spaceship_precmd() {
spaceship::build_section_cache
}

# A hook right before the command is started executing
prompt_spaceship_preexec() {
# Stop running prompt async jobs
if [[ "${SPACESHIP[async]}" == true ]]; then
if $(spaceship::is_async); then
async_flush_jobs "spaceship"
fi

# Start measuring exec_time right before executing the command
spaceship_exec_time_start
}

# A hook after changing the working directory
prompt_spaceship_chpwd() {
if [[ "${SPACESHIP[async]}" == true ]]; then
if $(spaceship::is_async); then
async_worker_eval "spaceship" 'cd' "$PWD"
fi

Expand Down
5 changes: 2 additions & 3 deletions lib/renderer.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# spaceship::load_sections
spaceship::load_sections() {
local load_async=false
local section_async_var section_async

# Iterate over sections
for section in $(spaceship::union $SPACESHIP_PROMPT_ORDER $SPACESHIP_RPROMPT_ORDER); do
Expand All @@ -28,12 +27,12 @@ spaceship::load_sections() {

if $(spaceship::is_section_async $section); then
load_async=true
SPACESHIP[async]=true
fi
done

if ${load_async}; then
(( ASYNC_INIT_DONE )) || source "${SPACESHIP_ROOT}/async/async.zsh"
SPACESHIP[async]=true
fi
}

Expand All @@ -54,7 +53,7 @@ spaceship::build_section_cache() {
spaceship::set_cache open "$SPACESHIP_PROMPT_FIRST_PREFIX_SHOW"

for section in $(spaceship::union $SPACESHIP_PROMPT_ORDER $SPACESHIP_RPROMPT_ORDER); do
if $(spaceship::is_section_async $section); then
if $(spaceship::is_async) && $(spaceship::is_section_async $section); then
# TODO: Count started jobs
async_job "spaceship" "spaceship_${section}"
else
Expand Down
7 changes: 7 additions & 0 deletions lib/utils.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ spaceship::is_section_async() {
[[ "${(P)async_option}" == true ]]
}

# Check if async is available and there is an async section
# USAGE:
# spaceship::is_async
spaceship::is_async() {
(( ASYNC_INIT_DONE )) && [[ "${SPACESHIP[async]}" == true ]]
}

# Print message backward compatibility warning
# USAGE:
# spaceship::deprecated <deprecated> [message]
Expand Down

0 comments on commit d539ee5

Please sign in to comment.