fix exit app

This commit is contained in:
2026-01-21 10:51:45 -05:00
parent b14ba33c3a
commit 9fe0ac2c6e
3 changed files with 20 additions and 9 deletions
+8 -4
View File
@@ -82,13 +82,16 @@
;; === Input Loop ===
(defn- start-input-loop!
"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?]
(async/thread
(loop []
(when @running?
(if (term/input-ready?)
(when-let [key-msg (input/read-key)]
(>!! msg-chan key-msg))
;; No input ready, sleep briefly and check running? again
(Thread/sleep 10))
(recur)))))
;; === Main Run Loop ===
@@ -101,11 +104,11 @@
- :view - (fn [model] hiccup) (required)
- :init-cmd - Initial command to run
- :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."
[{: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)
running? (atom true)
frame-time (/ 1000 fps)]
@@ -167,7 +170,8 @@
(when alt-screen (term/exit-alt-screen!))
(term/restore!)
(term/close-input!)
(println)))))
(println)
(flush)))))
;; === Convenience Macros ===
(defmacro defapp
+4 -3
View File
@@ -28,11 +28,11 @@
- :init - Initial model (required)
- :update - (fn [model msg] [new-model cmd]) (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."
[{:keys [init update view alt-screen]
:or {alt-screen false}}]
:or {alt-screen true}}]
;; Setup terminal
(term/raw-mode!)
@@ -62,7 +62,8 @@
(when alt-screen (term/exit-alt-screen!))
(term/restore!)
(term/close-input!)
(println))))
(println)
(flush))))
;; Re-export render
(def render render/render)
+6
View File
@@ -103,6 +103,12 @@
(.close r)
(reset! tty-reader nil)))
(defn input-ready?
"Check if input is available without blocking."
[]
(when-let [r @tty-reader]
(.ready r)))
(defn read-char
"Read a single character. Blocking."
[]