Skip to content

Commit a5ecfb6

Browse files
committed
feat(play): Add -R/--randomize option to play plugin
1 parent 9f6a4b4 commit a5ecfb6

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

beetsplug/play.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Send the results of a query to the configured music player as a playlist."""
1616

17+
import random
1718
import shlex
1819
import subprocess
1920
from os.path import relpath
@@ -91,6 +92,12 @@ def commands(self):
9192
action="store",
9293
help="add additional arguments to the command",
9394
)
95+
play_command.parser.add_option(
96+
"-R",
97+
"--randomize",
98+
action="store_true",
99+
help="randomize the order of tracks in the playlist",
100+
)
94101
play_command.parser.add_option(
95102
"-y",
96103
"--yes",
@@ -131,6 +138,9 @@ def _play_command(self, lib, opts, args):
131138
if relative_to:
132139
paths = [relpath(path, relative_to) for path in paths]
133140

141+
if opts.randomize:
142+
random.shuffle(paths)
143+
134144
if not selection:
135145
ui.print_(ui.colorize("text_warning", f"No {item_type} to play."))
136146
return

docs/changelog.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ below!
99
Unreleased
1010
----------
1111

12-
..
13-
New features
14-
~~~~~~~~~~~~
12+
New features
13+
~~~~~~~~~~~~
14+
15+
- :doc:`plugins/play`: Added ``-R``/``--randomize`` flag to shuffle the
16+
playlist order before passing it to the player.
1517

1618
..
1719
Bug fixes

docs/plugins/play.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ The ``--yes`` (or ``-y``) flag to the ``play`` command will skip the warning
120120
message if you choose to play more items than the **warning_threshold** value
121121
usually allows.
122122

123+
The ``--randomize`` (or ``-R``) flag shuffles the order of tracks in the
124+
playlist before passing it to the player:
125+
126+
::
127+
128+
$ beet play --randomize my query
129+
123130
Note on the Leakage of the Generated Playlists
124131
----------------------------------------------
125132

test/plugins/test_play.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ def test_skip_warning_threshold_bypass(self, open_mock):
145145
expected_playlist=expected_playlist,
146146
)
147147

148+
def test_randomize(self, open_mock):
149+
self.other_item = self.add_item(title="anotherTitle")
150+
151+
with patch("beetsplug.play.random.shuffle") as shuffle_mock:
152+
self.run_command("play", "-R", "NiceTitle")
153+
154+
shuffle_mock.assert_called_once()
155+
156+
def test_no_randomize_by_default(self, open_mock):
157+
self.other_item = self.add_item(title="anotherTitle")
158+
159+
with patch("beetsplug.play.random.shuffle") as shuffle_mock:
160+
self.run_command("play", "NiceTitle")
161+
162+
shuffle_mock.assert_not_called()
163+
148164
def test_command_failed(self, open_mock):
149165
open_mock.side_effect = OSError("some reason")
150166

0 commit comments

Comments
 (0)