From e2f8339d5a0543100dd5b382d9f5fe292491e212 Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Mon, 10 Jul 2023 21:55:33 -0400 Subject: bin: small fixes --- bin/shuffle | 62 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) (limited to 'bin/shuffle') 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 , or the current directory if is not passed') -parser.add_argument('-m', - dest='mpv_opts', - default=['--no-video'], - action='append', - help='pass 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 , or the current directory if is not passed') + parser.add_argument('-m', + dest='mpv_opts', + default=['--no-video'], + action='append', + help='pass 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 -- cgit v1.2.3