Permalink
Browse files

Allow -p and -n to be repeated

  • Loading branch information...
1 parent b4277c2 commit 2189619f4f4439a5d5489fc5c66d298de6ea763e @ttencate committed Jan 12, 2013
Showing with 34 additions and 23 deletions.
  1. +29 −18 playnext
  2. +5 −5 test.sh
View
@@ -26,8 +26,8 @@ function print_usage() {
echo " -f, --progress-file=filename configuration file name (default: ~/.playnextrc)"
echo " -h, --help show this help"
echo " -l, --list print the list of last episodes played"
- echo " -n, --no-advance don't advance; next run will print same output"
- echo " -p, --previous print previously outputted episode"
+ echo " -n, --next skip episode (can be repeated)"
+ echo " -p, --previous play previous episode (can be repeated)"
echo " -v, --verbose verbose mode; useful for debugging"
}
@@ -88,23 +88,33 @@ function find_next_episode() {
exit_error "No episodes found in $media_dir"
fi
+ local previous_index
if [[ -z $previous_episode ]]; then
print_verbose "No previous episode found; starting from first"
- current_episode="${episodes[0]}"
- return
+ previous_index=-1
+ else
+ for (( i = 0; i < ${#episodes[@]}; i++ )); do
+ if [[ ${episodes[$i]} == $previous_episode ]]; then
+ previous_index=$i
+ break
+ return
+ fi
+ done
fi
- for (( i = 0; i < ${#episodes[@]}; i++ )); do
- if [[ ${episodes[$i]} == $previous_episode ]]; then
- if (( i + 1 >= $num_episodes )); then
- exit_error "No more episodes after ${previous_episode#$media_dir/}"
- fi
- current_episode="${episodes[$(( i + 1 ))]}"
- return
- fi
- done
+ if [[ -z previous_index ]]; then
+ exit_error "Previous episode ${previous_episode#$media_dir/} not found; use -e to specify episode"
+ fi
+
+ local current_index=$(( previous_index + offset ))
+ if (( current_index < 0 )); then
+ exit_error "No more episodes before ${episodes[0]#$media_dir/}"
+ fi
+ if (( current_index >= $num_episodes )); then
+ exit_error "No more episodes after ${episodes[$(( num_episodes - 1 ))]#$media_dir/}"
+ fi
- exit_error "Previous episode ${previous_episode#$media_dir/} not found; use -e to specify episode"
+ current_episode="${episodes[$current_index]}"
}
function usage_error() {
@@ -146,11 +156,11 @@ while (( $# > 0 )); do
-l | --list)
list=1
;;
- -n | --no-advance)
- no_advance=1
+ -n | --next)
+ offset=$(( offset + 1 ))
;;
-p | --previous)
- previous=1
+ offset=$(( offset - 1 ))
;;
-v | --verbose)
verbose=1
@@ -169,6 +179,7 @@ done
progress_file=${progress_file-$HOME/.playnextrc}
media_dir=${media_dir-.}
command=${command-$DEFAULT_COMMAND}
+offset=$(( offset + 1 ))
touch "$progress_file"
read_progress_file
@@ -199,5 +210,5 @@ else
fi
if $command "${current_episode}"; then
- (( no_advance )) || write_progress_file "$current_episode"
+ write_progress_file "$current_episode"
fi
View
10 test.sh
@@ -126,18 +126,18 @@ function test_list_option() {
assert_output "$file" -l
}
-function test_no_advance_option() {
+function test_next_option() {
cd $media_dir_1
- assert_output "$media_dir_1/Dir 1/File 1"
- assert_output "$media_dir_1/Dir 1/file 2" -n
assert_output "$media_dir_1/Dir 1/file 2" -n
+ assert_output "$media_dir_1/Dir 3/File 1" -n
}
function test_previous_option() {
cd $media_dir_1
- assert_output "$media_dir_1/Dir 1/File 1"
- assert_output "$media_dir_1/Dir 1/File 1" -p
+ playnext > /dev/null
assert_output "$media_dir_1/Dir 1/File 1" -p
+ assert_output "$media_dir_1/Dir 1/file 2"
+ assert_output "$media_dir_1/Dir 1/File 1" -p -p
}
function test_command_option() {

0 comments on commit 2189619

Please sign in to comment.