refactor into modules
This commit is contained in:
parent
171b41d3c4
commit
856b3ff614
6 changed files with 178 additions and 94 deletions
29
modules/last_command/README.md
Normal file
29
modules/last_command/README.md
Normal file
|
@ -0,0 +1,29 @@
|
|||
Last Command
|
||||
============
|
||||
|
||||
Provides information about the last command run.
|
||||
|
||||
Exported variables
|
||||
------------------
|
||||
|
||||
- `last_command`: The string of the last command.
|
||||
- `last_command_time`: The execution time (in floating point seconds) of the
|
||||
last command.
|
||||
- `last_command_status`: The exit status of the last command.
|
||||
|
||||
|
||||
Exported functions
|
||||
------------------
|
||||
|
||||
- `time_to_human`: Prints a floating point time duration in seconds.
|
||||
|
||||
|
||||
Authors
|
||||
-------
|
||||
|
||||
*The authors of this module should be contacted via the [issue tracker][1].*
|
||||
|
||||
- [Alex Reece](https://github.com/awreece)
|
||||
|
||||
[1]: https://github.com/awreece/prezto/issues
|
||||
|
26
modules/last_command/functions/time_to_human
Normal file
26
modules/last_command/functions/time_to_human
Normal file
|
@ -0,0 +1,26 @@
|
|||
#
|
||||
# Pretty prints a time in human readable format.
|
||||
#
|
||||
# Authors:
|
||||
# Alex Reece <awreece@gmail.com>
|
||||
#
|
||||
|
||||
# Converts a floating point time duration in seconds to a human readable string.
|
||||
function time_to_human {
|
||||
seconds=$1
|
||||
if (( seconds < 10 )); then
|
||||
printf "%6.3fs" $seconds
|
||||
elif (( seconds < 60 )); then
|
||||
printf "%6.3fs" $seconds
|
||||
elif (( seconds < (60*60) )); then
|
||||
printf "%6.3fm" $(( seconds / 60 ))
|
||||
elif (( seconds < (60*60*24) )); then
|
||||
printf "%6.3fh" $(( seconds / (60*60) ))
|
||||
elif (( seconds < (60*60*24*30) )); then
|
||||
printf "%6.3fd" $(( seconds / (60*60*24) ))
|
||||
elif (( seconds < (60*60*24*30*12) )); then
|
||||
printf "%6.3fm" $(( seconds / (60*60*24*30) ))
|
||||
else
|
||||
printf "%6.3fy" $(( seconds / (60*60*24*30*12) ))
|
||||
fi
|
||||
}
|
39
modules/last_command/init.zsh
Normal file
39
modules/last_command/init.zsh
Normal file
|
@ -0,0 +1,39 @@
|
|||
#
|
||||
# Exports information about the last command.
|
||||
#
|
||||
# Authors:
|
||||
# Alex Reece <awreece@gmail.com>
|
||||
#
|
||||
|
||||
# Exported values.
|
||||
last_command=''
|
||||
last_command_status=0
|
||||
last_command_time=0.0
|
||||
|
||||
# Not exported.
|
||||
last_command_start_time=invalid
|
||||
|
||||
zmodload zsh/datetime # For EPOCHREALTIME.
|
||||
|
||||
function last_command_precmd {
|
||||
exit_status=$? # TODO(awreece) What happens if another precmd runs first?
|
||||
|
||||
# We do these 'invalid' shenanigans because zsh executes precmd but not
|
||||
# preexec if an empty line is entered.
|
||||
if [[ $last_command_start_time != 'invalid' ]]; then
|
||||
last_command_status=$exit_status
|
||||
last_command_time=$((EPOCHREALTIME - last_command_start_time))
|
||||
|
||||
last_command_start_time='invalid'
|
||||
fi
|
||||
}
|
||||
|
||||
function last_command_preexec {
|
||||
last_command_start_time=$EPOCHREALTIME
|
||||
last_command=$1
|
||||
}
|
||||
|
||||
autoload -Uz add-zsh-hook
|
||||
|
||||
add-zsh-hook precmd last_command_precmd
|
||||
add-zsh-hook preexec last_command_preexec
|
19
modules/notify/README.md
Normal file
19
modules/notify/README.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
Last Command
|
||||
============
|
||||
|
||||
Notifies if the command completes and terminal isn't the foreground window.
|
||||
|
||||
Caveats
|
||||
-------
|
||||
Currently only works on Mac OSX with `terminal-notifier`. At some point, I
|
||||
will add support for other notification mechanisms.
|
||||
|
||||
Authors
|
||||
-------
|
||||
|
||||
*The authors of this module should be contacted via the [issue tracker][1].*
|
||||
|
||||
- [Alex Reece](https://github.com/awreece)
|
||||
|
||||
[1]: https://github.com/awreece/prezto/issues
|
||||
|
60
modules/notify/init.zsh
Normal file
60
modules/notify/init.zsh
Normal file
|
@ -0,0 +1,60 @@
|
|||
# Notifies if the last command completes and terminal window is not in
|
||||
# foreground.
|
||||
#
|
||||
# Authors:
|
||||
# Alex Reece <awreece@gmail.com>
|
||||
#
|
||||
|
||||
pmodload 'last_command'
|
||||
|
||||
if [[ "$OSTYPE" == darwin* ]]; then
|
||||
terminal_window_id=$(osascript -e 'tell application "Terminal" to ¬' \
|
||||
-e ' get id of front window')
|
||||
fi
|
||||
|
||||
# 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 ]]
|
||||
}
|
||||
|
||||
# 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.
|
||||
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
|
||||
}
|
||||
|
||||
function notify_precmd {
|
||||
if ! window_is_focused; then
|
||||
last_command_notify
|
||||
fi
|
||||
}
|
||||
|
||||
autoload -Uz add-zsh-hook
|
||||
|
||||
add-zsh-hook precmd notify_precmd
|
|
@ -39,22 +39,8 @@ EOH
|
|||
function prompt_awreece_setup {
|
||||
prompt_opts=(cr percent subst)
|
||||
|
||||
# Load required functions.
|
||||
autoload -Uz add-zsh-hook
|
||||
zmodload zsh/datetime # For EPOCHREALTIME.
|
||||
|
||||
# Add hooks used for timing each command and displaying notifications.
|
||||
add-zsh-hook precmd prompt_awreece_precmd
|
||||
add-zsh-hook preexec prompt_awreece_preexec
|
||||
|
||||
last_run_time=0
|
||||
last_start_time='invalid'
|
||||
last_command=''
|
||||
last_status=0
|
||||
if [[ "$OSTYPE" == darwin* ]]; then
|
||||
terminal_window_id=$(osascript -e 'tell application "Terminal" to ¬' \
|
||||
-e ' get id of front window')
|
||||
fi
|
||||
# Load prerequisites.
|
||||
pmodload 'last_command'
|
||||
|
||||
# Variables used for prompts.
|
||||
local background_job_status='%(1j.%F{yellow}%j&%f .)'
|
||||
|
@ -70,83 +56,8 @@ function prompt_awreece_setup {
|
|||
RPROMPT="${background_job_status}${full_directory}${ssh_host}${command_time}"
|
||||
}
|
||||
|
||||
# Converts a floating point time in seconds to a human readable string.
|
||||
function prompt_awreece_time_to_human {
|
||||
seconds=$1
|
||||
if (( seconds < 10 )); then
|
||||
printf "%6.3fs" $seconds
|
||||
elif (( seconds < 60 )); then
|
||||
printf "%6.3fs" $seconds
|
||||
elif (( seconds < (60*60) )); then
|
||||
printf "%6.3fm" $(( seconds / 60 ))
|
||||
elif (( seconds < (60*60*24) )); then
|
||||
printf "%6.3fh" $(( seconds / (60*60) ))
|
||||
else
|
||||
printf "%6.3fd" $(( seconds / (60*60*24) ))
|
||||
fi
|
||||
}
|
||||
|
||||
function prompt_awreece_command_time {
|
||||
prompt_awreece_time_to_human $last_run_time
|
||||
}
|
||||
|
||||
function prompt_awreece_preexec {
|
||||
last_start_time=$EPOCHREALTIME
|
||||
last_command=$1
|
||||
}
|
||||
|
||||
function prompt_awreece_precmd {
|
||||
exit_status=$? # TODO(awreece) What happens if another precmd runs first?
|
||||
|
||||
# We do these 'invalid' shenanigans because zsh executes precmd but not
|
||||
# preexec if an empty line is entered.
|
||||
if [[ $last_start_time != 'invalid' ]]; then
|
||||
last_status=$exit_status
|
||||
last_run_time=$((EPOCHREALTIME - last_start_time))
|
||||
|
||||
if ! prompt_awreece_is_focused; then
|
||||
prompt_awreece_notify
|
||||
fi
|
||||
|
||||
last_start_time='invalid'
|
||||
last_comamnd=''
|
||||
fi
|
||||
}
|
||||
|
||||
# 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 prompt_awreece_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 ]]
|
||||
}
|
||||
|
||||
# Sends a notification that the last command terminated.
|
||||
# Warning: currently only implemented for mac.
|
||||
function prompt_awreece_notify {
|
||||
message=$(printf "Command \"%s\" finished (%d) after %s." \
|
||||
$last_command \
|
||||
$last_status \
|
||||
$(prompt_awreece_time_to_human $last_run_time))
|
||||
|
||||
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
|
||||
time_to_human $last_command_time
|
||||
}
|
||||
|
||||
# Return a zero exit status (true) iff the current shell is controlled via ssh.
|
||||
|
@ -161,8 +72,8 @@ function prompt_awreece_is_ssh {
|
|||
function prompt_awreece_preview {
|
||||
local +h PROMPT='%# '
|
||||
local +h RPROMPT=''
|
||||
# Set a last_run_time to make it interesting.
|
||||
local +h last_run_time=3.14159
|
||||
# Set a last_command_time to make it interesting.
|
||||
local +h last_commmand_time=3.14159
|
||||
|
||||
prompt_preview_theme 'awreece' "$@"
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue