From 95b7edebb2b037f1f6e9eb85a6b3bd917d669dcf Mon Sep 17 00:00:00 2001 From: jao Date: Fri, 15 Jan 2021 22:01:06 +0000 Subject: espotify: explicit trigger suffix --- media/espotify.org | 53 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/media/espotify.org b/media/espotify.org index c52d08e..1548c71 100644 --- a/media/espotify.org +++ b/media/espotify.org @@ -259,16 +259,19 @@ Let's start with an umbrella customization group: The top-level command will have this form: #+begin_src emacs-lisp + (require 'consult) + (defvar espotify-consult-history nil) (defun consult-spotify-by (type &optional filter) - (consult--read (format "Search %ss: " type) - (espotify--search-generator type filter) - :lookup 'espotify--consult-lookup - :category 'espotify-search-item - :history 'espotify-consult-history - :initial "#" - :require-match t)) + (let ((orderless-matching-styles '(orderless-literal))) + (consult--read (format "Search %ss: " type) + (espotify--search-generator type filter) + :lookup 'espotify--consult-lookup + :category 'espotify-search-item + :history 'espotify-consult-history + :initial "#" + :require-match t))) #+end_src where we can write an asynchronous generator of search results @@ -292,32 +295,44 @@ Let's start with an umbrella customization group: case: #+begin_src emacs-lisp + (defvar espotify-search-suffix "=" + "Suffix in the search string launching an actual Web query.") + (defun espotify--async-search (next type filter) (lambda (action) (pcase action ((pred stringp) - (espotify-search-all - (lambda (x) - (funcall next 'flush) - (funcall next x)) - action ;; when we receive a string as the action, - ;; it's the user input - type - filter)) + (if (string-suffix-p espotify-search-suffix action) + (espotify-search-all + (lambda (x) + (funcall next 'flush) + (funcall next x)) + (substring action 0 (- (length action) + (length espotify-search-suffix))) + type + filter))) (_ (funcall next action))))) #+end_src + We have introduced the convention that we're only launching a search + when the input string ends in "=", to avoid piling on HTTP requests. + When processing the results, we format them as a displayable string, while hiding in a property the URI that will allow us to play the item (and pass the formatter to ~consult-async--map~, in ~espotify--search-generator~ above): #+begin_src emacs-lisp + (defun espotify--additional-info (x) + (or (alist-get 'name (alist-get 'album x)) + (alist-get 'name (car (alist-get 'artists x))) + (alist-get 'display_name (alist-get 'owner x)))) + (defun espotify--format-item (x) (propertize (format "%s%s" (alist-get 'name x) - (if-let ((a (alist-get 'album x))) - (format " (%s)" (alist-get 'name a)) + (if-let ((info (espotify--additional-info x))) + (format " (%s)" info) "")) 'espotify-item x)) @@ -391,14 +406,12 @@ Let's start with an umbrella customization group: (let ((secs (/ d 1000))) (format "%02d:%02d" (/ secs 60) (mod secs 60))) "")) - ((or (alist-get 'name (car (alist-get 'artists x))) "") - :face 'marginalia-file-name :width 50) ((if-let (d (alist-get 'total_tracks x)) (format "%s tracks" d) "") :face 'marginalia-size :width 12) ((if-let (d (alist-get 'release_date (alist-get 'album x x))) (format "%s" d) "") - :face 'marginalia-modified :width 10)))) + :face 'marginalia-date :width 10)))) (add-to-list 'marginalia-annotators-heavy '(espotify-search-item . espotify-marginalia-annotate)) -- cgit v1.2.3