-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathhelp.clj
71 lines (60 loc) · 1.82 KB
/
help.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(ns compojure.api.help
(:require [schema.core :as s]
[clojure.string :as str]))
(def Topic (s/maybe s/Keyword))
(def Subject (s/maybe (s/cond-pre s/Str s/Keyword s/Symbol)))
;;
;; content formatting
;;
(defn text [& s]
(->> s
(map #(if (seq? %) (apply text %) %))
(str/join "\n")))
(defn title [& s]
(str "\u001B[32m" (text s) "\u001B[0m"))
(defn code [& s]
(str "\u001B[33m" (text s) "\u001B[0m"))
(defmulti help-for (fn [topic subject] [topic subject]) :default ::default)
(defn- subject-text [topic subject]
(text
""
(title subject)
""
(help-for topic subject)
""))
(defn- topic-text [topic]
(let [subjects (-> (methods help-for)
(dissoc ::default)
(keys)
(->> (filter #(-> % first (= topic)))))]
(text
"Topic:\n"
(title topic)
"\nSubjects:"
(->> subjects
(map (partial apply subject-text))
(map (partial str "\n"))))))
(defn- help-text []
(let [methods (dissoc (methods help-for) ::default)]
(text
"Usage:"
""
(code
"(help)"
"(help topic)"
"(help topic subject)")
"\nTopics:\n"
(title (->> methods keys (map first) (distinct) (sort)))
"\nTopics & subjects:\n"
(title (->> methods keys (map (partial str/join " ")) (sort))))))
(defmethod help-for ::default [_ _] (help-text))
(s/defn ^:always-validate help
([]
(println "------------------------------------------------------------")
(println (help-text)))
([topic :- Topic]
(println "------------------------------------------------------------")
(println (topic-text topic)))
([topic :- Topic, subject :- Subject]
(println "------------------------------------------------------------")
(println (subject-text topic subject))))