Fork me on GitHub
#nbb
<
2023-01-30
>
fenton22:01:02

loving nbb. tying to create an ink app, but struggling to get the useInput to work. Anyone have any luck getting user input in an Ink TUI? The following doesn't work:

(ns app
  (:require
   ["ink" :as ink :refer [render Text Box useInput]]))

(defonce state (r/atom {:j false
                        :k true}))

(defn handle-input [state input _]
  (println "here")
  (case input
    "j" (swap! state update :j not)
    (swap! state update :k not)))

(defn root [state]
  [useInput (partial handle-input state)]
  [:> Text (str @state)]
  )

(render (r/as-element [root state]))

(defn main-loop []
  (js/setTimeout main-loop 3000))

(main-loop)

borkdude23:01:41

@U0CGFT70T This seems to work:

(ns example
  (:require
   ["ink" :as ink :refer [render Text Box useInput]]
   [reagent.core :as r]))

(defonce state (r/atom {:j false
                        :k true}))

(defn handle-input [state input _]
  (println "here" input)
  (case input
    "j" (swap! state update :j not)
    (swap! state update :k not)))

(defn input []
  (useInput (fn [input key]
              (handle-input state input key)))
  [:> Text {:color "green"} (str @state)])

(render (r/as-element [:f> input]))

(defn main-loop []
  (js/setTimeout main-loop 3000))

(main-loop)

fenton21:01:01

@U04V15CAJ Thanks so much I'll go give that a try. Thanks for all the great work you do and sharing it... I'll see if I can send you a patreon or something?

borkdude21:01:36

@U0CGFT70T 🙏 Does Github Sponsors work for you? https://github.com/sponsors/borkdude OpenCollective is also possible https://opencollective.com/babashka Or ko-fi: https://ko-fi.com/borkdude

fenton21:01:08

done at github.