diff options
author | Thanos Apollo <[email protected]> | 2023-12-25 04:29:55 +0200 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2023-12-25 04:31:19 +0200 |
commit | db4b1be571a29291cfb7a635ed8156f31040e621 (patch) | |
tree | 66018e9d39d24f36b958caa42a491518c0073793 /gnosis.el | |
parent | 59b4f5a25e95904c60cbf08969e1a2bc823120bd (diff) |
Add gnosis--prompt
Replace old gnosis--ask-input with new prompt function.
- Uses cl-loop heavily but it's clean to read, at least compared to
other pure emacs lisp code I've tried before
- Optionally downcase & split string input, used for tags
Diffstat (limited to 'gnosis.el')
-rw-r--r-- | gnosis.el | 27 |
1 files changed, 16 insertions, 11 deletions
@@ -84,17 +84,22 @@ Example: (insert question) (sit-for 0.5)))) -(defun gnosis--ask-input (prompt) +(cl-defun gnosis--prompt (prompt &optional (downcase nil) (split nil)) "PROMPT user for input until `q' is given. -The user is prompted to provide input for the 'PROMPT' message, and -the returns the list of inputs in reverse order." - (let ((input nil)) - (while (not (equal (car input) "q")) - (push (read-string (concat prompt " (q for quit): ")) input)) - (when (equal (car input) "q") - (pop input)) - (reverse input))) +The user is prompted to provide input for the 'PROMPT' message. +Returns the list of non-'q' inputs in reverse order of their entry. + +Set DOWNCASE to t to downcase all input given. +Set SPLIT to t to split all input given." + (cl-loop with input = nil + for response = (read-string (concat prompt " (q for quit): ")) + do (if downcase (setq response (downcase response))) + for response-parts = (if split (split-string response " ") (list response)) + if (member "q" response-parts) return (nreverse input) + do (cl-loop for part in response-parts + unless (string-empty-p part) + do (push part input)))) (defun gnosis-add-deck (name) "Create deck with NAME." @@ -132,11 +137,11 @@ TAGS are used to organize questions." (interactive (list :deck (gnosis--get-deck-id) :question (read-string "Question: ") - :choices (gnosis--ask-input "Choices") + :choices (gnosis--prompt "Choices") ;; NOTE: string-to-number transforms non-number strings to 0 :correct-answer (string-to-number (read-string "Which is the correct answer (number)? ")) :extra (read-string "Extra: ") - :tags (gnosis--ask-input "Tags"))) + :tags (gnosis--prompt "Tags" t t))) (cond ((or (not (numberp correct-answer)) (equal correct-answer 0)) (error "Correct answer value must be the index number of the correct answer")) ((null tags) |