summaryrefslogtreecommitdiff
path: root/bin/shuffle
diff options
context:
space:
mode:
authorSam Nystrom <sam@samnystrom.dev>2023-07-10 21:55:33 -0400
committerSam Nystrom <sam@samnystrom.dev>2023-07-19 11:52:52 -0400
commite2f8339d5a0543100dd5b382d9f5fe292491e212 (patch)
tree27d4f8e39cacb8483b13bff4bad9884d76dc9abd /bin/shuffle
parent3666746ea6243478e07987904433d8493c039ef4 (diff)
bin: small fixes
Diffstat (limited to 'bin/shuffle')
-rwxr-xr-xbin/shuffle62
1 files changed, 33 insertions, 29 deletions
diff --git a/bin/shuffle b/bin/shuffle
index a8bfdc4..103b95c 100755
--- a/bin/shuffle
+++ b/bin/shuffle
@@ -5,38 +5,42 @@ import random
import os
import subprocess
-parser = argparse.ArgumentParser(
- prog='shuffle',
- description='play audio files in a directory in random order')
-parser.add_argument(
- 'path',
- default='.',
- nargs='?',
- help=
- 'play files in <path>, or the current directory if <path> is not passed')
-parser.add_argument('-m',
- dest='mpv_opts',
- default=['--no-video'],
- action='append',
- help='pass <opts> to mpv')
-args = parser.parse_args()
+def main():
+ parser = argparse.ArgumentParser(
+ prog='shuffle',
+ description='play audio files in a directory in random order')
+ parser.add_argument(
+ 'path',
+ default='.',
+ nargs='?',
+ help=
+ 'play files in <path>, or the current directory if <path> is not passed')
+ parser.add_argument('-m',
+ dest='mpv_opts',
+ default=['--no-video'],
+ action='append',
+ help='pass <opts> to mpv')
+ args = parser.parse_args()
-prev = []
+ # Keep track of the last few files we played and avoid playing them again
+ prev = []
-while True:
- files = [
- os.path.join(root, file) for root, _, files in os.walk(args.path)
- for file in files
- ]
- file = random.choice(files)
- while file in prev:
+ while True:
+ files = [
+ os.path.join(root, file) for root, _, files in os.walk(args.path)
+ for file in files
+ ]
+ for f in prev:
+ files.remove(f)
file = random.choice(files)
- prev.append(file)
- if len(prev) > 2:
- del prev[:len(prev) - 3]
+ prev.append(file)
+ del prev[:len(prev) - 2]
- print('Playing', file)
- try:
+ print('Playing', file)
subprocess.run(['mpv', *args.mpv_opts, file])
+
+if __name__ == "__main__":
+ try:
+ main()
except KeyboardInterrupt:
- break
+ pass