From 7f0340e99ab01219199af30b7393ca0d61ad1213 Mon Sep 17 00:00:00 2001 From: Yannick LM Date: Sun, 21 Sep 2008 13:50:26 +0200 Subject: [PATCH] Adding crop feature in ncmpc The "crop" command deletes all songs from the playlist, except the one currently being played. --- src/command.c | 2 ++ src/command.h | 1 + src/mpdclient.c | 35 +++++++++++++++++++++++++++++++++++ src/mpdclient.h | 2 ++ src/screen.c | 3 +++ src/screen_help.c | 1 + 6 files changed, 44 insertions(+) diff --git a/src/command.c b/src/command.c index afb7e42..0ae9033 100644 --- a/src/command.c +++ b/src/command.c @@ -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", diff --git a/src/command.h b/src/command.h index dab6c99..f4b6859 100644 --- a/src/command.h +++ b/src/command.h @@ -16,6 +16,7 @@ typedef enum CMD_SELECT_ALL, CMD_PAUSE, CMD_STOP, + CMD_CROP, CMD_TRACK_NEXT, CMD_TRACK_PREVIOUS, CMD_SEEK_FORWARD, diff --git a/src/mpdclient.c b/src/mpdclient.c index 2c46d8e..6d9d72b 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -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) { diff --git a/src/mpdclient.h b/src/mpdclient.h index c55e154..2735f04 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -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); diff --git a/src/screen.c b/src/screen.c index e510b16..2a5d641 100644 --- a/src/screen.c +++ b/src/screen.c @@ -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) { diff --git a/src/screen_help.c b/src/screen_help.c index 7416e6e..79b8fa7 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -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 }, -- 2.30.2