zsh: agregar scripts

This commit is contained in:
Cat /dev/Nulo 2021-12-22 21:12:11 -03:00
parent 749c1055d1
commit bda49307bc
3 changed files with 67 additions and 2 deletions

45
.config/zsh/bg_notify.zsh Normal file
View file

@ -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

20
.config/zsh/osc_7.zsh Normal file
View file

@ -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

4
.zshrc
View file

@ -41,8 +41,8 @@ alias e="$EDITOR"
alias r="trash -r" alias r="trash -r"
source /usr/share/fzf/key-bindings.zsh source /usr/share/fzf/key-bindings.zsh
source ~/.config/zsh_bg_notify.zsh source ~/.config/zsh/bg_notify.zsh
source ~/.config/zsh_osc_7.zsh source ~/.config/zsh/osc_7.zsh
if test ! "$REMOTE"; then if test ! "$REMOTE"; then
unset SSH_AGENT_PID unset SSH_AGENT_PID