Search code examples
unixmocha.jspiping

Piping to notify-send in chunks


I'm trying to compose a command to send unix output to my desktop notifications using the notify-send cl tool. I have the following command:

mocha -w | while read SPAM_OUT; do notify-send -t 5000 "mocha:" "$SPAM_OUT"; done Which does what I want, except that I'd like it to spit out the entirety of mocha's output in a single notification every time mocha barfs out some new stuff. Right now, I get a notification for every single line, which is profoundly annoying.

If there are any tools which should already do this for me, I'd be interested in them too.


Solution

  • This should do what you expect:

    notify-send -t 5000 "mocha:" "$(mocha -w)"
    

    This puts the complete output of mocha -w in the fourth argument of notify-send

    If mocha -w does not terminate, the bash-specific read -t comes in handy:

    mocha -w | ( while true; do MSG=""; while read -t .1 LINE; do MSG="$MSG $LINE"; done; if [ "$MSG" != "" ]; then notify-send -t 5000 "$MSG"; fi; done; )
    

    This aggregates all lines which come in in the timeframe of 1/10th second in one message. You can adjust this timeout to fit your needs. Note that this is bash-specific, other shells (i.e. dash) may not support it.