... | ... |
@@ -40,30 +40,43 @@ |
40 | 40 |
(fn [_ [_ x]] |
41 | 41 |
{:window/scroll-to x})) |
42 | 42 |
|
43 |
-(defn- up [x] |
|
44 |
- (case x |
|
45 |
- nil 0 |
|
46 |
- (inc x))) |
|
47 |
- |
|
48 |
-(defn- down [x] |
|
49 |
- (case x |
|
50 |
- nil nil |
|
51 |
- 0 nil |
|
52 |
- (dec x))) |
|
43 |
+(defn- cycle-history [cs direction] |
|
44 |
+ (let [history (-> cs :history) |
|
45 |
+ current (-> cs :current) |
|
46 |
+ orig (some-> cs :selected-history :orig) |
|
47 |
+ idx (some-> cs :selected-history :index) |
|
48 |
+ history-end (- (count history) 1) |
|
49 |
+ next (case direction :back dec :fwd inc) |
|
50 |
+ at-first? (= idx history-end) |
|
51 |
+ at-last? (= idx 0) |
|
52 |
+ going-forward? (= direction :fwd) |
|
53 |
+ going-backward? (= direction :back) |
|
54 |
+ not-cycling? (-> cs :selected-history nil?)] |
|
55 |
+ (cond |
|
56 |
+ (empty? history) {:current current :selected-history nil} |
|
57 |
+ (and at-first? going-forward?) {:current orig :selected-history nil} |
|
58 |
+ (and at-last? going-backward?) {} ; do nothing |
|
59 |
+ (and not-cycling? going-forward?) {} ; do nothing |
|
60 |
+ (and not-cycling? going-backward?) {:current (-> history (get history-end) :input) |
|
61 |
+ :selected-history {:orig current |
|
62 |
+ :index history-end}} |
|
63 |
+ :else {:current (-> history (get (next idx)) :input) |
|
64 |
+ :selected-history {:orig orig |
|
65 |
+ :index (next idx)}}))) |
|
53 | 66 |
|
54 | 67 |
(re-frame/reg-event-db |
55 | 68 |
::prompt-keypress |
56 |
- (fn [db [_ x]] |
|
57 |
- (case (.-key x) |
|
58 |
- "ArrowUp" |
|
59 |
- (do (.preventDefault x) |
|
60 |
- (println "up") |
|
61 |
- (update-in db [:cmdline :selected-history] up)) |
|
62 |
- "ArrowDown" |
|
63 |
- (do (.preventDefault x) |
|
64 |
- (println "down") |
|
65 |
- (update-in db [:cmdline :selected-history] down)) |
|
66 |
- nil))) |
|
69 |
+ (re-frame/path :cmdline) |
|
70 |
+ (fn [cmd [_ event]] |
|
71 |
+ (let [ |
|
72 |
+ direction (case (.-key event) |
|
73 |
+ "ArrowUp" :back |
|
74 |
+ "ArrowDown" :fwd |
|
75 |
+ nil)] |
|
76 |
+ (if direction |
|
77 |
+ (do (.preventDefault event) |
|
78 |
+ (merge cmd (cycle-history cmd direction))) |
|
79 |
+ cmd)))) |
|
67 | 80 |
|
68 | 81 |
(re-frame/reg-event-fx |
69 | 82 |
::submit-cmd |