Browse code

Merge pull request #49 from jbweston/feat/clear-command

Implement command 'clear'

This clears the command history

Closes #42.

Joseph Weston authored on 11/12/2021 21:32:32 • GitHub committed on 11/12/2021 21:32:32
Showing 1 changed files
... ...
@@ -16,19 +16,21 @@
16 16
  (fn-traced [db [_ current]]
17 17
   (assoc-in db [:cmdline :current] current)))
18 18
 
19
-(defn execute
20
-    [db cmdline]
21
- (let [spells (db :spells)
19
+(defn execute [db cmdline]
20
+ (let [append-history #(update-in db [:cmdline :history] conj {:input cmdline :output %})
21
+       spells (db :spells)
22 22
        equipment (db :equipment)
23 23
        cmd (keyword cmdline)]
24 24
   ; TODO: Replace with the 'match' macro
25 25
   (cond
26
-   (= "Open Game License" cmdline) (db :license)
27
-   (= "spells" cmdline) (vals spells)
28
-   (= "items" cmdline) (vals equipment)
29
-   (contains? spells cmd) (spells cmd)
30
-   (contains? equipment cmd) (equipment cmd)
31
-   :else {:err (str "Unknown command '" cmdline "'")})))
26
+   (empty? cmdline) db
27
+   (= "clear" cmdline) (assoc-in db [:cmdline :history] [])
28
+   (= "Open Game License" cmdline) (append-history (db :license))
29
+   (= "spells" cmdline) (append-history (vals spells))
30
+   (= "items" cmdline) (append-history (vals equipment))
31
+   (contains? spells cmd) (append-history (spells cmd))
32
+   (contains? equipment cmd) (append-history (equipment cmd))
33
+   :else (append-history {:err (str "Unknown command '" cmdline "'")}))))
32 34
 
33 35
 (re-frame/reg-fx
34 36
  :window/scroll-to
... ...
@@ -82,19 +84,13 @@
82 84
  ::submit-cmd
83 85
  (fn [cofx [_ prompt]]
84 86
   (let [db (cofx :db)
85
-        cmdline (some-> db :cmdline :current trim)
86
-        not-empty? seq]
87
+        cmdline (some-> db :cmdline :current trim)]
87 88
     {:db
88
-     ; TODO replace this with a "->when" macro so that we
89
-     ; don't always have to prefix with a conditional
90
-     (cond-> db
91
-      true (update-in [:cmdline :current] (constantly nil))
92
-      true (update-in [:cmdline :selected-history] (constantly nil))
93
-      (not-empty? cmdline)
94
-      (as-> db
95
-        (let [output (execute db cmdline)]
96
-            (update-in db [:cmdline :history] conj
97
-                {:input cmdline :output output}))))
89
+     (-> db
90
+      (update-in [:cmdline :current] (constantly nil))
91
+      (update-in [:cmdline :selected-history] (constantly nil))
92
+      (execute cmdline))
93
+
98 94
      ; dispatch-later to ensure that the DOM has already been updated,
99 95
      ; so that scrollMaxY has been changed appropriately.
100 96
      :fx [[:dispatch-later {:ms 10 :dispatch [::scroll-to prompt]}]]})))