Skip to content

Commit c7f7ce4

Browse files
committed
Add date field to albums
1 parent f120aae commit c7f7ce4

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

README.org

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The library is implemented around a set of entities.
4343
| *Name* | *Type* | *Fields* | *Description* |
4444
|------------------+-----------+----------------------+---------------------------------------------------------|
4545
| song | structure | name, album, file, … | |
46-
| album | structure | name, artist | |
46+
| album | structure | name, date, artist | |
4747
| artist | structure | name | |
4848
| directory | structure | name, path | |
4949
| stored-playlist | structure | name | A named user-specified sequence of songs |

libmpdel.el

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
(require 'tq)
3434
(require 'cl-lib)
3535
(require 'subr-x)
36+
(require 'seq)
3637

3738

3839
;;; Customization
@@ -171,6 +172,7 @@ message from the server.")
171172
(:constructor libmpdel--album-create)
172173
(:conc-name libmpdel--album-))
173174
(name nil :read-only t)
175+
(date nil :read-only t)
174176
(artist nil :read-only t))
175177

176178
(cl-defstruct (libmpdel-song
@@ -321,9 +323,16 @@ If the SONG's name is nil, return the filename instead."
321323
"Return the track number of SONG within its album."
322324
(or (libmpdel--song-track song) ""))
323325

324-
(defun libmpdel-entity-date (song)
325-
"Return the date of SONG."
326-
(or (libmpdel--song-date song) ""))
326+
(cl-defgeneric libmpdel-entity-date (entity)
327+
"Return the date of ENTITY.")
328+
329+
(cl-defmethod libmpdel-entity-date ((album libmpdel-album))
330+
"Return ALBUM's date."
331+
(libmpdel--album-date album))
332+
333+
(cl-defmethod libmpdel-entity-date ((song libmpdel-song))
334+
"Return SONG's date."
335+
(libmpdel--song-date song))
327336

328337
(defun libmpdel-song-disc (song)
329338
"Return the disc number of SONG within its album."
@@ -345,9 +354,7 @@ If the SONG's name is nil, return the filename instead."
345354
:name (cdr (assq 'Title song-data))
346355
:track (cdr (assq 'Track song-data))
347356
:file (cdr (assq 'file song-data))
348-
:album (libmpdel--album-create
349-
:name (cdr (assq 'Album song-data))
350-
:artist (libmpdel--artist-create :name (cdr (assq 'Artist song-data))))
357+
:album (libmpdel--create-album-from-data song-data)
351358
:date (cdr (assq 'Date song-data))
352359
:disc (cdr (assq 'Disc song-data))
353360
:id (cdr (assq 'Id song-data))
@@ -357,6 +364,17 @@ If the SONG's name is nil, return the filename instead."
357364
"Return a list of songs from DATA, a server's response."
358365
(mapcar #'libmpdel--create-song-from-data (libmpdel-group-data data)))
359366

367+
(defun libmpdel--create-album-from-data (song-data)
368+
"Return an album from ALBUM-DATA, a server's response."
369+
(libmpdel--album-create
370+
:name (cdr (assq 'Album song-data))
371+
:date (cdr (assq 'Date song-data))
372+
:artist (libmpdel--artist-create :name (cdr (assq 'Artist song-data)))))
373+
374+
(defun libmpdel--create-albums-from-data (data)
375+
"Return a list of albums from DATA, a server's response."
376+
(mapcar #'libmpdel--create-album-from-data (libmpdel-group-data data)))
377+
360378
(defun libmpdel-current-playlist-p (entity)
361379
"Return non-nil if ENTITY is the current playlist."
362380
(eq entity 'current-playlist))
@@ -953,11 +971,9 @@ If HANDLER is nil, ignore response."
953971
"list album"
954972
(lambda (data)
955973
(funcall function
956-
(mapcar
957-
(lambda (album-name)
958-
(libmpdel--album-create :name album-name
959-
:artist libmpdel--unknown-artist))
960-
(libmpdel-sorted-entries data 'Album))))))
974+
(seq-uniq
975+
(libmpdel--create-albums-from-data data)
976+
#'libmpdel-equal)))))
961977

962978
(cl-defmethod libmpdel-list ((_entity (eql stored-playlists)) function)
963979
"Call FUNCTION with all stored playlists as parameter."
@@ -972,12 +988,12 @@ If HANDLER is nil, ignore response."
972988
(cl-defmethod libmpdel-list ((artist libmpdel-artist) function)
973989
"Call FUNCTION with all albums of ARTIST as parameter."
974990
(libmpdel-send-command
975-
`("list album %s" ,(libmpdel-entity-to-criteria artist))
991+
`("find %s sort AlbumSort" ,(libmpdel-entity-to-criteria artist))
976992
(lambda (data)
977993
(funcall function
978-
(mapcar
979-
(lambda (album-name) (libmpdel--album-create :name album-name :artist artist))
980-
(libmpdel-sorted-entries data 'Album))))))
994+
(seq-uniq
995+
(libmpdel--create-albums-from-data data)
996+
#'libmpdel-equal)))))
981997

982998
(cl-defgeneric libmpdel-list-songs (entity function)
983999
"Call FUNCTION with all songs of ENTITY."

test/libmpdel-test.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@
116116
(let ((song (libmpdel--create-song-from-data
117117
'((Title . "The song")
118118
(file . "foo/song.ogg")
119+
(Date . "1952-08-29")
119120
(Album . "The Album")
120121
(Artist . "The Artist")))))
121122
(should (equal "The song" (libmpdel-entity-name song)))
122123
(should (equal "foo/song.ogg" (libmpdel-song-file song)))
124+
(should (equal "1952-08-29" (libmpdel-entity-date song)))
125+
(should (equal "1952-08-29" (libmpdel-entity-date (libmpdel-album song))))
123126
(should (equal "The Album" (libmpdel-entity-name (libmpdel-album song))))
124127
(should (equal "The Artist" (libmpdel-entity-name (libmpdel-artist (libmpdel-album song)))))))
125128

0 commit comments

Comments
 (0)