diff --git a/.config/zsh/bg_notify.zsh b/.config/zsh/bg_notify.zsh new file mode 100644 index 0000000..41d22ae --- /dev/null +++ b/.config/zsh/bg_notify.zsh @@ -0,0 +1,45 @@ +#!/usr/bin/env zsh +# Based on https://github.com/t413/zsh-background-notify/blob/master/bgnotify.plugin.zsh + +[[ -o interactive ]] || return +zmodload zsh/datetime || { print "can't load zsh/datetime"; return } # faster than date() +autoload -Uz add-zsh-hook || { print "can't add zsh hook!"; return } + +(( ${+bgnotify_threshold} )) || bgnotify_threshold=5 # default 5 seconds + +if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom function override + function bgnotify_formatted { ## args: (exit_status, command, elapsed_seconds) + elapsed="$(( $3 % 60 ))s" + (( $3 >= 60 )) && elapsed="$((( $3 % 3600) / 60 ))m $elapsed" + (( $3 >= 3600 )) && elapsed="$(( $3 / 3600 ))h $elapsed" + [ $1 -eq 0 ] && bgnotify "success (took $elapsed)" "$2" || bgnotify "fail (took $elapsed)" "$2" + } +fi + +bgnotify () { ## args: (title, subtitle) + echo -e "\033]777;notify;$1;$2\a" +} + +## ZSH hooks ## + +bgnotify_begin() { + bgnotify_timestamp=$EPOCHSECONDS + bgnotify_lastcmd="$1" +} + +bgnotify_end() { + didexit=$? + elapsed=$(( EPOCHSECONDS - bgnotify_timestamp )) + past_threshold=$(( elapsed >= bgnotify_threshold )) + if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then + print -n "\a" + bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed" + fi + bgnotify_timestamp=0 +} + +## only enable if a local (non-ssh) connection +if [ -z "$SSH_CLIENT" ] && [ -z "$SSH_TTY" ]; then + add-zsh-hook preexec bgnotify_begin + add-zsh-hook precmd bgnotify_end +fi diff --git a/.config/zsh/osc_7.zsh b/.config/zsh/osc_7.zsh new file mode 100644 index 0000000..0b6e182 --- /dev/null +++ b/.config/zsh/osc_7.zsh @@ -0,0 +1,20 @@ +#!/usr/bin/env zsh +# https://codeberg.org/dnkl/foot/wiki#user-content-bash-and-zsh + +_urlencode() { + local length="${#1}" + for (( i = 0; i < length; i++ )); do + local c="${1:$i:1}" + case $c in + %) printf '%%%02X' "'$c" ;; + *) printf "%s" "$c" ;; + esac + done +} + +osc7_cwd() { + printf '\e]7;file://%s%s\e\\' "$HOSTNAME" "$(_urlencode "$PWD")" +} + +autoload -Uz add-zsh-hook +add-zsh-hook -Uz chpwd osc7_cwd diff --git a/.zshrc b/.zshrc index 9ab4934..e731572 100644 --- a/.zshrc +++ b/.zshrc @@ -41,8 +41,8 @@ alias e="$EDITOR" alias r="trash -r" source /usr/share/fzf/key-bindings.zsh -source ~/.config/zsh_bg_notify.zsh -source ~/.config/zsh_osc_7.zsh +source ~/.config/zsh/bg_notify.zsh +source ~/.config/zsh/osc_7.zsh if test ! "$REMOTE"; then unset SSH_AGENT_PID