Skip to content

Commit

Permalink
Merge branch 'shutdown-on-update'
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkunz committed Oct 8, 2021
2 parents 3535a62 + 5d1cc2a commit 20d1945
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ if get_option('tests').enabled()
foreach test_name, test_sources : tests
test_exe = executable(
test_name + '_test',
test_sources,
include_directories : src_inc,
link_with: libashuffle,
dependencies : absl_deps + gtest_deps + [mpdfake_dep],
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ tweaks, and their meanings:

| Name | Values | Default | Description |
| ---- | ------ | ------- | ----------- |
| `window-size` | Integer `>=1` | `7` | Sets the size of the "window" used for the shuffle algorithm. See the section on the [shuffle algorithm](#shuffle-algorithm) for more details. In-short: Lower numbers mean more frequent repeats, and higher numbers mean less frequent repeats. |
| `exit-on-db-update` | Boolean | `no` | If set to a true value, then ashuffle will exit when the MPD database is updated. This can be useful when used in conjunction with the `-f -` option, as it allows you to re-start ashuffle with a new music list. |
| `play-on-startup` | Boolean | `yes` | If set to a true value, ashuffle starts playing music if MPD is paused, stopped, or the queue is empty on startup. If set to false, then ashuffle will not enqueue any music until a song is enqueued for the first time. |
| `suspend-timeout` | Duration `> 0` | `0ms` | Enables "suspend" mode, which may be useful to users that use ashuffe in a workflow where they clear their queue. In this mode, if the queue is cleared while ashuffle is running, ashuffle will wait for `suspend-timeout`. If songs were added to the queue during that period of time (i.e., the queue is no longer empty), then ashuffle suspends itself, and will not add any songs to the queue (even if the queue runs out) until the queue is cleared again, at which point normal operations resume. This was add to support use-cases like the one given in issue #13, where a music player had a "play album" mode that would clear the queue, and then play an album. See below for the duration format. |
| `window-size` | Integer `>=1` | `7` | Sets the size of the "window" used for the shuffle algorithm. See the section on the [shuffle algorithm](#shuffle-algorithm) for more details. In-short: Lower numbers mean more frequent repeats, and higher numbers mean less frequent repeats. |

Value types:

Expand Down
13 changes: 12 additions & 1 deletion src/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ std::variant<Parser::State, ParseError> Parser::ParseTweak(

if (key == "play-on-startup") {
auto v = ParseBool(value);
if (!v) {
if (!v.has_value()) {
return ParseError(absl::StrFormat(
"play-on-startup must be a boolean value ('%s' given)", value));
}
Expand All @@ -257,6 +257,17 @@ std::variant<Parser::State, ParseError> Parser::ParseTweak(
return kNone;
}

if (key == "exit-on-db-update") {
auto v = ParseBool(value);
if (!v.has_value()) {
return ParseError(absl::StrFormat(
"exit-on-db-update must be a boolean value ('%s' given)",
value));
}
opts_.tweak.exit_on_db_update = *v;
return kNone;
}

return ParseError(absl::StrFormat("unrecognized tweak '%s'", arg));
}

Expand Down
4 changes: 4 additions & 0 deletions src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class Options {
bool play_on_startup = true;
// Duration to wait before checking queue length for suspend/resume.
absl::Duration suspend_timeout = absl::ZeroDuration();
// If true, exit when MPD produces a database update event. This is
// intented to be used in cases where the user is passing in a
// list of songs via -f, and they may want to re-generate that list.
bool exit_on_db_update = false;
} tweak = {};
std::vector<enum mpd_tag_type> group_by = {};

Expand Down
6 changes: 6 additions & 0 deletions src/ashuffle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ void Loop(mpd::MPD *mpd, ShuffleChain *songs, const Options &options,
while (test_d.until_f == nullptr || test_d.until_f()) {
/* wait till the player state changes */
mpd::IdleEventSet events = mpd->Idle(set);

if (events.Has(MPD_IDLE_DATABASE) && options.tweak.exit_on_db_update) {
std::cout << "Database updated, exiting." << std::endl;
std::exit(0);
}

/* Only update the database if our original list was built from
* MPD. */
if (events.Has(MPD_IDLE_DATABASE) && options.file_in == nullptr) {
Expand Down
7 changes: 7 additions & 0 deletions t/args_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TEST(ParseTest, Empty) {
EXPECT_EQ(opts.tweak.window_size, 7);
EXPECT_EQ(opts.tweak.play_on_startup, true);
EXPECT_EQ(opts.tweak.suspend_timeout, absl::ZeroDuration());
EXPECT_EQ(opts.tweak.exit_on_db_update, false);
}

TEST(ParseTest, Short) {
Expand Down Expand Up @@ -211,6 +212,12 @@ TEST(ParseTest, TweakSuspendTimeout) {
}
}

TEST(ParseTest, TweakExitOnDBUpdate) {
Options opts = std::get<Options>(Options::Parse(
fake::TagParser(), {"--tweak", "exit-on-db-update=yes"}));
EXPECT_EQ(opts.tweak.exit_on_db_update, true);
}

using ParseFailureParam =
std::tuple<std::vector<std::string>, Matcher<std::string>>;

Expand Down
22 changes: 21 additions & 1 deletion t/ashuffle_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,26 @@ TEST(MPDUpdateTest, GroupByPersistsAcrossUpdate) {
EXPECT_THAT(mpd.state.song_position, Optional(2));
}

TEST(MPDUpdateTest, ExitOnDBUpdateTweak) {
fake::MPD mpd;

// Just use an empty chain here, we don't care about the songs.
ShuffleChain chain;

// Group by album.
Options opts;
opts.tweak.exit_on_db_update = true;

// Only run the inner loop for this test.
opts.tweak.play_on_startup = false;

// Trigger a database update.
mpd.idle_f = [] { return mpd::IdleEventSet(MPD_IDLE_DATABASE); };

EXPECT_EXIT({ Loop(&mpd, &chain, opts, loop_once_d); }, ExitedWithCode(0),
testing::_);
}

struct ConnectTestCase {
// Want is used to set the actual server host/port.
mpd::Address want;
Expand Down Expand Up @@ -681,4 +701,4 @@ TEST(PrintChainLengthTest, Groups) {

PrintChainLength(out, chain);
EXPECT_THAT(out.str(), HasSubstr("2 groups (3 songs)"));
}
}

0 comments on commit 20d1945

Please sign in to comment.