diff --git a/modules/notify/functions/focused_window_id b/modules/notify/functions/focused_window_id new file mode 100644 index 00000000..afdb850e --- /dev/null +++ b/modules/notify/functions/focused_window_id @@ -0,0 +1,26 @@ +# Gets the id of the current focused window. +# +# Authors: +# Alex Reece +# + +function focused_window_id { + case "$OSTYPE" in + (darwin*) + osascript -e 'tell application "System Events" to ¬' \ + -e ' set focus_app_name to ¬' \ + -e ' name of first application process ¬' \ + -e ' whose frontmost is true' \ + -e 'tell application focus_app_name to ¬' \ + -e ' get id of front window' + ;; + (linux-gnu*) + # http://stackoverflow.com/a/8688624/447288 + xprop -root -f _NET_ACTIVE_WINDOW 0x ' $0' _NET_ACTIVE_WINDOW \ + | awk '{print $2}' + ;; + (*) + return 1 + ;; + esac +} diff --git a/modules/notify/functions/should_load_notify_module b/modules/notify/functions/should_load_notify_module new file mode 100644 index 00000000..93a2359f --- /dev/null +++ b/modules/notify/functions/should_load_notify_module @@ -0,0 +1,34 @@ +# Check if it makes sense to load the notify module. +# +# Checks for requirements, that it is an xsession, etc. +# +# Authors: +# Alex Reece +# + +function should_load_notify_module { + case "$OSTYPE" in + (darwin*) + # For now, always enable on mac osx. + # TODO(awreece) Disable if ssh with no windows? + return 0 + ;; + (linux-gnu*) + # Disable if don't have X. + if [[ -z $XAUTHORITY ]]; then + return 1 + fi + + # We need both of these functions to operate. + if (( ! $+commands[notify-send] || ! $+commands[xprop] )); then + return 1 + fi + + return 0 + ;; + (*) + # If we don't know, then just disable. + return 1 + ;; + esac +} diff --git a/modules/notify/init.zsh b/modules/notify/init.zsh index 58415564..b5cab920 100644 --- a/modules/notify/init.zsh +++ b/modules/notify/init.zsh @@ -7,28 +7,18 @@ pmodload 'last_command' -if [[ "$OSTYPE" == darwin* ]]; then - terminal_window_id=$(osascript -e 'tell application "Terminal" to ¬' \ - -e ' get id of front window') +if ! should_load_notify_module; then + return 1 fi +# Initialize $terminal_window_id to the current active window. +terminal_window_id=$(focused_window_id) + # Returns true if the current window has focus. -# Warning: Currently only implementd on mac. -# Assume $terminal_window_id is the osascript id of the current window. # # TODO(awreece) Add support for tabs. function window_is_focused { - if [[ "$OSTYPE" == darwin* ]]; then - focus_window_id=$(osascript -e 'tell application "System Events" to ¬' \ - -e ' set focus_app_name to ¬' \ - -e ' name of first application process ¬' \ - -e ' whose frontmost is true' \ - -e 'tell application focus_app_name to ¬' \ - -e ' get id of front window') - fi - # On a not mac, this will always return true since focus_id and - # terminal_window_id are both undefined so empty strings. - [[ $focus_window_id == $terminal_window_id ]] + [[ $(focused_window_id) == $terminal_window_id ]] } # Sends a notification that the last command terminated. @@ -40,13 +30,21 @@ function last_command_notify { $(time_to_human $last_command_time)) # TODO(awreece) Add support for user defined callback. - if [[ "$OSTYPE" == darwin* ]]; then - callback="osascript -e 'tell application \"Terminal\"' \ - -e 'activate' \ - -e 'set index of window id $terminal_window_id to 1' \ - -e 'end tell'" - terminal-notifier -message $message -execute $callback >/dev/null - fi + case "$OSTYPE" in + (darwin*) + callback="osascript -e 'tell application \"Terminal\"' \ + -e 'activate' \ + -e 'set index of window id $terminal_window_id to 1' \ + -e 'end tell'" + terminal-notifier -message $message -execute $callback >/dev/null + ;; + (linux-gnu*) + notify-send "Command finished" $message + ;; + (*) + return 1 + ;; + esac } function notify_precmd {