Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'multi' backend and screen locking detection #170

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

ianwremmel
Copy link

@ianwremmel ianwremmel commented Mar 22, 2018

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. 🎉

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

Todo:

  • Linux 🐧 support (xscreensaver, lightlocker, gnome, mate)
  • macOS 🍎 support
  • Windows 🎏 support
  • a better solution than the multi module
  • documentation

Fixes #168 & fixes #125 & supercedes #147

dschep and others added 8 commits June 7, 2017 21:16
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. 🎉
WIP of dschep#125: screen locking detection

* dschep/locked:
  yapf-ify
  WIP of dschep#125: screen locking detection
…reenlock

* commit '84903e7ae7b7ff6ba102558948cd15e5a361a555':
  Add screensaver detection for MacOS
@ianwremmel
Copy link
Author

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

@coveralls
Copy link

Coverage Status

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

2 similar comments
@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

coveralls commented Mar 22, 2018

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@dschep dschep changed the title Add multibackend and detect macos screenlock 'multi' backend and screen locking detection Apr 3, 2018
@dschep dschep mentioned this pull request Apr 3, 2018
4 tasks
@ianwremmel
Copy link
Author

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.

@unai-ndz
Copy link

unai-ndz commented Apr 9, 2019

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

@ianwremmel
Copy link
Author

@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

@unai-ndz
Copy link

unai-ndz commented Apr 9, 2019

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

@ianwremmel
Copy link
Author

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.

@unai-ndz
Copy link

unai-ndz commented Apr 10, 2019

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.

@ianwremmel
Copy link
Author

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

@unai-ndz
Copy link

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():

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pick backend based on screen lock extend focus detection to include if the screen is locked
5 participants