Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sindresorhus/pure
Browse files Browse the repository at this point in the history
  • Loading branch information
chabou committed Aug 26, 2018
2 parents e56e51e + 9325fe6 commit f4ef240
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 168 deletions.
110 changes: 82 additions & 28 deletions async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
#
# zsh-async
#
# version: 1.5.0
# version: 1.7.0
# author: Mathias Fredriksson
# url: https://github.com/mafredri/zsh-async
#

typeset -g ASYNC_VERSION=1.7.0
# Produce debug output from zsh-async when set to 1.
ASYNC_DEBUG=${ASYNC_DEBUG:-0}
typeset -g ASYNC_DEBUG=${ASYNC_DEBUG:-0}

# Execute commands that can manipulate the environment inside the async worker. Return output via callback.
_async_eval() {
local ASYNC_JOB_NAME
# Rename job to _async_eval and redirect all eval output to cat running
# in _async_job. Here, stdout and stderr are not separated for
# simplicity, this could be improved in the future.
{
eval "$@"
} &> >(ASYNC_JOB_NAME=[async/eval] _async_job 'cat')
}

# Wrapper for jobs executed by the async worker, gives output in parseable format with execution time
_async_job() {
# Disable xtrace as it would mangle the output.
setopt localoptions noxtrace

# Store start time as double precision (+E disables scientific notation)
# Store start time for job.
float -F duration=$EPOCHREALTIME

# Run the command and capture both stdout (`eval`) and stderr (`cat`) in
Expand All @@ -25,6 +37,7 @@ _async_job() {
# block, after the command block has completed, the stdin for `cat` is
# closed, causing stderr to be appended with a $'\0' at the end to mark the
# end of output from this job.
local jobname=${ASYNC_JOB_NAME:-$1}
local stdout stderr ret tok
{
stdout=$(eval "$@")
Expand All @@ -35,7 +48,7 @@ _async_job() {
read -r -k 1 -p tok || exit 1

# Return output (<job_name> <return_code> <stdout> <duration> <stderr>).
print -r -n - ${(q)1} $ret ${(q)stdout} $duration
print -r -n - $'\0'${(q)jobname} $ret ${(q)stdout} $duration
} 2> >(stderr=$(cat) && print -r -n - " "${(q)stderr}$'\0')

# Unlock mutex by inserting a token.
Expand Down Expand Up @@ -131,7 +144,7 @@ _async_worker() {
coproc_pid=0 # Reset pid.
}

local request
local request do_eval=0
local -a cmd
while :; do
# Wait for jobs sent by async_job.
Expand All @@ -146,8 +159,9 @@ _async_worker() {

# Check for non-job commands sent to worker
case $request in
_unset_trap) notify_parent=0; continue;;
_killjobs) killjobs; continue;;
_unset_trap) notify_parent=0; continue;;
_killjobs) killjobs; continue;;
_async_eval*) do_eval=1;;
esac

# Parse the request using shell parsing (z) to allow commands
Expand Down Expand Up @@ -180,18 +194,27 @@ _async_worker() {
print -n -p "t"
fi

# Run job in background, completed jobs are printed to stdout.
_async_job $cmd &
# Store pid because zsh job manager is extremely unflexible (show jobname as non-unique '$job')...
storage[$job]="$!"
if (( do_eval )); then
shift cmd # Strip _async_eval from cmd.
_async_eval $cmd
do_eval=0
else
# Run job in background, completed jobs are printed to stdout.
_async_job $cmd &
# Store pid because zsh job manager is extremely unflexible (show jobname as non-unique '$job')...
storage[$job]="$!"
fi

processing=0 # Disable guard.
done
}

