Skip to content

Commit

Permalink
Merge branch 'notifications'
Browse files Browse the repository at this point in the history
* notifications:
  Return success from functions, move warning if no notifier was found, even less output
  Reduce amount of information output by default
  Additional documentation
  np daemon and notify --is-running, improved --stop and process handling
  Add support for checking if notifications are already running
  Notification support, at least on OS X with terminal-notifier
  • Loading branch information
joelpurra committed Apr 13, 2015
2 parents a66cbde + 459ecb9 commit 1d9d4e9
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 29 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Keep a daemon running in the background. Add sounds to a queue. Let the daemon p
- Adds sounds from deep folder structures by default.
- Control music playback daemon from any terminal window.
- Displays song paths relative to `$PWD`.
- Optional system notifications.
- Creates cached/index files to handle large sound libraries, useful on slow disks and network drives.


Expand All @@ -28,11 +29,17 @@ brew install npshell
On other systems:

- Clone the code then add a symlink to `npshell/src/np` to your path.
- Run `np daemon &` on system startup (such as `.bash_profile`) to keep the daemon in the background.
- Add `np daemon --is-running || ( np daemon & )` to your `~/.bash_profile` or similar.
- Requirements:
- One of the music players `afplay`, `mplayer`, `mpg123`, `mpg321`, `play` in your `$PATH`.
- [`fswatch`](https://github.com/emcrisostomo/fswatch), [`bash`](https://www.gnu.org/software/bash/) 4+, `shuf` (or `brew` prefixed `gshuf`), `tac` (or `brew` prefixed `gtac`) from [`coreutils`](https://www.gnu.org/software/coreutils/).

For notifications:

- Add `np notify --is-running || ( np notify & )` to your `~/.bash_profile` or similar.
- Requirements
- [`terminal-notifier`](https://github.com/alloy/terminal-notifier) or [`growlnotify`](http://growl.info/downloads).



## Get started
Expand Down
28 changes: 26 additions & 2 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,44 @@ List of commands, configuration and other usage of `np`.
## Commands


### `np daemon [--stop]`
### `np daemon [--is-running|--stop|--verbose]`

Play sounds in queue as soon as there are any. Can be controlled with `np start` and `np stop`, as well as the rest of the queue commands.

- `--is-running`
- Check if the daemon process has already started. Exits with `0` if it has, `1` otherwise.
- `--stop`
- Stop daemon execution. Can be used during system shutdown, but isn't part of everyday usage.
- `--verbose`
- Output paths to the sounds as they play.


**Examples**

```bash
# Start the daemon, let it run in the background.
# Should be done at user login.
np daemon &
np daemon --is-running || ( np daemon & )
```



### `np notify [--is-running|--stop]`

Show notifications when the track changes, playback is started/stopped or the queue is empty.

- `--is-running`
- Check if the daemon process has already started. Exits with `0` if it has, `1` otherwise.
- `--stop`
- Stop notification daemon execution. Can be used during system shutdown, but isn't part of everyday usage.


**Examples**

```bash
# Start the notification daemon, let it run in the background.
# Should be done at user login.
np notify --is-running || ( np notify & )
```


Expand Down
2 changes: 1 addition & 1 deletion src/np-add.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ getLineCount() {
declare -i lineCount=$(getSounds "$@" | playOrder | limit | tee -a "$configQueueFile" | nullAsNewline getLineCount)
cat "$configQueueFile" | nullAsNewline numberLines | nullAsNewline tail -n "$lineCount" | highlightAllWithLineNumbers

echo "Added ${lineCount} sounds."
displayMessage "Added ${lineCount} sounds."
19 changes: 18 additions & 1 deletion src/np-daemon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,33 @@ set -e
source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"

if [[ "$1" == "--is-running" ]];
then
if isDaemonRunning;
then
exit 0
else
exit 1
fi
fi

if [[ "$1" == "--stop" ]];
then
killPidFromFile "$configDaemonPidFile"
killDaemonIfRunning
killExternalPlayerIfRunning
exit 0
fi

exitIfAlreadyRunning "$configDaemonPidFile" "daemon"
savePidButDeleteOnExit "daemon" "$$" "$configDaemonPidFile"

verboseOutput="false"
if [[ "$1" == "--verbose" ]];
then
shift || true
verboseOutput="true"
fi

whenQueueOrModeIsChanged() {
waitForFileChange "$configQueueFile" "$configModeFile"
}
Expand Down
42 changes: 28 additions & 14 deletions src/np-doctor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,44 @@ source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"
source "${BASH_SOURCE%/*}/shared/mutexed.sh"

echo "Configuration: $configConfigFolder"
displayMessage "Configuration: $configConfigFolder"

echo "External player: '$externalPlayerExec'"
echo "fswatch: '$fswatchExec'"
echo "Shuffler: '$externalShuffleExec'"
echo "Line reverser: '$reverseLineOrderExec'"

echo "Mode: $(cat "$configModeFile")"
displayMessage "External player: '$externalPlayerExec'"
displayMessage "fswatch: '$fswatchExec'"
displayMessage "Shuffler: '$externalShuffleExec'"
displayMessage "Line reverser: '$reverseLineOrderExec'"
displayMessage "Notifications: '$notifyExec'"

echo -n "Daemon: "

displayMessage "Mode: $(cat "$configModeFile")"


displayMessage -n "Daemon: "
if isValidPidFile "$configDaemonPidFile" && isPidRunningFromFile "$configDaemonPidFile";
then
echo "running (pid $(cat "$configDaemonPidFile" ))"
displayMessage "running (pid $(cat "$configDaemonPidFile" ))"
else
echo "stopped"
displayMessage "stopped"
fi

echo -n "External player: "

displayMessage -n "Notifications: "
if isValidPidFile "$configNotifyPidFile" && isPidRunningFromFile "$configNotifyPidFile";
then
displayMessage "running (pid $(cat "$configNotifyPidFile" ))"
else
displayMessage "stopped"
fi


displayMessage -n "External player: "
if isExternalPlayerRunning;
then
echo "running (pid $(cat "$configExternalPlayerPidFile" ))"
displayMessage "running (pid $(cat "$configExternalPlayerPidFile" ))"

echo -n "Sound: "
highlight "$(cat "$configPlayingFile")" || echo
displayMessage -n "Sound: "
highlight "$(cat "$configPlayingFile")" || displayMessage
else
echo "stopped"
displayMessage "stopped"
fi
69 changes: 69 additions & 0 deletions src/np-notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
set -e

source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"

if [[ "$1" == "--is-running" ]];
then
if isNotificationsRunning;
then
exit 0
else
exit 1
fi
fi

if [[ "$1" == "--stop" ]];
then
killNotificationsIfRunning
exit 0
fi

exitIfAlreadyRunning "$configNotifyPidFile" "notify"
savePidButDeleteOnExit "notify" "$$" "$configNotifyPidFile"

checkNotify() {
local mode=$(getMode)
local sound=$(getCurrentSound)
local message

notificationCount+=1

# Don't display upon startup if np is stopped.
if (( notificationCount == 1 )) && [[ "$mode" == "stopped" ]];
then
return 0
fi

if [[ "$mode" == "playing" && -z "$sound" ]];
then
# Don't display upon startup if queue is empty.
if (( notificationCount == 1 ));
then
return 0
fi

message="Reached end of queue."
else
message="$sound"
fi

notify "$mode" "$message"
}

whenCurrentSoundIsChanged() {
waitForFileChange "$configPlayingFile"
}

notifyLoop() {
while true;
do
checkNotify
whenCurrentSoundIsChanged
done
}

declare -i notificationCount=0

notifyLoop
2 changes: 1 addition & 1 deletion src/np-now.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ source "${BASH_SOURCE%/*}/shared/functions.sh"
source "${BASH_SOURCE%/*}/shared/functionality.sh"
source "${BASH_SOURCE%/*}/shared/mutexed.sh"

highlight "$(cat "$configPlayingFile")"
highlight "$(getCurrentSound)"
3 changes: 3 additions & 0 deletions src/shared/functionality.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ configDaemonPidFile="$configDefaultDaemonPidFile"
readonly configDefaultExternalPlayerPidFile="${configConfigFolder}/.externalplayerpidfile~"
configExternalPlayerPidFile="$configDefaultExternalPlayerPidFile"

readonly configDefaultNotifyPidFile="${configConfigFolder}/.notifypidfile~"
configNotifyPidFile="$configDefaultNotifyPidFile"


# File configuration defaults.
configDefaultQueueFile="${configConfigFolder}/queue.pls"
Expand Down
2 changes: 2 additions & 0 deletions src/shared/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ source "${BASH_SOURCE%/*}/functions/queue.sh"
source "${BASH_SOURCE%/*}/functions/externalplayer.sh"
source "${BASH_SOURCE%/*}/functions/display.sh"
source "${BASH_SOURCE%/*}/functions/mode.sh"
source "${BASH_SOURCE%/*}/functions/daemon.sh"
source "${BASH_SOURCE%/*}/functions/notify.sh"
35 changes: 35 additions & 0 deletions src/shared/functions/daemon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -e

cleanupDaemon() {
if [[ -e "$configDaemonPidFile" ]];
then
rm "$configDaemonPidFile"
fi

return 0
}

killDaemon() {
if [[ -s "$configDaemonPidFile" ]];
then
killPidFromFileAndWaitUntilDead "$configDaemonPidFile"
fi

return 0
}

isDaemonRunning() {
if isValidPidFile "$configDaemonPidFile" && isPidRunningFromFile "$configDaemonPidFile";
then
return 0
else
return 1
fi
}

killDaemonIfRunning() {
{ isDaemonRunning && killDaemon; } || true &>/dev/null

return 0
}
8 changes: 6 additions & 2 deletions src/shared/functions/display.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env bash
set -e

displayMessage() {
echo "$@"
}

highlight() {
if (("$#" == 2 ));
then
Expand All @@ -14,7 +18,7 @@ highlight() {
die "wrong number of highlight arguments"
fi

echo -E "$line" | sed "s|^${cwd}/||" | grep --extended-regexp --color "/?[^/]+$"
displayMessage -E "$line" | sed "s|^${cwd}/||" | grep --extended-regexp --color "/?[^/]+$"
}

highlightAll() {
Expand All @@ -34,7 +38,7 @@ highlightWithLineNumbers() {
die "wrong number of highlight arguments"
fi

echo -E "$line" | sedExtRegexp "s|^([[:space:]]*-?[[:digit:]]+[[:space:]]+)${cwd}/|\1|" | grep --extended-regexp --color "/?[^/]+$"
displayMessage -E "$line" | sedExtRegexp "s|^([[:space:]]*-?[[:digit:]]+[[:space:]]+)${cwd}/|\1|" | grep --extended-regexp --color "/?[^/]+$"
}

highlightAllWithLineNumbers() {
Expand Down
16 changes: 13 additions & 3 deletions src/shared/functions/externalplayer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ externalPlayer() {
local externalplayerPid="$!"
savePidAtIndexButDeleteOnExit "$index" "externalplayer" "$externalplayerPid" "$configExternalPlayerPidFile"
wait "$externalplayerPid" &>/dev/null
killExternalPlayerIfRunning
killExternalPlayerIfRunningAndCleanup
clearPidAtIndex "$index"
}

Expand All @@ -18,13 +18,17 @@ cleanupExternalPlayer() {
then
rm "$configExternalPlayerPidFile"
fi

return 0
}

killExternalPlayer() {
if [[ -s "$configExternalPlayerPidFile" ]];
then
killPidFromFile "$configExternalPlayerPidFile"
killPidFromFileAndWaitUntilDead "$configExternalPlayerPidFile"
fi

return 0
}

isExternalPlayerRunning() {
Expand All @@ -37,7 +41,13 @@ isExternalPlayerRunning() {
}

killExternalPlayerIfRunning() {
{ isExternalPlayerRunning && killExternalPlayer; } &>/dev/null
{ isExternalPlayerRunning && killExternalPlayer; } || true &>/dev/null

return 0
}

killExternalPlayerIfRunningAndCleanup() {
killExternalPlayerIfRunning
cleanupExternalPlayer
}

Expand Down
9 changes: 7 additions & 2 deletions src/shared/functions/mode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ playSoundInPlayer() {
if [[ -s "$sound" ]];
then
echo "$sound" > "$configPlayingFile"
highlight "$sound"

if [[ "$verboseOutput" == "true" ]];
then
highlight "$(getCurrentSound)"
echo -ne '\r'
fi

( trap 'echo -n > "$configPlayingFile"' SIGINT EXIT; { playSound "$sound" || true; } )
else
errorMessage "play: sound not found: '${sound}'."
fi

echo -n > "$configPlayingFile"
echo -ne '\r'
}

setModeStart() {
Expand Down
Loading

0 comments on commit 1d9d4e9

Please sign in to comment.