From 9befb9456121b9bd919ddb4f1aef9a1efaa7fb5f Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 23 Mar 2012 10:33:44 -0700 Subject: [PATCH] -n option for random command (#24) --- beetsplug/rdm.py | 31 +++++++++++++++++++------------ docs/plugins/rdm.rst | 4 ++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/beetsplug/rdm.py b/beetsplug/rdm.py index 8131cdf58..d081c2a73 100644 --- a/beetsplug/rdm.py +++ b/beetsplug/rdm.py @@ -34,19 +34,24 @@ def random_item(lib, config, opts, args): template = Template(fmt) if opts.album: - items = list(lib.albums(query)) - item = random.choice(items) - if path: - print_(item.item_dir()) - else: - print_(template.substitute(item._record)) + objs = list(lib.albums(query=query)) else: - items = list(lib.items(query)) - item = random.choice(items) - if path: - print_(item.path) - else: - print_(template.substitute(item.record)) + objs = list(lib.items(query=query)) + number = min(len(objs), opts.number) + objs = random.sample(objs, number) + + if opts.album: + for album in objs: + if path: + print_(album.item_dir()) + else: + print_(template.substitute(album._record)) + else: + for item in objs: + if path: + print_(item.path) + else: + print_(template.substitute(item.record)) random_cmd = Subcommand('random', help='chose a random track or album') @@ -56,6 +61,8 @@ random_cmd.parser.add_option('-p', '--path', action='store_true', help='print the path of the matched item') random_cmd.parser.add_option('-f', '--format', action='store', help='print with custom format', default=None) +random_cmd.parser.add_option('-n', '--number', action='store', type="int", + help='number of objects to choose', default=1) random_cmd.func = random_item class Random(BeetsPlugin): diff --git a/docs/plugins/rdm.rst b/docs/plugins/rdm.rst index 1ced7fecf..4d8eb279e 100644 --- a/docs/plugins/rdm.rst +++ b/docs/plugins/rdm.rst @@ -15,3 +15,7 @@ The command has several options that resemble those for the ``beet list`` command (see :doc:`/reference/cli`). To choose an album instead of a single track, use ``-a``; to print paths to items instead of metadata, use ``-p``; and to use a custom format for printing, use ``-f FORMAT``. + +The ``-n NUMBER`` option controls the number of objects that are selected and +printed (default 1). To select 5 tracks from your library, type ``beet random +-n5``.