Compare commits
No commits in common. "master" and "0.5.0" have entirely different histories.
7 changed files with 47 additions and 219 deletions
|
@ -6,7 +6,7 @@ test: &test
|
|||
- checkout
|
||||
- run:
|
||||
name: Install Dependencies
|
||||
command: apt-get update && apt-get install -y curl git ncurses-bin libnotify-bin
|
||||
command: apt-get update && apt-get install -y curl git ncurses-bin
|
||||
- run:
|
||||
name: Download requirements
|
||||
command: |
|
||||
|
@ -25,7 +25,7 @@ test: &test
|
|||
jobs:
|
||||
lint:
|
||||
docker:
|
||||
- image: "python:3.11"
|
||||
- image: "python:3.7-stretch"
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
|
|
28
CHANGELOG.md
28
CHANGELOG.md
|
@ -1,34 +1,6 @@
|
|||
Changelog for zsh-auto-notify
|
||||
=============================
|
||||
|
||||
0.10.2
|
||||
------
|
||||
* Use preferable array argument expansion for flexible parameters
|
||||
|
||||
0.10.1
|
||||
------
|
||||
* Fix regression where not setting icon on Linux would cause issues (#59 #58)
|
||||
|
||||
0.10.0
|
||||
-----
|
||||
* Allow specifying an icon with notify-send backends
|
||||
|
||||
0.8.0
|
||||
-----
|
||||
* Change notify-send application title to `zsh`
|
||||
|
||||
0.7.0
|
||||
-----
|
||||
* Allow alternate `AUTO_NOTIFY_WHITELIST` for specifying commands to allow
|
||||
|
||||
0.6.0
|
||||
-----
|
||||
* Display warning and disable auto-notify if notify-send is not installed (Linux only)
|
||||
|
||||
0.5.1
|
||||
-----
|
||||
* Improved handling of MacOS notifications via #16 (Thanks @dmitmel!)
|
||||
|
||||
0.5.0
|
||||
-----
|
||||
* Support changing notification title and body using AUTO_NOTIFY_TITLE and AUTO_NOTIFY_BODY
|
||||
|
|
39
README.rst
39
README.rst
|
@ -64,14 +64,6 @@ Zgen_
|
|||
|
||||
zgen load "MichaelAquilina/zsh-auto-notify"
|
||||
|
||||
Fig_
|
||||
|
||||
Install ``zsh-auto-notify`` with Fig in just one click.
|
||||
|
||||
.. image:: https://fig.io/badges/install-with-fig.svg
|
||||
:target: https://fig.io/plugins/other/zsh-auto-notify_MichaelAquilina
|
||||
:alt: Install with Fig
|
||||
|
||||
oh-my-zsh_
|
||||
|
||||
Copy this repository to ``$ZSH_CUSTOM/custom/plugins``, where ``$ZSH_CUSTOM``
|
||||
|
@ -107,14 +99,13 @@ You can change the formatting of notifications by setting the values for ``AUTO_
|
|||
``AUTO_NOTIFY_BODY``. When writing these values, the following variables will be replaced according to
|
||||
the data that ``auto-notify`` has detected:
|
||||
|
||||
* ``%command`` - the command that the user executed
|
||||
* ``%elapsed`` - number of seconds that elapsed
|
||||
* ``%exit_code`` - the exit code of the command that was executed
|
||||
* `%command` - the command that the user executed
|
||||
* `%elapsed` - number of seconds that elapsed
|
||||
* `%exit_code` - the exit code of the command that was executed
|
||||
|
||||
An example of how these values can be set is shown below:
|
||||
|
||||
::
|
||||
|
||||
export AUTO_NOTIFY_TITLE="Hey! %command has just finished"
|
||||
export AUTO_NOTIFY_BODY="It completed in %elapsed seconds with exit code %exit_code"
|
||||
|
||||
|
@ -151,28 +142,6 @@ a new array.
|
|||
# redefine what is ignored by auto-notify
|
||||
export AUTO_NOTIFY_IGNORE=("docker" "man" "sleep")
|
||||
|
||||
**Using a Whitelist to ignore commands**
|
||||
|
||||
If you wish to use a whitelist approach instead of the default blacklist approach used by ``AUTO_NOTIFY_IGNORE``,
|
||||
you can do so by defining the environment variable ``AUTO_NOTIFY_WHITELIST`` with the elements you wish to
|
||||
allow ``auto-notify`` to track and send notifications for. NOTE: If ``AUTO_NOTIFY_WHITELIST`` is defined,
|
||||
then all the values in ``AUTO_NOTIFY_IGNORE`` are not used.
|
||||
|
||||
::
|
||||
|
||||
export AUTO_NOTIFY_WHITELIST=("apt-get" "docker")
|
||||
|
||||
**Adding an icon - Linux**
|
||||
|
||||
If you wish to have an icon displayed on command success and/or failure, you can do so by defining the environmental variables ``AUTO_NOTIFY_ICON_SUCCESS`` and ``AUTO_NOTIFY_ICON_FAILURE`` respectively.
|
||||
|
||||
::
|
||||
|
||||
export AUTO_NOTIFY_ICON_SUCCESS=/path/to/success/icon.png
|
||||
export AUTO_NOTIFY_ICON_FAILURE=/path/to/failure/icon.png
|
||||
|
||||
|
||||
|
||||
Temporarily Disabling Notifications
|
||||
-----------------------------------
|
||||
|
||||
|
@ -221,8 +190,6 @@ NOTE: It is required that you use a minimum zunit version of 0.8.2
|
|||
|
||||
.. _ZGen: https://github.com/tarjoilija/zgen
|
||||
|
||||
.. _Fig: https://fig.io
|
||||
|
||||
.. _oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh
|
||||
|
||||
.. |GPLv3| image:: https://img.shields.io/badge/License-GPL%20v3-blue.svg
|
||||
|
|
|
@ -1,30 +1,12 @@
|
|||
export AUTO_NOTIFY_VERSION="0.10.2"
|
||||
export AUTO_NOTIFY_VERSION="0.5.0"
|
||||
|
||||
# Time it takes for a notification to expire
|
||||
[[ -z "$AUTO_NOTIFY_EXPIRE_TIME" ]] &&
|
||||
export AUTO_NOTIFY_EXPIRE_TIME=8000
|
||||
# Threshold in seconds for when to automatically show a notification
|
||||
[[ -z "$AUTO_NOTIFY_THRESHOLD" ]] &&
|
||||
export AUTO_NOTIFY_THRESHOLD=10
|
||||
|
||||
[[ -z "$AUTO_NOTIFY_PUBLISH" ]] &&
|
||||
export AUTO_NOTIFY_PUBLISH="zsh_events"
|
||||
|
||||
# List of commands/programs to ignore sending notifications for
|
||||
[[ -z "$AUTO_NOTIFY_IGNORE" ]] &&
|
||||
export AUTO_NOTIFY_IGNORE=(
|
||||
'vim'
|
||||
'nvim'
|
||||
'less'
|
||||
'more'
|
||||
'man'
|
||||
'tig'
|
||||
'watch'
|
||||
'git commit'
|
||||
'top'
|
||||
'htop'
|
||||
'ssh'
|
||||
'nano'
|
||||
"vim" "nvim" "less" "more" "man" "tig" "watch" "git commit" "top" "htop" "ssh" "nano"
|
||||
)
|
||||
|
||||
function _auto_notify_format() {
|
||||
|
@ -44,30 +26,30 @@ function _auto_notify_message() {
|
|||
local exit_code="$3"
|
||||
local platform="$(uname)"
|
||||
# Run using echo -e in order to make sure notify-send picks up new line
|
||||
local DEFAULT_TITLE="ZSH: '%command' finished"
|
||||
local DEFAULT_BODY="$(echo -e "Time: %elapsed seconds elapsed\nExit code: %exit_code")"
|
||||
local DEFAULT_PUBLISH="zsh_events"
|
||||
local DEFAULT_TITLE="\"%command\" Completed"
|
||||
local DEFAULT_BODY="$(echo -e "Total time: %elapsed seconds\nExit code: %exit_code")"
|
||||
|
||||
local title="${AUTO_NOTIFY_TITLE:-$DEFAULT_TITLE}"
|
||||
local text="${AUTO_NOTIFY_BODY:-$DEFAULT_BODY}"
|
||||
local publish="${AUTO_NOTIFY_PUBLISH:-$DEFAULT_PUBLISH}"
|
||||
|
||||
title="$(_auto_notify_format "$title" "$command" "$elapsed" "$exit_code")"
|
||||
body="$(_auto_notify_format "$text" "$command" "$elapsed" "$exit_code")"
|
||||
|
||||
local urgency="3"
|
||||
local icon=${AUTO_NOTIFY_ICON_SUCCESS:-""}
|
||||
# Exit code 130 is returned when a process is terminated with SIGINT.
|
||||
# Since the user is already interacting with the program, there is no
|
||||
# need to make the notification persistent.
|
||||
if [[ "$exit_code" != "0" ]] && [[ "$exit_code" != "130" ]]; then
|
||||
urgency="4"
|
||||
icon=${AUTO_NOTIFY_ICON_FAILURE:-""}
|
||||
if [[ "$platform" == "Linux" ]]; then
|
||||
local urgency="normal"
|
||||
if [[ "$exit_code" != "0" ]]; then
|
||||
urgency="critical"
|
||||
fi
|
||||
notify-send "$title" "$body" "--urgency=$urgency" "--expire-time=$AUTO_NOTIFY_EXPIRE_TIME"
|
||||
elif [[ "$platform" == "Darwin" ]]; then
|
||||
# We need to escape quotes since we are passing a script into a command
|
||||
body="${body//\"/\\\"}"
|
||||
title="${title//\"/\\\"}"
|
||||
osascript -e "display notification \"$body\" with title \"$title\""
|
||||
else
|
||||
printf "Unknown platform for sending notifications: $platform\n"
|
||||
printf "Please post an issue on gitub.com/MichaelAquilina/zsh-auto-notify/issues/\n"
|
||||
fi
|
||||
|
||||
local arguments=("--title='$title'" "--priority=$urgency" $publish "$body")
|
||||
|
||||
ntfy publish -q ${arguments[@]}
|
||||
}
|
||||
|
||||
function _is_auto_notify_ignored() {
|
||||
|
@ -76,33 +58,8 @@ function _is_auto_notify_ignored() {
|
|||
local command_list=("${(@s/|/)command}")
|
||||
local target_command="${command_list[-1]}"
|
||||
# Remove leading whitespace
|
||||
target_command="$(echo "$target_command" | sed -e 's/^ *//')"
|
||||
target_command="$(echo "$target_command" | sed -e 's/^ *//')}"
|
||||
|
||||
# If the command is being run over SSH, then ignore it
|
||||
if [[ -n ${SSH_CLIENT-} || -n ${SSH_TTY-} || -n ${SSH_CONNECTION-} ]]; then
|
||||
print "yes"
|
||||
return
|
||||
fi
|
||||
|
||||
# Remove sudo prefix from command if detected
|
||||
if [[ "$target_command" == "sudo "* ]]; then
|
||||
target_command="${target_command/sudo /}"
|
||||
fi
|
||||
|
||||
# If AUTO_NOTIFY_WHITELIST is defined, then auto-notify will ignore
|
||||
# any item not defined in the white list
|
||||
# Otherwise - the alternative (default) approach is used where the
|
||||
# AUTO_NOTIFY_IGNORE blacklist is used to ignore commands
|
||||
|
||||
if [[ -n "$AUTO_NOTIFY_WHITELIST" ]]; then
|
||||
for allowed in $AUTO_NOTIFY_WHITELIST; do
|
||||
if [[ "$target_command" == "$allowed"* ]]; then
|
||||
print "no"
|
||||
return
|
||||
fi
|
||||
done
|
||||
print "yes"
|
||||
else
|
||||
for ignore in $AUTO_NOTIFY_IGNORE; do
|
||||
if [[ "$target_command" == "$ignore"* ]]; then
|
||||
print "yes"
|
||||
|
@ -110,7 +67,6 @@ function _is_auto_notify_ignored() {
|
|||
fi
|
||||
done
|
||||
print "no"
|
||||
fi
|
||||
}
|
||||
|
||||
function _auto_notify_send() {
|
||||
|
@ -165,5 +121,4 @@ function enable_auto_notify() {
|
|||
}
|
||||
|
||||
_auto_notify_reset_tracking
|
||||
|
||||
enable_auto_notify
|
|
@ -89,25 +89,10 @@
|
|||
assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed'
|
||||
assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds"
|
||||
assert "$lines[3]" same_as "Exit code: 0"
|
||||
assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000"
|
||||
assert "$lines[4]" same_as "--urgency=normal --expire-time=15000"
|
||||
}
|
||||
|
||||
@test 'auto-notify-send sends notification and icon on Linux on success' {
|
||||
AUTO_COMMAND="f bar -r"
|
||||
AUTO_COMMAND_FULL="foo bar -r"
|
||||
AUTO_COMMAND_START=11080
|
||||
AUTO_NOTIFY_EXPIRE_TIME=15000
|
||||
AUTO_NOTIFY_ICON_SUCCESS=/path/to/success/icon.png
|
||||
run _auto_notify_send
|
||||
|
||||
assert $state equals 0
|
||||
assert "$lines[1]" same_as 'Notification Title: "f bar -r" Completed'
|
||||
assert "$lines[2]" same_as "Notification Body: Total time: 20 seconds"
|
||||
assert "$lines[3]" same_as "Exit code: 0"
|
||||
assert "$lines[4]" same_as "--app-name=zsh --hint=int:transient:1 --urgency=normal --expire-time=15000 --icon=/path/to/success/icon.png"
|
||||
}
|
||||
|
||||
@test 'auto-notify-send sends notification on macOS' {
|
||||
@test 'auto-notify-send sends notification on MacOSX' {
|
||||
AUTO_COMMAND="f bar -r"
|
||||
AUTO_COMMAND_FULL="foo bar -r"
|
||||
AUTO_COMMAND_START=11080
|
||||
|
@ -123,8 +108,8 @@
|
|||
run _auto_notify_send
|
||||
|
||||
assert $state equals 0
|
||||
assert "$lines[1]" same_as '-e on run argv -e display notification (item 1 of argv) with title (item 2 of argv) -e end run Total time: 20 seconds'
|
||||
assert "$lines[2]" same_as 'Exit code: 0 "f bar -r" Completed'
|
||||
assert "$lines[1]" same_as '-e display notification "Total time: 20 seconds'
|
||||
assert "$lines[2]" same_as 'Exit code: 0" with title "\"f bar -r\" Completed"'
|
||||
}
|
||||
|
||||
@test 'auto-notify-send sends warning on unsupported platform' {
|
||||
|
|
|
@ -33,30 +33,3 @@
|
|||
assert $state equals 0
|
||||
assert "$output" same_as "yes"
|
||||
}
|
||||
|
||||
@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST disallowed' {
|
||||
AUTO_NOTIFY_WHITELIST="foobar"
|
||||
|
||||
run _is_auto_notify_ignored "boom baz"
|
||||
|
||||
assert $state equals 0
|
||||
assert "$output" same_as "yes"
|
||||
}
|
||||
|
||||
@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST allowed' {
|
||||
AUTO_NOTIFY_WHITELIST="foobar"
|
||||
|
||||
run _is_auto_notify_ignored "foobar baz"
|
||||
|
||||
assert $state equals 0
|
||||
assert "$output" same_as "no"
|
||||
}
|
||||
|
||||
@test 'is_auto_notify_ignored - AUTO_NOTIFY_WHITELIST allowed with sudo' {
|
||||
AUTO_NOTIFY_WHITELIST="foobar"
|
||||
|
||||
run _is_auto_notify_ignored "sudo foobar baz"
|
||||
|
||||
assert $state equals 0
|
||||
assert "$output" same_as "no"
|
||||
}
|
||||
|
|
|
@ -1,47 +1,23 @@
|
|||
#!/usr/bin/env zunit
|
||||
|
||||
@setup {
|
||||
function uname {
|
||||
printf "Linux"
|
||||
}
|
||||
# Mock function for passing builds where we don't
|
||||
# really need notify-send to be installed (e.g. Mac OSX)
|
||||
function notify-send {
|
||||
}
|
||||
}
|
||||
|
||||
@test 'print warning if notify-send is not installed' {
|
||||
function type {
|
||||
return 1
|
||||
}
|
||||
|
||||
run load "../auto-notify.plugin.zsh"
|
||||
|
||||
assert "$lines[1]" same_as "'notify-send' must be installed for zsh-auto-notify to work"
|
||||
assert "$lines[2]" same_as "Please install it with your relevant package manager"
|
||||
|
||||
@test 'dont load auto-notify if notify-send is not installed' {
|
||||
function type {
|
||||
return 1
|
||||
}
|
||||
|
||||
load "../auto-notify.plugin.zsh"
|
||||
|
||||
assert "_auto_notify_track" not_in $preexec_functions
|
||||
assert "_auto_notify_send" not_in $precmd_functions
|
||||
}
|
||||
|
||||
@test 'version exported' {
|
||||
git_version="$(git tag --list | sort -V | tail -1)"
|
||||
git tag --list
|
||||
|
||||
assert "$AUTO_NOTIFY_VERSION" is_not_empty
|
||||
assert "$AUTO_NOTIFY_VERSION" same_as "$git_version"
|
||||
}
|
||||
|
||||
@test 'hook functions are loaded by default' {
|
||||
load "../auto-notify.plugin.zsh"
|
||||
|
||||
assert '_auto_notify_track' in $preexec_functions
|
||||
assert '_auto_notify_send' in $precmd_functions
|
||||
}
|
||||
|
||||
@test 'enable/disable auto-notify' {
|
||||
load "../auto-notify.plugin.zsh"
|
||||
|
||||
disable_auto_notify
|
||||
assert '_auto_notify_track' not_in $preexec_functions
|
||||
assert '_auto_notify_send' not_in $precmd_functions
|
||||
|
|
Loading…
Add table
Reference in a new issue