tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit e7b876506cbb2a0278e0ad40ec9cfe8e936482ca
parent d67065277c6e7c6cab7c21d835a2193491b15520
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri,  4 Mar 2022 17:22:17 +0100

Added rename command

Diffstat:
Msrc/cmd.c | 31+++++++++++++++++++++++++++++++
Msrc/data.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/data.h | 2++
Msrc/player_mpd.c | 6+++---
4 files changed, 98 insertions(+), 3 deletions(-)

diff --git a/src/cmd.c b/src/cmd.c @@ -24,6 +24,7 @@ static bool cmd_copy(const char *args); static bool cmd_reindex(const char *args); static bool cmd_add_tag(const char *args); static bool cmd_rm_tag(const char *args); +static bool cmd_rename(const char *args); const struct cmd commands[] = { { "save", cmd_save }, @@ -32,6 +33,7 @@ const struct cmd commands[] = { { "reindex", cmd_reindex }, { "addtag", cmd_add_tag }, { "rmtag", cmd_rm_tag }, + { "rename", cmd_rename }, }; const size_t command_count = ARRLEN(commands); @@ -246,6 +248,35 @@ cmd_rm_tag(const char *name) return true; } +bool +cmd_rename(const char *name) +{ + struct link *link; + struct track *track; + struct tag *tag; + + if (!*name) + CMD_ERROR("Supply a name"); + + ASSERT(pane_sel == cmd_pane); + + if (pane_after_cmd == track_pane) { + link = list_at(tracks_vis, track_nav.sel); + if (!link) return false; + track = tracks_vis_track(link); + if (!track_rename(track, name)) + return false; + } else if (pane_after_cmd == tag_pane) { + link = list_at(&tags, tag_nav.sel); + if (!link) return false; + tag = UPCAST(link, struct tag, link); + if (!tag_rename(tag, name)) + return false; + } + + return true; +} + void cmd_init(void) { diff --git a/src/data.c b/src/data.c @@ -184,6 +184,12 @@ make_dir(const char *path) } bool +move_dir(const char *src, const char *dst) +{ + return rename(src, dst) == 0; +} + +bool rm_dir(const char *path, bool recursive) { char *files[] = { (char *) path, NULL }; @@ -449,6 +455,34 @@ tag_rm(struct tag *tag, bool sync_fs) return true; } +bool +tag_rename(struct tag *tag, const char *name) +{ + struct link *link; + struct track *track; + char *newpath; + + newpath = aprintf("%s/%s", datadir, name); + OOM_CHECK(newpath); + + if (!move_dir(tag->fpath, newpath)) { + free(newpath); + return false; + } + + free(tag->fpath); + tag->fpath = newpath; + + for (LIST_ITER(&tag->tracks, link)) { + track = UPCAST(link, struct track, link_tt); + free(track->fpath); + track->fpath = aprintf("%s/%s", newpath, track->name); + OOM_CHECK(track->fpath); + } + + return true; +} + struct track * track_add(struct tag *tag, const char *fname) { @@ -503,6 +537,34 @@ track_rm(struct track *track, bool sync_fs) } bool +track_rename(struct track *track, const char *name) +{ + char *newpath; + + newpath = aprintf("%s/%s", track->tag->fpath, name); + OOM_CHECK(newpath); + + if (path_exists(newpath)) { + free(newpath); + return false; + } + + if (!move_file(track->fpath, newpath)) { + free(newpath); + return false; + } + + free(track->fpath); + track->fpath = newpath; + + free(track->name); + track->name = strdup(name); + OOM_CHECK(track->name); + + return true; +} + +bool aquire_lock(const char *datadir) { char *lockpath, *procpath; diff --git a/src/data.h b/src/data.h @@ -42,9 +42,11 @@ void playlist_update(bool exec); struct tag *tag_add(const char *fname); struct tag *tag_find(const char *name); bool tag_rm(struct tag *tag, bool sync_fs); +bool tag_rename(struct tag *tag, const char *name); struct track *track_add(struct tag *tag, const char *fname); bool track_rm(struct track *track, bool sync_fs); +bool track_rename(struct track *track, const char *name); bool acquire_lock(const char *path); bool release_lock(const char *path); diff --git a/src/player_mpd.c b/src/player_mpd.c @@ -138,7 +138,7 @@ playlist_track_lru(int skip) } struct track * -playlist_track_next_unused(int skip) +playlist_track_next_unused(int index) { struct track *track; struct link *link, *start; @@ -150,11 +150,11 @@ playlist_track_next_unused(int skip) return NULL; len = list_len(&player.playlist); - start = link = list_at(&player.playlist, skip); + start = link = list_at(&player.playlist, index); while (LIST_INNER(link)) { track = UPCAST(link, struct track, link_pl); - if (!history_contains(track, len)) + if (!history_contains(track, len - 1)) break; link = link->next;