Skip to content

Commit

Permalink
Update API call to get templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jupl committed Feb 11, 2017
1 parent 03aad6e commit 2a2e7da
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions helm-gitignore.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;; helm-gitignore.el --- Generate .gitignore files with gitignore.io.
;;
;; Author: Juan Placencia
;; Version: 0.1.0
;; Version: 0.2.0
;; Keywords: helm gitignore gitignore.io
;; Homepage: https://github.com/jupl/helm-gitignore
;; Package-Requires: ((gitignore-mode "1.1.0") (helm "1.7.0") (request "0.1.0") (cl-lib "0.5"))
Expand All @@ -24,16 +24,43 @@
"https://www.gitignore.io/api/%s"
"Url used to generate .gitignore file.")

(defvar helm-gitignore--candidates nil
"Cached list of available candidates for helm window.")

(defvar helm-gitignore--list-url
"https://www.gitignore.io/dropdown/templates.json"
"https://www.gitignore.io/dropdown/templates.json?term=%s"
"Url used to get list of templates in raw and human-friendly text.")

(defvar helm-gitignore--source '((name . "gitignore.io")
(action . helm-gitignore--action)
(candidates . helm-gitignore--candidates)))
(defvar helm-gitignore--cache nil
"Cache that contains results of querying of candidates from gitignore.io.")

(defvar helm-gitignore--source
(helm-build-sync-source "gitignore.io"
:candidates 'helm-gitignore--candidates
:action 'helm-gitignore--action
:volatile t
:requires-pattern 1))

(defun helm-gitignore--candidates ()
"Query for gitignore templates."
(let ((delay 0.1))
(unless helm-gitignore--cache
(run-at-time delay nil 'helm-gitignore--query-candidates helm-pattern))
(and (sit-for delay)
(prog1 helm-gitignore--cache
(and helm-gitignore--cache
(setq helm-gitignore--cache nil))))))

(defun helm-gitignore--query-candidates (pattern)
"Query for gitignore templates using given PATTERN."
(request
(format helm-gitignore--list-url pattern)
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(setq helm-gitignore--cache
(mapcar 'helm-gitignore--format-result data))
(and helm-alive-p (helm-update))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys&rest _)
(message error-thrown)))))

(defun helm-gitignore--action (candidate)
"Generate .gitignore given at least a CANDIDATE and present to screen."
Expand All @@ -57,34 +84,18 @@

(defun helm-gitignore--format-result (result)
"Pluck data from RESULT to create a Helm candidate."
(cons (assoc-default 'text result) (assoc-default 'id result)))
(cons (assoc-default 'text result) (assoc-default 'id result)))

(defun helm-gitignore--generate-url (list)
"Create url from LIST to generate .gitignore using gitignore.io."
(format helm-gitignore--api-url (mapconcat 'identity list ",")))

(defun helm-gitignore--start ()
"Actually start helm-gitignore assuming candidates are fetched."
(helm :sources 'helm-gitignore--source
:buffer "*helm-gitignore*"))

;;;###autoload
(defun helm-gitignore ()
"Helm to generate .gitignore using gitignore.io."
(interactive)
(if helm-gitignore--candidates
(helm-gitignore--start)
(request
helm-gitignore--list-url
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(setq helm-gitignore--candidates
(mapcar 'helm-gitignore--format-result data))
(helm-gitignore--start)))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys&rest _)
(message error-thrown))))))
(helm :sources 'helm-gitignore--source
:buffer "*helm-gitignore*"))

(provide 'helm-gitignore)

Expand Down

0 comments on commit 2a2e7da

Please sign in to comment.