+pass/ivy: fix open-url; add +pass-get-field (#103)

This commit is contained in:
Henrik Lissner 2017-06-12 02:38:16 +02:00
parent 8c3cf83c6c
commit 77298f3672
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 41 additions and 25 deletions

View file

@ -11,8 +11,23 @@
(pass)))) (pass))))
(after! ivy ;;;###autoload
(defun +pass-ivy-action--get-field (item) (defun +pass-get-field (entry fields)
(if-let (data (if (listp entry) entry (auth-pass-parse-entry entry)))
(cl-loop for key in (doom-enlist fields)
when (assoc key data)
return (cdr it))
(error "Couldn't find entry: %s" entry)))
(defun +pass-ivy-action--open-url (entry)
(if-let (url (+pass-get-field entry +pass-url-fields))
(and (or (string-prefix-p "http://" url)
(string-prefix-p "https://" url)
(error "Field for %s doesn't look like an url" item))
(browse-url url))
(error "Username not found.")))
(defun +pass-ivy-action--get-field (item)
(let* ((data (auth-pass-parse-entry item)) (let* ((data (auth-pass-parse-entry item))
(field (if data (completing-read "Field: " (mapcar #'car data) nil t)))) (field (if data (completing-read "Field: " (mapcar #'car data) nil t))))
(if data (if data
@ -20,27 +35,25 @@
(password-store-clear) (password-store-clear)
(message "Copied %s's %s field to clipboard. Will clear in %s seconds" (message "Copied %s's %s field to clipboard. Will clear in %s seconds"
item field (password-store-timeout)) item field (password-store-timeout))
(kill-new (cdr (assoc field data))) (kill-new (+pass-get-field data field))
(setq password-store-timeout-timer (setq password-store-timeout-timer
(run-at-time (password-store-timeout) nil 'password-store-clear))) (run-at-time (password-store-timeout) nil 'password-store-clear)))
(error "Couldn't find entry: %s" item)))) (error "Couldn't find entry: %s" item))))
(defun +pass-ivy-action--copy-username (item) (defun +pass-ivy-action--copy-username (entry)
(if-let (user (cl-loop with data = (auth-pass-parse-entry item) (if-let (user (+pass-get-field entry +pass-user-fields))
for key in +pass-user-fields
when (assoc key data)
return (cdr it)))
(progn (password-store-clear) (progn (password-store-clear)
(message "Copied username to the kill ring.") (message "Copied username to the kill ring.")
(kill-new user)) (kill-new user))
(message "Username not found!"))) (error "Username not found.")))
(after! ivy
(ivy-add-actions (ivy-add-actions
'+pass/ivy '+pass/ivy
'(("o" password-store-copy "copy password") '(("o" password-store-copy "copy password")
("u" +pass-ivy-action--copy-username "copy username")
("e" password-store-edit "edit entry") ("e" password-store-edit "edit entry")
("b" password-store-url "open url in browser") ("u" +pass-ivy-action--copy-username "copy username")
("b" +pass-ivy-action--open-url "open url in browser")
("f" +pass-ivy-action--get-field "get field")))) ("f" +pass-ivy-action--get-field "get field"))))
;;;###autoload ;;;###autoload

View file

@ -3,6 +3,9 @@
(defvar +pass-user-fields '("login" "user" "username" "email") (defvar +pass-user-fields '("login" "user" "username" "email")
"A list of fields for `+pass/ivy' to search for the username.") "A list of fields for `+pass/ivy' to search for the username.")
(defvar +pass-url-fields '("url" "site" "location")
"A list of fields for `+pass/ivy' to search for the username.")
;; ;;
;; Plugins ;; Plugins