/
core.clj
182 lines (153 loc) · 4.82 KB
/
core.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(ns terminal-todo-mvc.core
(:require [membrane.ui :as ui
:refer
[horizontal-layout
vertical-layout
on]]
[clojure.core.async :as async]
[membrane.lanterna
:refer [textarea checkbox label button rectangle]
:as lanterna]
[membrane.component :as component
:refer [defui defeffect]])
(:gen-class))
;;; todo app
(defui todo-item [{:keys [todo]}]
(horizontal-layout
(on
:mouse-down
(fn [[mx my]]
[[:delete $todo]])
(ui/with-color [1 0 0]
(label "X")))
(checkbox {:checked? (:complete? todo)})
(ui/wrap-on
:key-press
(fn [default-handler s]
(when (not= s :enter)
(default-handler s)))
(textarea {:text (:description todo)}))))
(comment
(run-ui #'todo-item {:todo
{:complete? false
:description "fix me"}}))
(defui todo-list [{:keys [todos]}]
(apply
vertical-layout
(for [todo todos]
(todo-item {:todo todo}))))
(comment
(run-ui #'todo-list {:todos
[{:complete? false
:description "first"}
{:complete? false
:description "second"}
{:complete? true
:description "third"}]}))
(def filter-fns
{:all (constantly true)
:active (comp not :complete?)
:complete? :complete?})
;; Create a toggle that allows the user
;; to toggle between options
(defui toggle [{:keys [options selected]}]
(apply
horizontal-layout
(for [option options]
(if (= option selected)
(label (name option))
(on
:mouse-down
(fn [[mx my]]
[[:set $selected option]])
(ui/with-color [0.8 0.8 0.8]
(label (name option))))))))
(comment
(run-ui #'toggle
{:options [:all :active :complete?]
:selected nil}))
(defui todo-app [{:keys [todos next-todo-text selected-filter size]
:or {selected-filter :all}}]
(on
:terminal-resized
(fn [new-size]
[[:set $size new-size]])
(vertical-layout
(label (str "size: "size))
(horizontal-layout
(button "Add Todo"
(fn []
[[::add-todo $todos next-todo-text]
[:set $next-todo-text ""]]))
(ui/wrap-on
:key-press
(fn [default-handler s]
(let [effects (default-handler s)]
(if (and (seq effects)
(= s :enter))
[[::add-todo $todos next-todo-text]
[:set $next-todo-text ""]]
effects)))
(textarea {:text next-todo-text})))
(toggle {:selected selected-filter
:options [:all :active :complete?]})
(let [filter-fn (get filter-fns selected-filter :all)
visible-todos (filter filter-fn todos)]
(todo-list {:todos visible-todos})))))
(def todo-state (atom {:todos
[{:complete? false
:description "first"}
{:complete? false
:description "second"}
{:complete? true
:description "third"}]
:next-todo-text ""}))
(defeffect ::add-todo [$todos next-todo-text]
(dispatch! :update $todos #(conj % {:description next-todo-text
:complete? false})))
(comment
(run-ui #'todo-app todo-state
))
(comment
(def todo-state
(run-ui #'todo-app {:todos
[{:complete? false
:description "first"}
{:complete? false
:description "second"}
{:complete? true
:description "third"}]
:next-todo-text ""})))
(def todo-state (atom {:todos
[{:complete? false
:description "first"}
{:complete? false
:description "second"}
{:complete? true
:description "third"}]
:next-todo-text ""}))
(defn bordered-box [title body]
(let [body (ui/padding 1 1
body)
[w h] (ui/bounds body)
w (max (+ 2 (count title))
w)]
[(rectangle (inc w) (inc h))
(ui/translate 2 0 (label title))
body]))
(comment
(do
(def close-ch (async/chan))
(lanterna/run (component/make-app #'todo-app todo-state)
{:in membrane.lanterna/in
:out membrane.lanterna/out
:close-ch close-ch
}))
(async/close! close-ch)
,)
(defn -main [& args]
(lanterna/run-sync (component/make-app #'todo-app todo-state))
;; (component/run-ui-sync )
;; (.close System/in)
;; (shutdown-agents)
)