Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
oh-my-zsh
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
github
oh-my-zsh
Commits
a04cf078
Commit
a04cf078
authored
Oct 14, 2022
by
Marc Cornellà
Committed by
Carlo
Nov 11, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(bgnotify): clean up and reorganize code
🗸 Standardize code style 🗸 Organize code sections 🗸 Optimize calls for window ID
parent
5b2d0a3f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
53 deletions
+68
-53
bgnotify.plugin.zsh
plugins/bgnotify/bgnotify.plugin.zsh
+68
-53
No files found.
plugins/bgnotify/bgnotify.plugin.zsh
View file @
a04cf078
#!/usr/bin/env zsh
#!/usr/bin/env zsh
##
setup ##
##
Setup
[[
-o
interactive
]]
||
return
#interactive only!
[[
-o
interactive
]]
||
return
# don't load on non-interactive shells
zmodload zsh/datetime
||
{
print
"can't load zsh/datetime"
;
return
}
# faster than date()
[[
-z
"
$SSH_CLIENT
"
&&
-z
"
$SSH_TTY
"
]]
||
return
# don't load on a SSH connection
autoload
-Uz
add-zsh-hook
||
{
print
"can't add zsh hook!"
;
return
}
((
${
+bgnotify_threshold
}
))
||
bgnotify_threshold
=
5
#default 10 seconds
zmodload zsh/datetime
# faster than `date`
##
definitions ##
##
Zsh Hooks
if
!
(
type
bgnotify_formatted |
grep
-q
'function'
)
;
then
## allow custom function override
function
bgnotify_begin
{
function
bgnotify_formatted
{
## args: (exit_status, command, elapsed_seconds)
bgnotify_timestamp
=
$EPOCHSECONDS
elapsed
=
"
$((
$3
%
60
))
s"
bgnotify_lastcmd
=
"
${
1
:-
$2
}
"
((
$3
>=
60
))
&&
elapsed
=
"
$((
(
$3
%
3600
)
/
60
))
m
$elapsed
"
}
((
$3
>=
3600
))
&&
elapsed
=
"
$((
$3
/
3600
))
h
$elapsed
"
[
$1
-eq
0
]
&&
bgnotify
"#win (took
$elapsed
)"
"
$2
"
||
bgnotify
"#fail (took
$elapsed
)"
"
$2
"
function
bgnotify_end
{
{
local
exit_status
=
$?
local
elapsed
=
$((
EPOCHSECONDS
-
bgnotify_timestamp
))
# check time elapsed
[[
$bgnotify_timestamp
-gt
0
]]
||
return
[[
$elapsed
-ge
$bgnotify_threshold
]]
||
return
# check if Terminal app is not active
[[
$(
bgnotify_appid
)
!=
"
$bgnotify_termid
"
]]
||
return
printf
'\a'
# beep sound
bgnotify_formatted
"
$exit_status
"
"
$bgnotify_lastcmd
"
"
$elapsed
"
}
always
{
bgnotify_timestamp
=
0
}
}
fi
}
autoload
-Uz
add-zsh-hook
add-zsh-hook preexec bgnotify_begin
add-zsh-hook precmd bgnotify_end
## Functions
# allow custom function override
((
${
+functions[bgnotify_formatted]
}
))
||
\
function
bgnotify_formatted
{
local
exit_status
=
$1
local
cmd
=
"
$2
"
# humanly readable elapsed time
local
elapsed
=
"
$((
$3
%
60
))
s"
((
$3
< 60
))
||
elapsed
=
"
$((
(
$3
%
3600
)
/
60
))
m
$elapsed
"
((
$3
< 3600
))
||
elapsed
=
"
$((
$3
/
3600
))
h
$elapsed
"
if
[[
$1
-eq
0
]]
;
then
bgnotify
"#win (took
$elapsed
)"
"
$2
"
else
bgnotify
"#fail (took
$elapsed
)"
"
$2
"
fi
}
}
function
currentAppId
{
# for macOS, output is "app ID, window ID" (com.googlecode.iterm2, 116)
function
bgnotify_appid
{
if
((
${
+commands[osascript]
}
))
;
then
if
((
${
+commands[osascript]
}
))
;
then
# output: com.googlecode.iterm2, 116
osascript
-e
'tell application (path to frontmost application as text) to get the {id, id of front window}'
2>/dev/null
osascript
-e
'tell application (path to frontmost application as text) to get the {id, id of front window}'
2>/dev/null
elif
((
${
+commands[
notify-send]
}
||
${
+commands[kdialog
]
}
))
;
then
elif
((
${
+commands[
xprop
]
}
))
;
then
xprop
-root
2> /dev/null |
awk
'/NET_ACTIVE_WINDOW/{print $5;exit} END{exit !$5}'
||
echo
"0"
xprop
-root
_NET_ACTIVE_WINDOW 2>/dev/null |
cut
-d
' '
-f5
else
else
echo
$EPOCHSECONDS
echo
$EPOCHSECONDS
fi
fi
}
}
# currentAppId is expensive (more on macOS!) and it will remain the same until the shell is close
function
bgnotify
{
bgnotify_termid
=
$(
currentAppId
)
# $1: title, $2: message
if
((
${
+commands[terminal-notifier]
}
))
;
then
# macOS
bgnotify
()
{
## args: (title, subtitle)
if
hash
terminal-notifier 2>/dev/null
;
then
#osx
local
term_id
=
"
${
bgnotify_termid
%%,*
}
"
# remove window id
local
term_id
=
"
${
bgnotify_termid
%%,*
}
"
# remove window id
if
[[
-z
"
$term_id
"
]]
;
then
if
[[
-z
"
$term_id
"
]]
;
then
case
"
$TERM_PROGRAM
"
in
case
"
$TERM_PROGRAM
"
in
...
@@ -46,46 +81,26 @@ bgnotify () { ## args: (title, subtitle)
...
@@ -46,46 +81,26 @@ bgnotify () { ## args: (title, subtitle)
esac
esac
fi
fi
## now call terminal-notifier, (hopefully with $term_id!)
if
[[
-z
"
$term_id
"
]]
;
then
if
[[
-z
"
$term_id
"
]]
;
then
terminal-notifier
-message
"
$2
"
-title
"
$1
"
>
/dev/null
terminal-notifier
-message
"
$2
"
-title
"
$1
"
&
>/dev/null
else
else
terminal-notifier
-message
"
$2
"
-title
"
$1
"
-activate
"
$term_id
"
-sender
"
$term_id
"
>
/dev/null
terminal-notifier
-message
"
$2
"
-title
"
$1
"
-activate
"
$term_id
"
-sender
"
$term_id
"
&
>/dev/null
fi
fi
elif
hash
growlnotify 2>/dev/null
;
then
#osx
growl
elif
((
${
+commands[growlnotify]
}
))
;
then
# macOS
growl
growlnotify
-m
"
$1
"
"
$2
"
growlnotify
-m
"
$1
"
"
$2
"
elif
hash
notify-send 2>/dev/null
;
then
#ubuntu gnome!
elif
((
${
+commands[notify-send]
}
))
;
then
# GNOME
notify-send
"
$1
"
"
$2
"
notify-send
"
$1
"
"
$2
"
elif
hash
kdialog 2>/dev/null
;
then
#ubuntu kde!
elif
((
${
+commands[kdialog]
}
))
;
then
# KDE
kdialog
--title
"
$1
"
--passivepopup
"
$2
"
5
kdialog
--title
"
$1
"
--passivepopup
"
$2
"
5
elif
hash
notifu 2>/dev/null
;
then
#cygwyn support!
elif
((
${
+commands[notifu]
}
))
;
then
# cygwin
notifu /m
"
$2
"
/p
"
$1
"
notifu /m
"
$2
"
/p
"
$1
"
fi
fi
}
}
## Defaults
## Zsh hooks ##
# notify if command took longer than 5s by default
bgnotify_threshold
=
${
bgnotify_threshold
:-
5
}
bgnotify_begin
()
{
bgnotify_timestamp
=
$EPOCHSECONDS
bgnotify_lastcmd
=
"
${
1
:-
$2
}
"
}
bgnotify_end
()
{
didexit
=
$?
elapsed
=
$((
EPOCHSECONDS
-
bgnotify_timestamp
))
past_threshold
=
$((
elapsed
>=
bgnotify_threshold
))
if
((
bgnotify_timestamp
>
0
))
&&
((
past_threshold
))
;
then
if
[[
$(
currentAppId
)
!=
"
$bgnotify_termid
"
]]
;
then
print
-n
"
\a
"
bgnotify_formatted
"
$didexit
"
"
$bgnotify_lastcmd
"
"
$elapsed
"
fi
fi
bgnotify_timestamp
=
0
#reset it to 0!
}
## only enable if a local (non-ssh) connection
# bgnotify_appid is slow in macOS and the terminal ID won't change, so cache it at startup
if
[
-z
"
$SSH_CLIENT
"
]
&&
[
-z
"
$SSH_TTY
"
]
;
then
bgnotify_termid
=
"
$(
bgnotify_appid
)
"
add-zsh-hook preexec bgnotify_begin
add-zsh-hook precmd bgnotify_end
fi
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment