From 448664739ad1182460dfca49929c6662eb4ad4b1 Mon Sep 17 00:00:00 2001 From: Joel Purra Date: Sun, 12 Oct 2014 12:53:29 +0200 Subject: [PATCH] Allow adding of arbitrary paths; play add [count] [order] [path ...] --- src/play-add.sh | 2 +- src/play-shared-functionality-mutexed.sh | 2 - src/play-shared-functions.sh | 56 +++++++++++++++++++++--- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/play-add.sh b/src/play-add.sh index 255a872..1cb0d41 100755 --- a/src/play-add.sh +++ b/src/play-add.sh @@ -21,4 +21,4 @@ then shift fi -getSounds | playOrder | limit | absoluteSoundPaths >> "$sharedQueueFile" +getSounds "$@" | playOrder | limit >> "$sharedQueueFile" diff --git a/src/play-shared-functionality-mutexed.sh b/src/play-shared-functionality-mutexed.sh index af24d74..2fedd71 100755 --- a/src/play-shared-functionality-mutexed.sh +++ b/src/play-shared-functionality-mutexed.sh @@ -25,8 +25,6 @@ sharedOrder="$sharedDefaultOrder" sharedCacheFile=".play.cache~" -sharedCwd=$(getCdw) - ensureFoldersAndFilesExist readConfig diff --git a/src/play-shared-functions.sh b/src/play-shared-functions.sh index 5da7b5d..9ec6f2f 100755 --- a/src/play-shared-functions.sh +++ b/src/play-shared-functions.sh @@ -34,8 +34,22 @@ onExit() { return 0 } -getCdw() { - echo "$(cd -- "$PWD"; echo "$PWD")" +resolveDirectory() { + echo -n "$(cd -- "$1"; echo "$PWD")" +} + +# Isn't there a better way to do this? String wise ./ and ../ squisher that doesn't re-parse directories/links? +resolvePath() { + if [[ -d "$1" ]]; + then + resolveDirectory "$1" + else + echo -n "$(resolveDirectory "$(dirname "$1")")/$(basename -a "$1")" + fi +} + +getCwd() { + resolveDirectory "$PWD" } allsounds() { @@ -54,20 +68,48 @@ getOrGenerateSoundCache() { } absoluteSoundPaths() { + local cwd=$(getCwd) + # Using read seems to be about 7 times slower than sed - why? # NOTE: The sed version might be suceptible to improperly escaped characters. # TODO: Can be written as a sed replace using \0 instead of nullAsNewline? - # nullAsNewline sed -e 's|^./||' -e "s|^|$(echo -n -e "${sharedCwd/&/\\&}")/|" + # nullAsNewline sed -e 's|^./||' -e "s|^|$(echo -n -e "${cwd/&/\\&}")/|" while IFS= read -r -d '' sound; do - echo -n "${sharedCwd}/${sound/#.\/}" + echo -n "${cwd}/${sound/#.\/}" echo -n -e "\0" done } +getSoundsInFolder() { + getOrGenerateSoundCache | absoluteSoundPaths +} + getSounds() { - getOrGenerateSoundCache + if (( $# == 0 )); + then + getSoundsInFolder + else + local cwd=$(getCwd) + + for soundPath in "$@"; + do + if [[ -d "${soundPath}" ]]; + then + # TODO: is pushd/popd better? + cd -- "${soundPath}" + getSoundsInFolder + cd - >/dev/null + elif [[ -s "${soundPath}" ]]; + then + echo -n $(resolvePath "${cwd}/${soundPath}") + echo -n -e "\0" + else + errorMessage "could not add the path '${soundPath}' to playlist." + fi + done + fi } shuffle() { @@ -159,7 +201,9 @@ playSound() { } highlight() { - echo "$@" | sed "s|$sharedCwd/||" | grep --extended-regexp --color "/?[^/]+$" + local cwd=$(getCwd) + + echo "$@" | sed "s|^${cwd}/||" | grep --extended-regexp --color "/?[^/]+$" } nullAsNewline() {