ntfy icon indicating copy to clipboard operation
ntfy copied to clipboard

'multi' backend and screen locking detection

Open ianwremmel opened this issue 7 years ago β€’ 14 comments

separate backends implemented via a 'multi' meta-backend. Uses config like this:

backends: [multi]
multi:
    locked:
        pushover:
            user_key: user-api-key
    unfocused:
        default: {}
    focused: {}

This config would cause no notifications if the shell is focused, desktop notifications when unfocused, and pushover notifications when the screen is locked. Freaking magic. :tada:

There's also a --locked-only cli option.

Todo:

  • [x] Linux :penguin: support (xscreensaver, lightlocker, gnome, mate)
  • [x] macOS :apple: support
  • [ ] Windows :flags: support
  • [ ] a better solution than the multi module
  • [ ] documentation

Fixes #168 & fixes #125 & supercedes #147

ianwremmel avatar Mar 22 '18 05:03 ianwremmel

I can't really explain what happened with that last merge commit...

ianwremmel avatar Mar 22 '18 06:03 ianwremmel

Coverage Status

Coverage decreased (-8.5%) to 74.86% when pulling 45a6e172b6d99a7ad4adce12f3ca1823a3c018e0 on ianwremmel:multi-screenlock into c6a49d4cfc039b4b21521afa2e1732654a058d6c on dschep:master.

coveralls avatar Mar 22 '18 06:03 coveralls

Coverage Status

Coverage decreased (-8.5%) to 74.86% when pulling 45a6e172b6d99a7ad4adce12f3ca1823a3c018e0 on ianwremmel:multi-screenlock into c6a49d4cfc039b4b21521afa2e1732654a058d6c on dschep:master.

coveralls avatar Mar 22 '18 06:03 coveralls

Coverage Status

Coverage decreased (-8.5%) to 74.86% when pulling 45a6e172b6d99a7ad4adce12f3ca1823a3c018e0 on ianwremmel:multi-screenlock into c6a49d4cfc039b4b21521afa2e1732654a058d6c on dschep:master.

coveralls avatar Mar 22 '18 06:03 coveralls

Coverage Status

Coverage decreased (-10.2%) to 73.194% when pulling 9e96ba07d9e3286ece9e0638c1919321e741e98b on ianwremmel:multi-screenlock into 9b5e7d25bb64513b3f08d1e1ea16cd18eaeed460 on dschep:master.

coveralls avatar Mar 22 '18 06:03 coveralls

Coverage Status

Coverage decreased (-8.5%) to 74.86% when pulling 45a6e172b6d99a7ad4adce12f3ca1823a3c018e0 on ianwremmel:multi-screenlock into c6a49d4cfc039b4b21521afa2e1732654a058d6c on dschep:master.

coveralls avatar Mar 22 '18 06:03 coveralls

for the new config file format, what about

# specify a version number to maintain compatibility with the old parser
version: 2
backends:
  - 
    # use pushover when the screen is locked
    type: pushover
   # it *might* make sense to put user key under a config hash so that every 
   # backend in the list has the same schema except for whatever is in `config`. 
    user_key: user-api-key
    enable:
      - locked
  -
    # use native notifications when the terminal in question is unfocused
    type: darwin
    enable:
      - unfocused
  - 
    # do not use growl 
    type: growl
    enable: []

Rather than having a hash of configurations and a list of which configurations to use, just mash them all together and include their operational mode as part of their config.

ianwremmel avatar May 18 '18 15:05 ianwremmel

Any updates on this? I'm getting the same as you here. This happens in both ianwremmel:multi-screenlock and ianwremmel:multi-screenlock-2019-02-06

unai-ndz avatar Apr 09 '19 08:04 unai-ndz

@unai-ndz, assuming you’re referring to the failure to notify on long commands, I ended up switching to zsh. The behavior is built in rather than relying on the hacks in bash-preexec

ianwremmel avatar Apr 09 '19 15:04 ianwremmel

Yes, on long running commands. I actually use zsh already. Mainstream works well but when I use your branches it stops working. I'm using the default config from this PR and the eval line on my zshrc

unai-ndz avatar Apr 09 '19 17:04 unai-ndz

I'm not using oh-my-zsh (found it added a lot of overhead that I wasn't using). I source the following file in .zshrc.

# shellcheck disable

