Skip to content

Instantly share code, notes, and snippets.

@hryk
Last active December 1, 2016 09:38
Show Gist options
  • Select an option

  • Save hryk/1627790 to your computer and use it in GitHub Desktop.

Select an option

Save hryk/1627790 to your computer and use it in GitHub Desktop.
notification plugin for oh-my-zsh.
# oh-my-zsh! notification plugin
#
# DESCRIPTION
#
# INSTALL
#
# Place this file to $HOME/.oh-my-zsh/plugins/notification/notification.plugin.zsh
#
# USAGE
#
# # ~/.zshrc
#
# plugins=(notification)
#
# znotify[ignore]=(vim emacs man less ssh)
# znotify[on_complete]="growlnotify -t $command_name -m 'elapsed $formatted_elapsed_time.'"
# znotify[threshold]="30" # sec
#
# TODO
#
# * Use znotify[_default_ignore] and znotify[ignore].
# * Remove 'bundle exec' from znotify[_command]
#
autoload -Uz add-zsh-hook
# Defaults
typeset -Ag znotify
typeset "znotify[on_complete]"='growlnotify -t $command_name -m "$command_name is completed within $elapsed_time sec."'
typeset "znotify[ignore]"="vim ssh man less top builtin"
typeset "znotify[threshold]"='30'
typeset "znotify[_default_ignore]"="vim ssh man less top emacs builtin tail"
typeset "znotify[_command]"='0'
typeset "znotify[_command_start]"='0'
typeset "znotify[_disabled]"='0'
typeset "znotify[_strftime_available]"='0'
## Functions
__set_notification(){
emulate -L zsh
typeset -a cmd;cmd=(${(z)2})
znotify[_command_start]=$(date +%s)
case $cmd[1] in
fg)
if (( $#cmd == 1 )); then
cmd=(builtin jobs -l %+)
else
cmd=(builtin jobs -l $cmd[2])
fi
;;
%*)
cmd=(builtin jobs -l $cmd[1])
;;
cd)
if (( $#cmd == 2)); then
cmd[1]=$cmd[2]
fi
;;
esac
for ignore in ${(z)znotify[ignore]}; do
if [[ ${cmd[1]} == $ignore ]] {
znotify[_disabled]='1'
break
}
done
znotify[_command]=${cmd[1]}
}
__exec_notification(){
if [[ ${znotify[_disabled]} -eq '0' && ${znotify[_command_start]} != '0' ]] {
integer stopped_time=$(date +%s);
integer elapsed_time;
(( elapsed_time = $stopped_time - ${znotify[_command_start]} ))
if [[ $elapsed_time -ge ${znotify[threshold]} ]] {
local command_name;
local formatted_elapsed_time;
if [[ ${znotify[_strftime_available]} -eq '1' ]] {
if [[ $elapsed_time -ge '3600' ]] {
formatted_elapsed_time=$(strftime %H:%M:%S $elapsed_time)
} elif [[ $elapsed_time -ge '60' ]] {
formatted_elapsed_time=$(strftime %M:%S $elapsed_time)
} else {
formatted_elapsed_time=$elapsed_time
}
} else {
formatted_elapsed_time=$elapsed_time
}
command_name=${znotify[_command]}
eval ${znotify[on_complete]}
}
}
znotify[_command]='0'
znotify[_command_start]='0'
znotify[_disabled]='0'
}
__znotify_init(){
# Check zsh/strftime
if [[ $(zmodload zsh/strftime 2>/dev/null) -eq '0' ]] {
znotify[_strftime_available]='1'
} else {
znotify[_strftime_available]='0'
}
# Setup hooks
if (($+functions[add-zsh-hook])); then
add-zsh-hook preexec __set_notification
add-zsh-hook precmd __exec_notification
else
echo '"add-zsh-hook" does not exists. Update zsh to 4.3.4 or later.'
fi
}
## Main program
__znotify_init
@xiorcal
Copy link
Copy Markdown

xiorcal commented Dec 1, 2016

to use xfce notification use
znotify[on_complete]='notify-send "${command_name} ended" "elapsed $formatted_elapsed_time." --icon=dialog-information'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment