Skip to content

Commit

Permalink
Annotations in completion
Browse files Browse the repository at this point in the history
Showing file name, language, score and movie/show name, for easier
selection.
  • Loading branch information
danielfleischer committed May 1, 2024
1 parent 2e3d55f commit 22cc8ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Set up an account at [[https://www.opensubtitles.com/en/home][opensubtitles.com]
=opensub-languages= variable.

To run, call ~M-x opensub~, provide a query string such as *matrix* or *office s04e13*. Select the relevant file among the
options, and it will be downloaded to =opensub-download-directory=. See example:
options, and it will be downloaded to =opensub-download-directory=. Columns include subtitle name, language, score and
movie/show name. See example:

[[./image.png]]

Expand Down
Binary file modified image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 21 additions & 6 deletions opensub.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
"Languages to search for; 2 letter codes."
:type '(repeat string))

(defcustom opensub-column-width 72
"Width of the column in the completion list."
:type 'integer)

(defvar opensub--json-fn
(if (version<= "27.1" emacs-version)
(lambda () (json-parse-buffer :object-type 'alist))
Expand Down Expand Up @@ -81,19 +85,30 @@
clean-query)))
(opensub--curl url)))

(defun opensub--annotation (cand)
(let* ((rest (cdr (assoc cand minibuffer-completion-table))))
(concat " "
(propertize (alist-get 'language rest) 'face 'success) " "
(propertize (string-pad (alist-get 'rating rest) 4) 'face 'warning) " "
(propertize (alist-get 'full_name rest)))))

(defun opensub--get-file-id (results)
"Given query RESULTS, return the file id of user-selected item."
(let* ((items (alist-get 'data results))
(completion-extra-properties (list :annotation-function 'opensub--annotation))
(options (cl-mapcar
(lambda (item)
(concat
(let-alist item .attributes.language) ": "
(let-alist item .attributes.release)))
`(,(substring (string-pad (let-alist item .attributes.release)
opensub-column-width ? )
0 opensub-column-width)
. ((language . ,(let-alist item .attributes.language))
(rating . ,(number-to-string (let-alist item .attributes.ratings)))
(year . ,(number-to-string (let-alist item .attributes.feature_details.year)))
(full_name . ,(let-alist item .attributes.feature_details.movie_name)))))
items))
(option (completing-read "Select: " options))
(option (string-trim-right (completing-read "Select: " options)))
(item (cl-find-if (lambda (x)
(string-suffix-p (let-alist x .attributes.release)
option))
(string-search option (let-alist x .attributes.release)))
items)))
(alist-get 'file_id (aref (let-alist item .attributes.files) 0))))

Expand Down

0 comments on commit 22cc8ef

Please sign in to comment.