command/QueueCommands: offset relative "addid" positions by one

Now, "+0" means "right after the current song" and "-0" means "right
before the current song".  Mnemonic: there are zero songs between the
current song and the newly added song.
This commit is contained in:
Max Kellermann 2021-10-07 22:06:13 +02:00
parent 3993176b76
commit 076c9a0dd9
2 changed files with 21 additions and 20 deletions

View File

@ -711,9 +711,10 @@ Whenever possible, ids should be used.
If the second parameter is given, then the song is inserted at the If the second parameter is given, then the song is inserted at the
specified position. If the parameter starts with ``+`` or ``-``, specified position. If the parameter starts with ``+`` or ``-``,
then it is relative to the current song; e.g. ``+1`` inserts right then it is relative to the current song; e.g. ``+0`` inserts right
after the current song and ``-1`` inserts right before the current after the current song and ``-0`` inserts right before the current
song. (``±0`` is not a legal value.) song (i.e. zero songs between the current song and the newly added
song).
.. _command_clear: .. _command_clear:

View File

@ -120,8 +120,8 @@ handle_addid(Client &client, Request args, Response &r)
const auto queue_length = partition.playlist.queue.GetLength(); const auto queue_length = partition.playlist.queue.GetLength();
const char *const s = args[1]; const char *const s = args[1];
if (*s == '+' || *s == '-') { if (*s == '+') {
/* relative to the current song */ /* after the current song */
const int current = const int current =
partition.playlist.GetCurrentPosition(); partition.playlist.GetCurrentPosition();
@ -129,23 +129,23 @@ handle_addid(Client &client, Request args, Response &r)
throw ProtocolError(ACK_ERROR_PLAYER_SYNC, throw ProtocolError(ACK_ERROR_PLAYER_SYNC,
"No current song"); "No current song");
to = args.ParseInt(1, -current - 1, assert(unsigned(current) < queue_length);
queue_length - current);
if (to == 0)
throw ProtocolError(ACK_ERROR_ARG,
"Zero is not a legal relative position");
/* special case for negative offsets: the to = current + 1 +
offset "-1" shall insert the new song right ParseCommandArgUnsigned(s + 1,
before the current song (just like "+1" queue_length - current - 1);
inserts right after the current song); } else if (*s == '-') {
computationally, that would be a zero /* before the current song */
offset, but that's not intuitive, so we
need to add one here */
if (to < 0)
++to;
to += current; const int current =
partition.playlist.GetCurrentPosition();
if (current < 0)
throw ProtocolError(ACK_ERROR_PLAYER_SYNC,
"No current song");
assert(unsigned(current) < queue_length);
to = current - ParseCommandArgUnsigned(s + 1, current);
} else } else
/* absolute position */ /* absolute position */
to = args.ParseUnsigned(1, queue_length); to = args.ParseUnsigned(1, queue_length);