update examples. fix bugs
This commit is contained in:
+29
-27
@@ -2,6 +2,7 @@
|
||||
"List selection example - demonstrates cursor navigation and multi-select.
|
||||
Mirrors bubbletea's list examples."
|
||||
(:require [tui.core :as tui]
|
||||
[tui.events :as ev]
|
||||
[clojure.string :as str]))
|
||||
|
||||
;; === Model ===
|
||||
@@ -12,37 +13,38 @@
|
||||
:submitted false})
|
||||
|
||||
;; === Update ===
|
||||
(defn update-model [{:keys [cursor items] :as model} msg]
|
||||
(cond
|
||||
;; Quit
|
||||
(or (tui/key= msg "q")
|
||||
(tui/key= msg [:ctrl \c]))
|
||||
[model tui/quit]
|
||||
(defn update-fn [{:keys [model event]}]
|
||||
(let [{:keys [cursor items]} model]
|
||||
(cond
|
||||
;; Quit
|
||||
(or (ev/key= event \q)
|
||||
(ev/key= event \c #{:ctrl}))
|
||||
{:model model :events [(ev/quit)]}
|
||||
|
||||
;; Move up
|
||||
(or (tui/key= msg :up)
|
||||
(tui/key= msg "k"))
|
||||
[(update model :cursor #(max 0 (dec %))) nil]
|
||||
;; Move up
|
||||
(or (ev/key= event :up)
|
||||
(ev/key= event \k))
|
||||
{:model (update model :cursor #(max 0 (dec %)))}
|
||||
|
||||
;; Move down
|
||||
(or (tui/key= msg :down)
|
||||
(tui/key= msg "j"))
|
||||
[(update model :cursor #(min (dec (count items)) (inc %))) nil]
|
||||
;; Move down
|
||||
(or (ev/key= event :down)
|
||||
(ev/key= event \j))
|
||||
{:model (update model :cursor #(min (dec (count items)) (inc %)))}
|
||||
|
||||
;; Toggle selection
|
||||
(tui/key= msg " ")
|
||||
[(update model :selected
|
||||
#(if (contains? % cursor)
|
||||
(disj % cursor)
|
||||
(conj % cursor)))
|
||||
nil]
|
||||
;; Toggle selection
|
||||
(ev/key= event \space)
|
||||
{:model (update model :selected
|
||||
#(if (contains? % cursor)
|
||||
(disj % cursor)
|
||||
(conj % cursor)))}
|
||||
|
||||
;; Submit
|
||||
(tui/key= msg :enter)
|
||||
[(assoc model :submitted true) tui/quit]
|
||||
;; Submit
|
||||
(ev/key= event :enter)
|
||||
{:model (assoc model :submitted true)
|
||||
:events [(ev/quit)]}
|
||||
|
||||
:else
|
||||
[model nil]))
|
||||
:else
|
||||
{:model model})))
|
||||
|
||||
;; === View ===
|
||||
(defn view [{:keys [cursor items selected submitted]} _size]
|
||||
@@ -83,7 +85,7 @@
|
||||
(defn -main [& _args]
|
||||
(println "Starting list selection...")
|
||||
(let [{:keys [items selected submitted]} (tui/run {:init initial-model
|
||||
:update update-model
|
||||
:update update-fn
|
||||
:view view})]
|
||||
(when submitted
|
||||
(println)
|
||||
|
||||
Reference in New Issue
Block a user