Skip to content

Instantly share code, notes, and snippets.

@corpulentcoffee
Last active November 26, 2020 18:49
Show Gist options
  • Save corpulentcoffee/c8aab43b90c995d9ee5a9c0b2a351729 to your computer and use it in GitHub Desktop.
Save corpulentcoffee/c8aab43b90c995d9ee5a9c0b2a351729 to your computer and use it in GitHub Desktop.
Hack for notify-send to tolerate --expire-time false
#!/usr/bin/env python3
"""
Jest calls `node-notifier` using `notify({ timeout: false })`, which
ends up being sent down to `notify-send` with `--expire-time false`.
`notify-send` doesn't actually support this, which will write an error
of "Cannot parse integer value 'false' for --expire-time" to stderr and
exit with a non-zero status code.
This is a quick and dirty `~/bin` hack to filter out the bad arguments
before passing the rest to `/usr/bin/notify-send`.
No longer needed as of `node-notifier@8.0.0`, see:
- https://github.com/facebook/jest/issues/9701
- https://github.com/mikaelbr/node-notifier/pull/341
- https://github.com/mikaelbr/node-notifier/commit/860c06e192540e11a93d63d50fc5008ba2c1fa3b
"""
from subprocess import run
from sys import argv
from syslog import syslog
debug = lambda msg: syslog(f"[notifier hack] {msg}")
args = argv[1:]
debug(f"received {args}")
with_positions = list(enumerate(args))
bad_start_positions = [
start_position
for ((start_position, arg_name), (end_position, arg_value))
in zip(with_positions, with_positions[1:])
if arg_name == "--expire-time" and arg_value == "false"
]
if bad_start_positions:
for bad_start_position in reversed(bad_start_positions):
args = args[:bad_start_position] + args[bad_start_position + 2:]
debug(f"rewritten to {args}")
else:
debug(f"sending as-is")
status = run(["/usr/bin/notify-send", *args]).returncode
exit(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment