1
0
Fork 0

Add changes from sorin codereview

-    autoload functions aren't surrounded in an addition function { }
-    export cross platform notify function
-    control notify behavior with zstyles
pull/443/head
Alex Reece 12 years ago
parent f99f480eee
commit 36559d9978

@ -6,21 +6,19 @@
# #
# Converts a floating point time duration in seconds to a human readable string. # Converts a floating point time duration in seconds to a human readable string.
function time_to_human { seconds=$1
seconds=$1 if (( seconds < 10 )); then
if (( seconds < 10 )); then
printf "%6.3fs" $seconds printf "%6.3fs" $seconds
elif (( seconds < 60 )); then elif (( seconds < 60 )); then
printf "%6.3fs" $seconds printf "%6.3fs" $seconds
elif (( seconds < (60*60) )); then elif (( seconds < (60*60) )); then
printf "%6.3fm" $(( seconds / 60 )) printf "%6.3fm" $(( seconds / 60 ))
elif (( seconds < (60*60*24) )); then elif (( seconds < (60*60*24) )); then
printf "%6.3fh" $(( seconds / (60*60) )) printf "%6.3fh" $(( seconds / (60*60) ))
elif (( seconds < (60*60*24*30) )); then elif (( seconds < (60*60*24*30) )); then
printf "%6.3fd" $(( seconds / (60*60*24) )) printf "%6.3fd" $(( seconds / (60*60*24) ))
elif (( seconds < (60*60*24*30*12) )); then elif (( seconds < (60*60*24*30*12) )); then
printf "%6.3fm" $(( seconds / (60*60*24*30) )) printf "%6.3fm" $(( seconds / (60*60*24*30) ))
else else
printf "%6.3fy" $(( seconds / (60*60*24*30*12) )) printf "%6.3fy" $(( seconds / (60*60*24*30*12) ))
fi fi
}

