Skip to content

Commit

Permalink
Allow adding of arbitrary paths; play add [count] [order] [path ...]
Browse files Browse the repository at this point in the history
  • Loading branch information
joelpurra committed Oct 12, 2014
1 parent ad3f923 commit 4486647
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/play-add.sh
Expand Up @@ -21,4 +21,4 @@ then
shift
fi

getSounds | playOrder | limit | absoluteSoundPaths >> "$sharedQueueFile"
getSounds "$@" | playOrder | limit >> "$sharedQueueFile"
2 changes: 0 additions & 2 deletions src/play-shared-functionality-mutexed.sh
Expand Up @@ -25,8 +25,6 @@ sharedOrder="$sharedDefaultOrder"

sharedCacheFile=".play.cache~"

sharedCwd=$(getCdw)

ensureFoldersAndFilesExist

readConfig
56 changes: 50 additions & 6 deletions src/play-shared-functions.sh
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 4486647

Please sign in to comment.