diff --git a/arch/PKGBUILD b/arch/PKGBUILD index a60a851..18cdc92 100644 --- a/arch/PKGBUILD +++ b/arch/PKGBUILD @@ -21,4 +21,6 @@ package() { cd $srcdir/$pkgname install -Dm644 pure.zsh \ "$pkgdir/usr/share/zsh/functions/Prompts/prompt_pure_setup" + install -Dm644 async.zsh \ + "$pkgdir/usr/share/zsh/functions/async" } diff --git a/async.plugin.zsh b/async.plugin.zsh new file mode 120000 index 0000000..27d2d7e --- /dev/null +++ b/async.plugin.zsh @@ -0,0 +1 @@ +async.zsh \ No newline at end of file diff --git a/async.zsh b/async.zsh new file mode 100644 index 0000000..43a7b31 --- /dev/null +++ b/async.zsh @@ -0,0 +1,244 @@ +#!/usr/bin/env zsh + +# +# zsh-async +# +# version: 0.2.1 +# author: Mathias Fredriksson +# url: https://github.com/mafredri/zsh-async +# + +# Wrapper for jobs executed by the async worker, gives output in parseable format with execution time +_async_job() { + # store start time + local start=$EPOCHREALTIME + + # run the command + local out + out=$($* 2>&1) + local ret=$? + + # Grab mutex lock + read -ep >/dev/null + + # return output ( ) + print -r -N -n -- $1 $ret "$out" $(( $EPOCHREALTIME - $start ))$'\0' + + # Unlock mutex + print -p "t" +} + +# The background worker manages all tasks and runs them without interfering with other processes +_async_worker() { + local -A storage + local unique=0 + + # Process option parameters passed to worker + while getopts "np:u" opt; do + case "$opt" in + # Use SIGWINCH since many others seem to cause zsh to freeze, e.g. ALRM, INFO, etc. + n) trap 'kill -WINCH $ASYNC_WORKER_PARENT_PID' CHLD;; + p) ASYNC_WORKER_PARENT_PID=$OPTARG;; + u) unique=1;; + esac + done + + # Create a mutex for writing to the terminal through coproc + coproc cat + # Insert token into coproc + print -p "t" + + while read -r cmd; do + # Separate on spaces into an array + cmd=(${=cmd}) + local job=$cmd[1] + + # Check for non-job commands sent to worker + case "$job" in + _killjobs) + kill ${${(v)jobstates##*:*:}%=*} &>/dev/null + continue + ;; + esac + + # If worker should perform unique jobs + if ((unique)); then + # Check if a previous job is still running, if yes, let it finnish + for pid in ${${(v)jobstates##*:*:}%\=*}; do + if [[ "${storage[$job]}" == "$pid" ]]; then + continue 2 + fi + done + fi + + # run task in background + _async_job $cmd & + # store pid because zsh job manager is extremely unflexible (show jobname as non-unique '$job')... + storage[$job]=$! + 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. +# +# usage: +# async_process_results +# +# callback_function is called with the following parameters: +# $1 = job name, e.g. the function passed to async_job +# $2 = return code +# $3 = resulting output from execution +# $4 = execution time, floating point e.g. 2.05 seconds +# +async_process_results() { + integer count=0 + local -a items + local IFS=$'\0' + + typeset -gA ASYNC_PROCESS_BUFFER + # Read output from zpty and parse it if available + while zpty -rt $1 line 2>/dev/null; do + # Remove unwanted \r from output + ASYNC_PROCESS_BUFFER[$1]+=${line//$'\r'$'\n'/$'\n'} + # Split buffer on null characters, preserve empty elements + items=("${(@)=ASYNC_PROCESS_BUFFER[$1]}") + # Remove last element since it's due to the return string separator structure + items=("${(@)items[1,${#items}-1]}") + + # Continue until we receive all information + (( ${#items} % 4 )) && continue + + # Work through all results + while ((${#items} > 0)); do + eval '$2 "${(@)=items[1,4]}"' + shift 4 items + count+=1 + done + + # Empty the buffer + ASYNC_PROCESS_BUFFER[$1]="" + done + + # If we processed any results, return success + (( $count )) && return 0 + + # No results were processed + return 1 +} + +# +# Start a new asynchronous job on specified worker, assumes the worker is running. +# +# usage: +# async_job [] +# +async_job() { + local worker=$1; shift + zpty -w $worker $* +} + +# This function traps notification signals and calls all registered callbacks +_async_notify_trap() { + for k in ${(k)ASYNC_CALLBACKS}; do + async_process_results "${k}" "${ASYNC_CALLBACKS[$k]}" + done +} + +# +# Register a callback for completed jobs. As soon as a job is finnished, async_process_results will be called with the +# specified callback function. This requires that a worker is initialized with the -n (notify) option. +# +# usage: +# async_register_callback +# +async_register_callback() { + typeset -gA ASYNC_CALLBACKS + + ASYNC_CALLBACKS[$1]="${*[2,${#*}]}" + + trap '_async_notify_trap' WINCH +} + +# +# Unregister the callback for a specific worker. +# +# usage: +# async_unregister_callback +# +async_unregister_callback() { + typeset -gA ASYNC_CALLBACKS + + unset "ASYNC_CALLBACKS[$1]" +} + +# +# Flush all current jobs running on a worker. This will terminate any and all running processes under the worker, use +# with caution. +# +# usage: +# async_flush_jobs +# +async_flush_jobs() { + zpty -t $worker &>/dev/null || return 1 + + # Send kill command to worker + zpty -w $1 "_killjobs" + + # Clear all output buffers + while zpty -r $1 line; do done + + # Clear any partial buffers + typeset -gA ASYNC_PROCESS_BUFFER + ASYNC_PROCESS_BUFFER[$1]="" +} + +# +# Start a new async worker with optional parameters, a worker can be told to only run unique tasks and to notify a +# process when tasks are complete. +# +# usage: +# async_start_worker [-u] [-n] [-p ] +# +# opts: +# -u unique (only unique job names can run) +# -n notify through SIGWINCH signal +# -p pid to notify (defaults to current pid) +# +async_start_worker() { + local worker=$1; shift + zpty -t $worker &>/dev/null || zpty -b $worker _async_worker -p $$ $* || async_stop_worker $worker +} + +# +# Stop one or multiple workers that are running, all unfetched and incomplete work will be lost. +# +# usage: +# async_stop_worker [] +# +async_stop_worker() { + local ret=0 + for worker in $*; do + async_unregister_callback $worker + zpty -d $worker 2>/dev/null || ret=$? + done + + return $ret +} + +# +# Initialize the required modules for zsh-async. To be called before using the zsh-async library. +# +# usage: +# async_init +# +async_init() { + zmodload zsh/zpty + zmodload zsh/datetime +} + +async() { + async_init +} + +async "$*" diff --git a/package.json b/package.json index 9623e25..74b7200 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=0.10.0" }, "scripts": { - "postinstall": "dest=/usr/local/share/zsh/site-functions/; mkdir -p $dest && ln -sf \"$PWD/pure.zsh\" $dest/prompt_pure_setup || echo 'Could not automagically symlink the prompt. Check out the readme on how to do it manually: https://github.com/sindresorhus/pure#manually'" + "postinstall": "dest=/usr/local/share/zsh/site-functions/; mkdir -p $dest && ln -sf \"$PWD/pure.zsh\" $dest/prompt_pure_setup && ln -sf \"$PWD/async.zsh\" $dest/async || echo 'Could not automagically symlink the prompt. Check out the readme on how to do it manually: https://github.com/sindresorhus/pure#manually'" }, "files": [ "pure.zsh" diff --git a/pure.zsh b/pure.zsh index 9f0e821..748b6c5 100644 --- a/pure.zsh +++ b/pure.zsh @@ -15,6 +15,12 @@ # %n => username # %m => shortname host # %(?..) => prompt conditional - %(condition.true.false) +# terminal codes: +# \e7 => save cursor position +# \e[2A => move cursor 2 lines up +# \e[1G => go to position 1 in terminal +# \e8 => restore cursor position +# \e[K => clears everything after the cursor on the current line # turns seconds into human readable time @@ -32,27 +38,27 @@ prompt_pure_human_time() { echo "${seconds}s" } -# fastest possible way to check if repo is dirty -prompt_pure_git_dirty() { - # check if we're in a git repo - [[ "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]] || return - # check if it's dirty - [[ "$PURE_GIT_UNTRACKED_DIRTY" == 0 ]] && local umode="-uno" || local umode="-unormal" - command test -n "$(git status --porcelain --ignore-submodules ${umode})" - - (($? == 0)) && echo '*' -} - # displays the exec time of the last command if set threshold was exceeded -prompt_pure_cmd_exec_time() { +prompt_pure_check_cmd_exec_time() { local stop=$EPOCHSECONDS - local start=${cmd_timestamp:-$stop} + local start=${prompt_pure_cmd_timestamp:-$stop} integer elapsed=$stop-$start (($elapsed > ${PURE_CMD_MAX_EXEC_TIME:=5})) && prompt_pure_human_time $elapsed } +prompt_pure_check_git_arrows() { + # check if there is an upstream configured for this branch + command git rev-parse --abbrev-ref @'{u}' &>/dev/null || return + + local arrows="" + (( $(command git rev-list --right-only --count HEAD...@'{u}' 2>/dev/null) > 0 )) && arrows='⇣' + (( $(command git rev-list --left-only --count HEAD...@'{u}' 2>/dev/null) > 0 )) && arrows+='⇡' + # output the arrows + [[ "$arrows" != "" ]] && echo " ${arrows}" +} + prompt_pure_preexec() { - cmd_timestamp=$EPOCHSECONDS + prompt_pure_cmd_timestamp=$EPOCHSECONDS # shows the current dir and executed command in the title when a process is active print -Pn "\e]0;" @@ -66,38 +72,155 @@ prompt_pure_string_length() { echo $(( ${#${(S%%)1//(\%([KF1]|)\{*\}|\%[Bbkf])}} - 1 )) } +prompt_pure_preprompt_render() { + # check that no command is currently running, the prompt will otherwise be rendered in the wrong place + [[ -n ${prompt_pure_cmd_timestamp+x} && "$1" != "precmd" ]] && return + + # set color for git branch/dirty status, change color if dirty checking has been delayed + local git_color=242 + [[ -n ${prompt_pure_git_delay_dirty_check+x} ]] && git_color=red + + # construct prompt, beginning with path + local prompt="%F{blue}%~%f" + # git info + prompt+="%F{$git_color}${vcs_info_msg_0_}${prompt_pure_git_dirty}%f" + # git pull/push arrows + prompt+="%F{cyan}${prompt_pure_git_arrows}%f" + # username and machine if applicable + prompt+=$prompt_pure_username + # execution time + prompt+="%F{yellow}${prompt_pure_cmd_exec_time}%f" + + # if executing through precmd, do not perform fancy terminal editing + if [[ "$1" == "precmd" ]]; then + print -P "\n${prompt}" + else + # only redraw if prompt has changed + [[ "${prompt_pure_last_preprompt}" != "${prompt}" ]] || return + + # calculate length of prompt for redraw purposes + local prompt_length=$(prompt_pure_string_length $prompt) + local lines=$(( $prompt_length / $COLUMNS + 1 )) + + # disable clearing of line if last char of prompt is last column of terminal + local clr="\e[K" + (( $prompt_length * $lines == $COLUMNS - 1 )) && clr="" + + # modify previous prompt + print -Pn "\e7\e[${lines}A\e[1G${prompt}${clr}\e8" + fi + + # store previous prompt for comparison + prompt_pure_last_preprompt=$prompt +} + prompt_pure_precmd() { + # store exec time for when preprompt gets re-rendered + prompt_pure_cmd_exec_time=$(prompt_pure_check_cmd_exec_time) + + # by making sure that prompt_pure_cmd_timestamp is defined here the async functions are prevented from interfering + # with the initial preprompt rendering + prompt_pure_cmd_timestamp= + + # check for git arrows + prompt_pure_git_arrows=$(prompt_pure_check_git_arrows) + # shows the full path in the title print -Pn '\e]0;%~\a' - # git info + # get vcs info vcs_info - local prompt_pure_preprompt="\n%F{blue}%~%F{242}$vcs_info_msg_0_`prompt_pure_git_dirty`$prompt_pure_username%f%F{yellow}`prompt_pure_cmd_exec_time`%f" - print -P $prompt_pure_preprompt + # preform async git dirty check and fetch + prompt_pure_async_tasks - # check async if there is anything to pull - (( ${PURE_GIT_PULL:-1} )) && { - # check if we're in a git repo - [[ "$(command git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]] && - # make sure working tree is not $HOME - [[ "$(command git rev-parse --show-toplevel)" != "$HOME" ]] && - # check check if there is anything to pull - # set GIT_TERMINAL_PROMPT=0 to disable auth prompting for git fetch (git 2.3+) - GIT_TERMINAL_PROMPT=0 command git -c gc.auto=0 fetch &>/dev/null && - # check if there is an upstream configured for this branch - command git rev-parse --abbrev-ref @'{u}' &>/dev/null && { - local arrows='' - (( $(command git rev-list --right-only --count HEAD...@'{u}' 2>/dev/null) > 0 )) && arrows='⇣' - (( $(command git rev-list --left-only --count HEAD...@'{u}' 2>/dev/null) > 0 )) && arrows+='⇡' - print -Pn "\e7\e[A\e[1G\e[`prompt_pure_string_length $prompt_pure_preprompt`C%F{cyan}${arrows}%f\e8" - } - } &! + # print the preprompt + prompt_pure_preprompt_render "precmd" - # reset value since `preexec` isn't always triggered - unset cmd_timestamp + # remove the prompt_pure_cmd_timestamp, indicating that precmd has completed + unset prompt_pure_cmd_timestamp } +# fastest possible way to check if repo is dirty +prompt_pure_async_git_dirty() { + local untracked_dirty=$2 + local umode="-unormal" + [[ "$untracked_dirty" == "0" ]] && umode="-uno" + + cd "$1" + command test -n "$(git status --porcelain --ignore-submodules ${umode})" + (($? == 0)) && echo "*" +} + +prompt_pure_async_git_fetch() { + cd "$1" + + # set GIT_TERMINAL_PROMPT=0 to disable auth prompting for git fetch (git 2.3+) + GIT_TERMINAL_PROMPT=0 command git -c gc.auto=0 fetch +} + +prompt_pure_async_tasks() { + # initialize async worker + ((!${prompt_pure_async_init:-0})) && { + async_start_worker "prompt_pure" -u -n + async_register_callback "prompt_pure" prompt_pure_async_callback + prompt_pure_async_init=1 + } + + # get the current git working tree, empty if not inside a git directory + local working_tree="$(command git rev-parse --show-toplevel 2>/dev/null)" + + # check if the working tree changed, it is prefixed with "x" to prevent variable resolution in path + if [ "${prompt_pure_current_working_tree:-x}" != "x${working_tree}" ]; then + # stop any running async jobs + async_flush_jobs "prompt_pure" + + # reset git preprompt variables, switching working tree + unset prompt_pure_git_dirty + unset prompt_pure_git_delay_dirty_check + + # set the new working tree, prefixed with "x" + prompt_pure_current_working_tree="x${working_tree}" + fi + + # only perform tasks inside git working tree + [[ "${working_tree}" != "" ]] || return + + # tell worker to do a git fetch + async_job "prompt_pure" prompt_pure_async_git_fetch $working_tree + + # if dirty checking is sufficiently fast, tell worker to check it again, or wait for timeout + local dirty_check=$(( $EPOCHSECONDS - ${prompt_pure_git_delay_dirty_check:-0} )) + if (( $dirty_check > ${PURE_GIT_DELAY_DIRTY_CHECK:-1800} )); then + unset prompt_pure_git_delay_dirty_check + (( ${PURE_GIT_PULL:-1} )) && + # make sure working tree is not $HOME + [[ "${working_tree}" != "$HOME" ]] && + # check check if there is anything to pull + async_job "prompt_pure" prompt_pure_async_git_dirty $working_tree $PURE_GIT_UNTRACKED_DIRTY + fi +} + +prompt_pure_async_callback() { + local job=$1 + local output=$3 + local exec_time=$4 + + case "${job}" in + prompt_pure_async_git_dirty) + prompt_pure_git_dirty=$output + prompt_pure_preprompt_render + + # when prompt_pure_git_delay_dirty_check is set, the git info is displayed in a different color, this is why the + # prompt is rendered before the variable is (potentially) set + (( $exec_time > 2 )) && prompt_pure_git_delay_dirty_check=$EPOCHSECONDS + ;; + prompt_pure_async_git_fetch) + prompt_pure_git_arrows=$(prompt_pure_check_git_arrows) + prompt_pure_preprompt_render + ;; + esac +} prompt_pure_setup() { # prevent percentage showing up @@ -109,6 +232,7 @@ prompt_pure_setup() { zmodload zsh/datetime autoload -Uz add-zsh-hook autoload -Uz vcs_info + autoload -Uz async && async add-zsh-hook precmd prompt_pure_precmd add-zsh-hook preexec prompt_pure_preexec @@ -119,10 +243,10 @@ prompt_pure_setup() { zstyle ':vcs_info:git*' actionformats ' %b|%a' # show username@host if logged in through SSH - [[ "$SSH_CONNECTION" != '' ]] && prompt_pure_username=' %n@%m' + [[ "$SSH_CONNECTION" != '' ]] && prompt_pure_username=' %F{242}%n@%m%f' # show username@host if root, with username in white - [[ $UID -eq 0 ]] && prompt_pure_username=' %F{white}%n%F{242}@%m' + [[ $UID -eq 0 ]] && prompt_pure_username=' %F{white}%n%f%F{242}@%m%f' # prompt turns red if the previous command didn't exit with 0 PROMPT="%(?.%F{magenta}.%F{red})${PURE_PROMPT_SYMBOL:-❯}%f " diff --git a/readme.md b/readme.md index 339f70b..b969ca9 100644 --- a/readme.md +++ b/readme.md @@ -43,10 +43,13 @@ That's it. Skip to [Getting started](#getting-started). 2. Symlink `pure.zsh` to somewhere in [`$fpath`](http://www.refining-linux.org/archives/46/ZSH-Gem-12-Autoloading-functions/) with the name `prompt_pure_setup`. +3. Symlink `async.zsh` in `$fpath` with the name `async`. + #### Example ```sh $ ln -s "$PWD/pure.zsh" /usr/local/share/zsh/site-functions/prompt_pure_setup +$ ln -s "$PWD/async.zsh" /usr/local/share/zsh/site-functions/async ``` *Run `echo $fpath` to see possible locations.* @@ -61,6 +64,7 @@ Then install the theme there: ```sh $ ln -s "$PWD/pure.zsh" "$HOME/.zfunctions/prompt_pure_setup" +$ ln -s "$PWD/async.zsh" "$HOME/.zfunctions/async" ``` @@ -89,6 +93,10 @@ Set `PURE_GIT_PULL=0` to prevent Pure from checking whether the current Git remo Set `PURE_GIT_UNTRACKED_DIRTY=0` to not include untracked files in dirtiness check. Only really useful on extremely huge repos like the WebKit repo. +### `PURE_GIT_DELAY_DIRTY_CHECK` + +Time in seconds to delay git dirty checking for large repositories (git status takes > 2 seconds). The check is performed asynchronously, this is to save CPU. Defaults to `1800` seconds. + ### `PURE_PROMPT_SYMBOL` Defines the prompt symbol. The default value is `❯`.