@ -1,20 +1,21 @@
# #
# Exports information about the last command. # Provides information about the last command.
# #
# Authors: # Authors:
# Alex Reece <awreece@gmail.com> # Alex Reece <awreece@gmail.com>
# #
# Exported values. # Provided values.
last_command='' last_command=''
last_command_status=0 last_command_status=0
last_command_time=0.0 last_command_time=0.0
# Not exported.
last_command_start_time=invalid
zmodload zsh/datetime # For EPOCHREALTIME. zmodload zsh/datetime # For EPOCHREALTIME.
# This value is used internally by this module and is not intended to be used
# elsewhere.
last_command_start_time='invalid'
function last_command_precmd { function last_command_precmd {
exit_status=$? # TODO(awreece) What happens if another precmd runs first? exit_status=$? # TODO(awreece) What happens if another precmd runs first?

@ -16,6 +16,8 @@ if (( ! $+commands[node] )); then
return 1 return 1
fi fi
pmodload 'helper'
# Load NPM completion. # Load NPM completion.
if (( $+commands[npm] )); then if (( $+commands[npm] )); then
cache_file="${0:h}/cache.zsh" cache_file="${0:h}/cache.zsh"

@ -3,10 +3,28 @@ Last Command
Notifies if the command completes and terminal isn't the foreground window. Notifies if the command completes and terminal isn't the foreground window.
Caveats Settings
------- --------
Currently only works on Mac OSX with `terminal-notifier`. At some point, I
will add support for other notification mechanisms. ### Growl
By default, `notify` will use the Notification Center on Mac OSX via
`terminal-notifier` and will fall back to Growl if this is not available. To
force the use of growl, add the following to *zpreztorc*.
zstyle ':prezto:module:notify' force-growl 'yes'
### Auto-notify if window not focused
This module can automatically notify if a (long running) command completes
while the terminal window does not have focus. This will pop up a notification
with information about the last command like this:
![Last command notification](http://codearcana.com/images/zsh_theme_popup.png)
To enable this feature, add the following to *zpreztorc*:
zstyle ':prezto:module:notify' auto-notify 'yes'
Authors Authors
------- -------

@ -4,8 +4,7 @@
# Alex Reece <awreece@gmail.com> # Alex Reece <awreece@gmail.com>
# #
function focused_window_id { case "$OSTYPE" in
case "$OSTYPE" in
(darwin*) (darwin*)
osascript -e 'tell application "System Events" to ¬' \ osascript -e 'tell application "System Events" to ¬' \
-e ' set focus_app_name to ¬' \ -e ' set focus_app_name to ¬' \
@ -22,5 +21,4 @@ function focused_window_id {
(*) (*)
return 1 return 1
;; ;;
esac esac
}

@ -0,0 +1,27 @@
# Pops up a notification with the provided arg.
#
# Authors:
# Alex Reece <awreece@gmail.com>
#
# I actually want all the args as one string.
message="$*"
case "$OSTYPE" in
(darwin*)
if is-callable terminal-notifier && \
! zstyle -t ':prezto:module:notify' force-growl; then
terminal-notifier >/dev/null -message $message
elif is-callable growlnotify; then
growlnotify --message $message
else
return 1
fi
;;
(linux-gnu*)
notify-send $message
;;
(*)
return 1
;;
esac

@ -0,0 +1,40 @@
# Sends a notification that the last command completed.
#
# Assumes $terminal_window_id is the id of the terminals window and that
# last_comand module is loaded.
#
# Authors:
# Alex Reece <awreece@gmail.com>
#
message=$(printf "Command \"%s\" finished (%d) after %s." \
$last_command \
$last_command_status \
$(time_to_human $last_command_time))
# We duplicate a lot of the functionality of notify, but it means we can
# have this nifty callback to select the window that finished.
# TODO(awreece) Add support for user defined callback.
case "$OSTYPE" in
(darwin*)
if is-callable terminal-notifier &&
! zstyle -t ':prezto:module:notify' force-growl; 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
elif is-callable growlnotify; then
growlnotify --message $message
else
return 1
fi
;;
(linux-gnu*)
notify-send "Command finished" $message
;;
(*)
return 1
;;
esac

@ -0,0 +1,35 @@
# Check if it makes sense to load the notify module.
#
# Checks for requirements, that it is an xsession, etc.
#
# Authors:
# Alex Reece <awreece@gmail.com>
#
if ! zstyle -t ':prezto:module:notify' auto-notify; then
return 1
fi
case "$OSTYPE" in
(darwin*)
# TODO(awreece) Disable if ssh with no windows?
is-callable terminal-notifier || is-callable growlnotify
;;
(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

@ -1,34 +0,0 @@
# Check if it makes sense to load the notify module.
#
# Checks for requirements, that it is an xsession, etc.
#
# Authors:
# Alex Reece <awreece@gmail.com>
#
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
}

@ -5,54 +5,21 @@
# Alex Reece <awreece@gmail.com> # Alex Reece <awreece@gmail.com>
# #
pmodload 'last_command' pmodload 'helper'
if ! should_load_notify_module; then if should_auto_notify; then
return 1 pmodload 'last_command'
fi
# Initialize $terminal_window_id to the current active window.
terminal_window_id=$(focused_window_id)
# Returns true if the current window has focus.
#
# TODO(awreece) Add support for tabs.
function window_is_focused {
[[ $(focused_window_id) == $terminal_window_id ]]
}
# Sends a notification that the last command terminated.
# Warning: currently only implemented for mac.
function last_command_notify {
message=$(printf "Command \"%s\" finished (%d) after %s." \
$last_command \
$last_command_status \
$(time_to_human $last_command_time))
# TODO(awreece) Add support for user defined callback. # Initialize $terminal_window_id to the current active window.
case "$OSTYPE" in terminal_window_id=$(focused_window_id)
(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 { function notify_precmd {
if ! window_is_focused; then if [[ $(focused_window_id) != $terminal_window_id ]]; then
last_command_notify notify_last_command
fi fi
} }
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
add-zsh-hook precmd notify_precmd add-zsh-hook precmd notify_precmd
fi

Loading…
Cancel
Save