#
# Get results from finnished jobs and pass it to the to callback function. This is the only way to reliably return the
# job name, return code, output and execution time and with minimal effort.
# Get results from finished jobs and pass it to the to callback function. This is the only way to reliably return the
# job name, return code, output and execution time and with minimal effort.
#
# If the async process buffer becomes corrupt, the callback will be invoked with the first argument being `[async]` (job
# name), non-zero return code and fifth argument describing the error (stderr).
#
# usage:
# async_process_results <worker_name> <callback_function>
Expand All @@ -202,16 +225,17 @@ _async_worker() {
# $3 = resulting stdout from execution
# $4 = execution time, floating point e.g. 2.05 seconds
# $5 = resulting stderr from execution
# $6 = has next result in buffer (0 = buffer empty, 1 = yes)
#
async_process_results() {
setopt localoptions noshwordsplit
setopt localoptions unset noshwordsplit noksharrays noposixidentifiers noposixstrings

local worker=$1
local callback=$2
local caller=$3
local -a items
local null=$'\0' data
integer -l len pos num_processed
integer -l len pos num_processed has_next

typeset -gA ASYNC_PROCESS_BUFFER

Expand All @@ -234,19 +258,23 @@ async_process_results() {
# Remove the extracted items from the buffer.
ASYNC_PROCESS_BUFFER[$worker]=${ASYNC_PROCESS_BUFFER[$worker][$pos+1,$len]}

len=${#ASYNC_PROCESS_BUFFER[$worker]}
if (( len > 1 )); then
pos=${ASYNC_PROCESS_BUFFER[$worker][(i)$null]} # Get index of NULL-character (delimiter).
fi

has_next=$(( len != 0 ))
if (( $#items == 5 )); then
items+=($has_next)
$callback "${(@)items}" # Send all parsed items to the callback.
(( num_processed++ ))
elif [[ -z $items ]]; then
# Empty items occur between results due to double-null ($'\0\0')
# caused by commands being both pre and suffixed with null.
else
# In case of corrupt data, invoke callback with *async* as job
# name, non-zero exit status and an error message on stderr.
$callback "async" 1 "" 0 "$0:$LINENO: error: bad format, got ${#items} items (${(@q)items})"
fi

(( num_processed++ ))

len=${#ASYNC_PROCESS_BUFFER[$worker]}
if (( len > 1 )); then
pos=${ASYNC_PROCESS_BUFFER[$worker][(i)$null]} # Get index of NULL-character (delimiter).
$callback "[async]" 1 "" 0 "$0:$LINENO: error: bad format, got ${#items} items (${(q)items})" $has_next
fi
done
done
Expand Down Expand Up @@ -279,7 +307,31 @@ _async_zle_watcher() {
# async_job <worker_name> <my_function> [<function_params>]
#
async_job() {
setopt localoptions noshwordsplit
setopt localoptions noshwordsplit noksharrays noposixidentifiers noposixstrings

local worker=$1; shift

local -a cmd
cmd=("$@")
if (( $#cmd > 1 )); then
cmd=(${(q)cmd}) # Quote special characters in multi argument commands.
fi

# Quote the cmd in case RC_EXPAND_PARAM is set.
zpty -w $worker "$cmd"$'\0'
}

#
# Evaluate a command (like async_job) inside the async worker, then worker environment can be manipulated. For example,
# issuing a cd command will change the PWD of the worker which will then be inherited by all future async jobs.
#
# Output will be returned via callback, job name will be [async/eval].
#
# usage:
# async_worker_eval <worker_name> <my_function> [<function_params>]
#
async_worker_eval() {
setopt localoptions noshwordsplit noksharrays noposixidentifiers noposixstrings

local worker=$1; shift

Expand All @@ -289,13 +341,15 @@ async_job() {
cmd=(${(q)cmd}) # Quote special characters in multi argument commands.
fi

zpty -w $worker $cmd$'\0'
# Quote the cmd in case RC_EXPAND_PARAM is set.
zpty -w $worker "_async_eval $cmd"$'\0'
}

# This function traps notification signals and calls all registered callbacks
_async_notify_trap() {
setopt localoptions noshwordsplit

local k
for k in ${(k)ASYNC_CALLBACKS}; do
async_process_results $k ${ASYNC_CALLBACKS[$k]} trap
done
Expand Down Expand Up @@ -442,7 +496,7 @@ async_start_worker() {
async_stop_worker() {
setopt localoptions noshwordsplit

local ret=0
local ret=0 worker k v
for worker in $@; do
# Find and unregister the zle handler for the worker
for k v in ${(@kv)ASYNC_PTYS}; do
Expand Down Expand Up @@ -470,14 +524,14 @@ async_stop_worker() {
#
async_init() {
(( ASYNC_INIT_DONE )) && return
ASYNC_INIT_DONE=1
typeset -g ASYNC_INIT_DONE=1

zmodload zsh/zpty
zmodload zsh/datetime

# Check if zsh/zpty returns a file descriptor or not,
# shell must also be interactive with zle enabled.
ASYNC_ZPTY_RETURNS_FD=0
typeset -g ASYNC_ZPTY_RETURNS_FD=0
[[ -o interactive ]] && [[ -o zle ]] && {
typeset -h REPLY
zpty _async_test :
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pure-prompt-now",
"version": "1.5.4",
"description": "Pretty, minimal and fast ZSH prompt with zeit's now flavor",
"version": "1.8.0",
"description": "Pretty, minimal and fast ZSH prompt with Zeit's Now flavor",
"license": "MIT",
"repository": "chabou/pure-now",
"author": {
Expand All @@ -21,7 +21,7 @@
"scripts": {
"postinstall": "PURE_DEST=/usr/local/share/zsh/site-functions npm run --silent postinstall-link && exit 0; PURE_DEST=\"$PWD/functions\" npm run postinstall-link && npm run postinstall-fail-instructions",
"postinstall-link": "mkdir -p \"$PURE_DEST\" && ln -sf \"$PWD/pure.zsh\" \"$PURE_DEST/prompt_pure-now_setup\" && ln -sf \"$PWD/async.zsh\" \"$PURE_DEST/async\" && ln -sf \"$PWD/now_functions.zsh\" \"$PURE_DEST/now_functions\"",
"postinstall-fail-instructions": "echo \"ERROR: Could not automagically symlink the prompt. Either:\\n1. Check out the readme on how to do it manually: https://github.com/sindresorhus/pure#manually\\n2. Or add the following to your \\`.zshrc\\`:\\n\\n fpath+=(\\$fpath '$PWD/functions')\""
"postinstall-fail-instructions": "echo \"ERROR: Could not automagically symlink the prompt. Either:\\n1. Check out the readme on how to do it manually: https://github.com/chabou/pure-now#manually\\n2. Or add the following to your \\`.zshrc\\`:\\n\\n fpath+=('$PWD/functions')\""
},
"files": [
"pure.zsh",
Expand Down
Loading

0 comments on commit f4ef240

Please sign in to comment.