Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/agkozak/agkozak-zsh-prompt
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
agkozak committed Aug 23, 2018
2 parents b32072c + 3d2c6d2 commit c39c0b1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ If you would like to display a different number of directory elements, set the e

![AGKOZAK_PROMPT_DIRTRIM](img/AGKOZAK_PROMPT_DIRTRIM.png)

By default, [static named directories created with `hash -d`](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Static-named-directories) will be displayed in the prompt just like any others. If you use them extensively, though, you may opt to set `AGKOZAK_NAMED_DIRS=1`. The prompt will then use the named directories as base directories for its path. For example, if you execute
By default, [static named directories created with `hash -d`](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Static-named-directories) will be used as base directories in the path the prompt displays. For example,
if you have executed

hash -d wp-content=/var/www/html/wp-content

then `/var/www/html/wp-content` will appear in the prompt as `~wp-content`, and `/var/www/html/wp-content/plugins/redirection/actions` will be represented as `~wp-content/.../redirection/actions`.
then `/var/www/html/wp-content` will appear in the prompt as `wp-content`, and `/var/www/html/wp-content/plugins/redirection/actions` will be represented as `~wp-content/.../redirection/actions`. If you prefer to have named directories displayed just like any others, set `AGKOZAK_NAMED_DIRS=0`.

## Git Branch and Status

Expand Down
13 changes: 9 additions & 4 deletions agkozak-zsh-prompt.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ _agkozak_is_ssh() {
#
# ~/.../polyglot/img
#
# If AGKOZAK_NAMED_DIRS is set to 1, ZSH named directories
# will be displayed using their aliases in the prompt.
# Named directories will by default be displayed using their
# aliases in the prompt. Set AGKOZAK_NAMED_DIRS=0 to have
# them displayed just like any other directory.
#
# Arguments:
# $1 Number of directory elements to display (default: 2)
############################################################
_agkozak_prompt_dirtrim() {
[[ $1 -ge 1 ]] || set 2
typeset -g AGKOZAK_NAMED_DIRS=${AGKOZAK_NAMED_DIRS:-1}
if (( AGKOZAK_NAMED_DIRS )); then
local zsh_pwd
zsh_pwd=$(print -Pn '%~')
Expand Down Expand Up @@ -261,19 +263,22 @@ TRAPWINCH() {
# ASYNCHRONOUS FUNCTIONS
###########################################################

typeset -g AGKOZAK_PROMPT_DIR="${0:A:h}"

###########################################################
# If zsh-async has not already been loaded, try to load it;
# the exit code should indicate success or failure
#
# Globals:
# AGKOZAK_PROMPT_DEBUG
# AGKOZAK_PROMPT_DIR
###########################################################
_agkozak_load_async_lib() {
if ! whence -w async_init &> /dev/null; then # Don't load zsh-async twice
if (( AGKOZAK_PROMPT_DEBUG )); then
source "${0:A:h}/lib/async.zsh"
source "${AGKOZAK_PROMPT_DIR}/lib/async.zsh"
else
source "${0:A:h}/lib/async.zsh" &> /dev/null
source "${AGKOZAK_PROMPT_DIR}/lib/async.zsh" &> /dev/null
fi
local success=$?
return $success
Expand Down
72 changes: 59 additions & 13 deletions lib/async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
#
# zsh-async
#
# version: 1.6.2
# version: 1.7.0
# author: Mathias Fredriksson
# url: https://github.com/mafredri/zsh-async
#

typeset -g ASYNC_VERSION=1.6.2
typeset -g ASYNC_VERSION=1.7.0
# Produce debug output from zsh-async when set to 1.
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.
Expand All @@ -26,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 @@ -36,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 - $'\0'${(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 @@ -132,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 @@ -147,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 @@ -181,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 Down Expand Up @@ -252,7 +274,7 @@ async_process_results() {
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})" $has_next
$callback "[async]" 1 "" 0 "$0:$LINENO: error: bad format, got ${#items} items (${(q)items})" $has_next
fi
done
done
Expand Down Expand Up @@ -299,6 +321,30 @@ async_job() {
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

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 "_async_eval $cmd"$'\0'
}

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

0 comments on commit c39c0b1

Please sign in to comment.