fix exit app
This commit is contained in:
+8
-4
@@ -82,13 +82,16 @@
|
|||||||
;; === Input Loop ===
|
;; === Input Loop ===
|
||||||
(defn- start-input-loop!
|
(defn- start-input-loop!
|
||||||
"Start thread that reads input and puts messages on channel.
|
"Start thread that reads input and puts messages on channel.
|
||||||
Uses thread instead of go-loop because input reading is blocking I/O."
|
Uses polling with timeout to allow clean shutdown when running? becomes false."
|
||||||
[msg-chan running?]
|
[msg-chan running?]
|
||||||
(async/thread
|
(async/thread
|
||||||
(loop []
|
(loop []
|
||||||
(when @running?
|
(when @running?
|
||||||
|
(if (term/input-ready?)
|
||||||
(when-let [key-msg (input/read-key)]
|
(when-let [key-msg (input/read-key)]
|
||||||
(>!! msg-chan key-msg))
|
(>!! msg-chan key-msg))
|
||||||
|
;; No input ready, sleep briefly and check running? again
|
||||||
|
(Thread/sleep 10))
|
||||||
(recur)))))
|
(recur)))))
|
||||||
|
|
||||||
;; === Main Run Loop ===
|
;; === Main Run Loop ===
|
||||||
@@ -101,11 +104,11 @@
|
|||||||
- :view - (fn [model] hiccup) (required)
|
- :view - (fn [model] hiccup) (required)
|
||||||
- :init-cmd - Initial command to run
|
- :init-cmd - Initial command to run
|
||||||
- :fps - Target frames per second (default 60)
|
- :fps - Target frames per second (default 60)
|
||||||
- :alt-screen - Use alternate screen buffer (default false)
|
- :alt-screen - Use alternate screen buffer (default true)
|
||||||
|
|
||||||
Returns the final model."
|
Returns the final model."
|
||||||
[{:keys [init update view init-cmd fps alt-screen]
|
[{:keys [init update view init-cmd fps alt-screen]
|
||||||
:or {fps 60 alt-screen false}}]
|
:or {fps 60 alt-screen true}}]
|
||||||
(let [msg-chan (chan 256)
|
(let [msg-chan (chan 256)
|
||||||
running? (atom true)
|
running? (atom true)
|
||||||
frame-time (/ 1000 fps)]
|
frame-time (/ 1000 fps)]
|
||||||
@@ -167,7 +170,8 @@
|
|||||||
(when alt-screen (term/exit-alt-screen!))
|
(when alt-screen (term/exit-alt-screen!))
|
||||||
(term/restore!)
|
(term/restore!)
|
||||||
(term/close-input!)
|
(term/close-input!)
|
||||||
(println)))))
|
(println)
|
||||||
|
(flush)))))
|
||||||
|
|
||||||
;; === Convenience Macros ===
|
;; === Convenience Macros ===
|
||||||
(defmacro defapp
|
(defmacro defapp
|
||||||
|
|||||||
+4
-3
@@ -28,11 +28,11 @@
|
|||||||
- :init - Initial model (required)
|
- :init - Initial model (required)
|
||||||
- :update - (fn [model msg] [new-model cmd]) (required)
|
- :update - (fn [model msg] [new-model cmd]) (required)
|
||||||
- :view - (fn [model] hiccup) (required)
|
- :view - (fn [model] hiccup) (required)
|
||||||
- :alt-screen - Use alternate screen buffer (default false)
|
- :alt-screen - Use alternate screen buffer (default true)
|
||||||
|
|
||||||
Returns the final model."
|
Returns the final model."
|
||||||
[{:keys [init update view alt-screen]
|
[{:keys [init update view alt-screen]
|
||||||
:or {alt-screen false}}]
|
:or {alt-screen true}}]
|
||||||
|
|
||||||
;; Setup terminal
|
;; Setup terminal
|
||||||
(term/raw-mode!)
|
(term/raw-mode!)
|
||||||
@@ -62,7 +62,8 @@
|
|||||||
(when alt-screen (term/exit-alt-screen!))
|
(when alt-screen (term/exit-alt-screen!))
|
||||||
(term/restore!)
|
(term/restore!)
|
||||||
(term/close-input!)
|
(term/close-input!)
|
||||||
(println))))
|
(println)
|
||||||
|
(flush))))
|
||||||
|
|
||||||
;; Re-export render
|
;; Re-export render
|
||||||
(def render render/render)
|
(def render render/render)
|
||||||
|
|||||||
@@ -103,6 +103,12 @@
|
|||||||
(.close r)
|
(.close r)
|
||||||
(reset! tty-reader nil)))
|
(reset! tty-reader nil)))
|
||||||
|
|
||||||
|
(defn input-ready?
|
||||||
|
"Check if input is available without blocking."
|
||||||
|
[]
|
||||||
|
(when-let [r @tty-reader]
|
||||||
|
(.ready r)))
|
||||||
|
|
||||||
(defn read-char
|
(defn read-char
|
||||||
"Read a single character. Blocking."
|
"Read a single character. Blocking."
|
||||||
[]
|
[]
|
||||||
|
|||||||
Reference in New Issue
Block a user