From c9bd41b9a49b3f77fb4a930db1e8fcc1d00c0d4c Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 22 May 2022 00:51:51 +0100 Subject: mpc: search support --- lib/media/jao-mpc.el | 64 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/media/jao-mpc.el b/lib/media/jao-mpc.el index dc8ff6d..e25cc46 100644 --- a/lib/media/jao-mpc.el +++ b/lib/media/jao-mpc.el @@ -38,8 +38,9 @@ (defvar-local jao-mpc--port nil) (defun jao-mpc--cmd (cmd &optional port) - (let ((port (or port jao-mpc--port jao-mpc-port))) - (shell-command-to-string (format "mpc -p %s %s" port cmd)))) + (let* ((port (or port jao-mpc--port jao-mpc-port)) + (r (shell-command-to-string (format "mpc -p %s %s" port cmd)))) + (replace-regexp-in-string "^\\(warning: \\)?MPD .+\n" "" r))) (defconst jao-mpc--fields '(artist album composer originaldate genre title track position time name)) @@ -55,10 +56,10 @@ (push (cons (intern (match-string 1 s)) (match-string 2 s)) res))))) (defun jao-mpc--playing-p (&optional port) - (not (string-blank-p (jao-mpc--cmd "status|grep '\\[playing\\]'" port)))) + (string-prefix-p "playing" (jao-mpc--cmd "status %state%" port))) (defun jao-mpc--queue-len (&optional port) - (string-to-number (jao-mpc--cmd "playlist|wc -l" port))) + (string-to-number (jao-mpc--cmd "status %length%" port))) (defsubst jao--put-face (str face) (put-text-property 0 (length str) 'face face str) @@ -109,11 +110,13 @@ "idleloop" "player") :filter (lambda (_p _s) (jao-mpc--set-current-str port))))) +(defvar jao-mpc--browser-port nil) + (define-derived-mode jao-mpc-albums-mode fundamental-mode "MPC Albums" "Mode to display the list of albums known by mpd." (read-only-mode -1) (delete-region (point-min) (point-max)) - (insert (jao-mpc--cmd "list album")) + (insert (jao-mpc--cmd "list album" jao-mpc--browser-port)) (goto-char (point-min)) (read-only-mode 1)) @@ -124,12 +127,13 @@ (jao-mpc-albums-mode) (current-buffer)))) -(defun jao-mpc--add-and-play (&optional album) +(defun jao-mpc--add-and-play (&optional album port idp) (interactive) - (let ((album (or album (string-trim (thing-at-point 'line))))) - (jao-mpc--cmd "clear") - (jao-mpc--cmd (format "findadd album \"%s\"" album)) - (jao-mpc--cmd "play"))) + (let ((a (or album (string-trim (thing-at-point 'line)))) + (p (or port jao-mpc--browser-port))) + (jao-mpc--cmd "clear" p) + (jao-mpc--cmd (if idp (concat "add " a) (format "findadd album \"%s\"" a)) p) + (jao-mpc--cmd "play" p))) (define-key jao-mpc-albums-mode-map (kbd "n") #'next-line) (define-key jao-mpc-albums-mode-map (kbd "p") #'previous-line) @@ -216,9 +220,10 @@ (jao-mpc--cmd (format "add %s" url))) ;;;###autoload -(defun jao-mpc-show-albums () +(defun jao-mpc-show-albums (&optional port) "Show album list." (interactive) + (setq jao-mpc--browser-port port) (pop-to-buffer (jao-mpc--album-buffer))) ;;;###autoload @@ -255,19 +260,40 @@ (jao-minibuffer-add-variable 'jao-mpc-minibuffer-str priority) (jao-minibuffer-add-msg-variable 'jao-mpc-minibuffer-str (- priority))))) -(defvar jao-mpc--album-titles nil) (defconst jao-mpc--albums-cmd "-f '%album% - %artist%' find \"(ALBUM =~ '.*')\" | uniq") +(defconst jao-mpc--simple-albums-cmd "list album") ;;;###autoload -(defun jao-mpc-select-album (refresh) - (interactive "P") - (let ((albums (or (and (not refresh) jao-mpc--album-titles) - (setq jao-mpc--album-titles - (split-string (jao-mpc--cmd jao-mpc--albums-cmd) - "\n" t))))) +(defun jao-mpc-select-album (&optional port) + (interactive) + (let* ((albums-str (jao-mpc--cmd jao-mpc--albums-cmd port)) + (albums-str (if (string= "" albums-str) + (jao-mpc--cmd jao-mpc--simple-albums-cmd port) + albums-str)) + (albums (split-string albums-str "\n" t))) (when-let (album (completing-read "Play album: " albums nil t)) - (jao-mpc--add-and-play (car (split-string album "-" t " ")))))) + (jao-mpc--add-and-play (car (split-string album "-" t " ")) port)))) + +(defconst jao-mpc--search-cmd + "-f '%%album%% - %%artist%% :::%%file%%' search %s '%s'|grep :::tidal:album") + +(defun jao-mpc--search-albums (query) + (let* ((cmd (format jao-mpc--search-cmd "any" query)) + (str (jao-mpc--cmd cmd)) + (res (split-string str "\n" t))) + (mapcar (lambda (s) (split-string s ":::" t " ")) res))) + +;;;###autoload +(defun jao-mpc-search-and-select-album (&optional query port) + (interactive "sSearch terms: ") + (let* ((jao-mpc-port (or port jao-mpc-port)) + (resa (jao-mpc--search-albums query))) + (if (null resa) + (user-error "No results") + (when-let* ((a (completing-read "Play album: " resa nil t)) + (s (car (alist-get a resa nil nil 'string=)))) + (jao-mpc--add-and-play s port t))))) (provide 'jao-mpc) ;;; jao-mpc.el ends here -- cgit v1.2.3