-n option for random command (#24)

This commit is contained in:
Adrian Sampson 2012-03-23 10:33:44 -07:00
parent 7e6cdd75d6
commit 9befb94561
2 changed files with 23 additions and 12 deletions

View file

@ -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):

View file

@ -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``.