Code

Adding crop feature in ncmpc
authorYannick LM <yannicklm1337@gmail.com>
Sun, 21 Sep 2008 11:50:26 +0000 (13:50 +0200)
committerMax Kellermann <max@duempel.org>
Sun, 21 Sep 2008 11:50:26 +0000 (13:50 +0200)
The "crop" command deletes all songs from the playlist, except the one
currently being played.

src/command.c
src/command.h
src/mpdclient.c
src/mpdclient.h
src/screen.c
src/screen_help.c

index afb7e42e41ea3a1a092859e0378a1b642c2cf73e..0ae90338194157da3b30e2c1f1f1eebf97cfff6f 100644 (file)
@@ -102,6 +102,8 @@ static command_definition_t cmds[] =
     N_("Pause") },
   { { 's',  BS,   0 }, 0, CMD_STOP, "stop",   
     N_("Stop") },
+  { { 'o',  0,   0 }, 0, CMD_CROP, "crop",
+    N_("Crop") },
   { { '>',   0,   0 }, 0, CMD_TRACK_NEXT, "next", 
     N_("Next track") },
   { { '<',   0,   0 }, 0, CMD_TRACK_PREVIOUS, "prev", 
index dab6c99cef93746921f2eaf643d2d26241da42d8..f4b6859e84cf93757cee869b4d8a8758ddef4d2e 100644 (file)
@@ -16,6 +16,7 @@ typedef enum
   CMD_SELECT_ALL,
   CMD_PAUSE,
   CMD_STOP,
+  CMD_CROP,
   CMD_TRACK_NEXT,
   CMD_TRACK_PREVIOUS,
   CMD_SEEK_FORWARD,
index 2c46d8e9069b09201069a9a32221d10605cd18a4..6d9d72b9c206789e6694400783397f8b13553ba1 100644 (file)
@@ -299,6 +299,41 @@ mpdclient_cmd_pause(mpdclient_t *c, gint value)
        return mpdclient_finish_command(c);
 }
 
+gint
+mpdclient_cmd_crop(mpdclient_t *c)
+{
+       mpd_Status *status;
+       int length;
+
+       mpd_sendStatusCommand(c->connection);
+       status = mpd_getStatus(c->connection);
+       length = status->playlistLength - 1;
+
+       if (length <= 0) {
+               mpd_freeStatus(status);
+               screen_status_message("You have to have a playlist longer than 1 song in length to crop");
+       } else if (status->state == 3 || status->state == 2) {
+               /* If playing or paused */
+
+               mpd_sendCommandListBegin( c->connection );
+
+               while (length >= 0) {
+                       if (length != status->song)
+                               mpd_sendDeleteCommand(c->connection, length);
+
+                       length--;
+               }
+
+               mpd_sendCommandListEnd(c->connection);
+               mpd_freeStatus(status);
+       } else {
+               mpd_freeStatus(status);
+               screen_status_message("You need to be playing to crop the playlist\n");
+       }
+
+       return mpdclient_finish_command(c);
+}
+
 gint
 mpdclient_cmd_stop(mpdclient_t *c)
 {
index c55e15453c5f8ae5e6668ae7f8267ec20fca39f5..2735f04ffa067d5cf9c12ff888704143281ab90a 100644 (file)
@@ -46,6 +46,8 @@ gint mpdclient_update(mpdclient_t *c);
 /*** MPD Commands  **********************************************************/
 gint mpdclient_cmd_play(mpdclient_t *c, gint index);
 gint mpdclient_cmd_pause(mpdclient_t *c, gint value);
+gint
+mpdclient_cmd_crop(mpdclient_t *c);
 gint mpdclient_cmd_stop(mpdclient_t *c);
 gint mpdclient_cmd_next(mpdclient_t *c);
 gint mpdclient_cmd_prev(mpdclient_t *c);
index e510b168265ed7f3a3a3ea781890723a51507c7b..2a5d6416f4d04adca020188dffb2488afc377545 100644 (file)
@@ -814,6 +814,9 @@ screen_client_cmd(mpdclient_t *c, command_t cmd)
        case CMD_STOP:
                mpdclient_cmd_stop(c);
                break;
+       case CMD_CROP:
+               mpdclient_cmd_crop(c);
+               break;
        case CMD_SEEK_FORWARD:
                if (!IS_STOPPED(c->status->state)) {
                        if (c->song && seek_id != c->song->id) {
index 7416e6e404f6a6dfcaaf5d35d646fe2ecbbf032d..79b8fa722ef146a9e638be46515285f42644da07 100644 (file)
@@ -67,6 +67,7 @@ static help_text_row_t help_text[] =
   { 2, CMD_NONE,           NULL },
   { 0, CMD_STOP,           NULL },
   { 0, CMD_PAUSE,          NULL },
+  { 0, CMD_CROP, NULL },
   { 0, CMD_TRACK_NEXT,     NULL },
   { 0, CMD_TRACK_PREVIOUS, NULL },
   { 0, CMD_SEEK_FORWARD,   NULL },