AUTO_NTFY_DONE_IGNORE=${AUTO_NTFY_DONE_IGNORE:-ntfy emacs info less mail man meld most mutt nano screen ssh tail tmux vi vim watch bash zsh}
# Bash option example
#AUTO_NTFY_DONE_OPTS='-b default'
# Zsh option example
AUTO_NTFY_DONE_OPTS=(-b multi)
# notify for unfocused only (Used by ntfy internally)
#AUTO_NTFY_DONE_UNFOCUSED_ONLY=-b
# notify for commands runing longer than N sec only (Used by ntfy internally)
AUTO_NTFY_DONE_LONGER_THAN=-L5

function _ntfy_precmd () {
    local ret_value="$?"
    [ -n "$ntfy_start_time" ] || return
    local duration=$(( $(date +%s) - $ntfy_start_time ))
    ntfy_start_time=''

    local appname=$(basename "${ntfy_command%% *}")
    [[ " $AUTO_NTFY_DONE_IGNORE " == *" $appname "* ]] && return

    ntfy $AUTO_NTFY_DONE_OPTS done \
        $AUTO_NTFY_DONE_UNFOCUSED_ONLY $AUTO_NTFY_DONE_LONGER_THAN \
        --formatter "$ntfy_command" "$ret_value" "$duration"
}

function _ntfy_preexec () {
    ntfy_start_time=$(date +%s)
    ntfy_command="$1"
}

function _contains_element() {
    local e
    for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
    return 1
}

# Only setup ntfy shell integration if it's been configured
if [[ -f "$HOME/.ntfy.yml" ]]; then
  if ! _contains_element _ntfy_preexec "${preexec_functions[@]}"; then
      preexec_functions+=(_ntfy_preexec)
  fi

  if ! _contains_element _ntfy_precmd "${precmd_functions[@]}"; then
      precmd_functions+=(_ntfy_precmd)
  fi
fi

This is mostly just what ntfy shell-integration does, but with (I think) three adjustments:

  1. I set the timeout to 5 seconds instead of 10
  2. I changed the backend to multi
  3. I wrapped the preexec/precmd setup to make sure ~/.ntfy.yml exists

2 seems like the only relevant difference.

ianwremmel avatar Apr 09 '19 18:04 ianwremmel

It was actually line 5 of terminal.py missing a point and the except ImportError on multy.py returning False for is_focused()

Changing line 5 of terminal.py to this fixed it: from .screensaver import is_locked

And i also needed to change line 33 to: split('light-locker-command -q')).decode('utf-8') to make it compatible with python 3. I have tried on a console and seems to also be compatible with python2.

It was failing silently before and then started showing errors which helped me fix this. Your config above didn't make a difference but made my life much easier when testing this (waiting 2 seconds instead 0f 11 between tries)

I guess i can provide a patch but being changes so small i don't think is worth it.

unai-ndz avatar Apr 10 '19 10:04 unai-ndz

@unai-ndz no need to open a PR, but could you post the output of git diff?

ianwremmel avatar Apr 10 '19 17:04 ianwremmel

Sure, I actually changed it slightly.

index e6decb4..2e11046 100644
--- a/ntfy/screensaver.py
+++ b/ntfy/screensaver.py
@@ -16,7 +16,7 @@ def xscreensaver_detect():
 
 
 def xscreensaver_is_locked():
-    return 'screen locked' in check_output(split('xscreensaver-command -time'))
+    return b'screen locked' in check_output(split('xscreensaver-command -time'))
 
 
 def lightlocker_detect():
@@ -29,7 +29,7 @@ def lightlocker_detect():
 
 
 def lightlocker_is_active():
-    return 'The screensaver is active' in check_output(
+    return b'The screensaver is active' in check_output(
         split('light-locker-command -q'))
 
 
diff --git a/ntfy/terminal.py b/ntfy/terminal.py
index 30cd3b6..206ce6d 100644
--- a/ntfy/terminal.py
+++ b/ntfy/terminal.py
@@ -2,7 +2,7 @@ import shlex
 from os import environ, ttyname
 from subprocess import PIPE, Popen, check_output, CalledProcessError
 from sys import platform, stdout
-from screensaver import is_locked
+from .screensaver import is_locked
 
 
 def linux_window_is_focused():

unai-ndz avatar Apr 10 '19 20:04 unai-ndz