Liking cljdoc? Tell your friends :D

Text Input

Text entry component with cursor movement, editing, and multiple echo modes.

Quick Example

(require '[charm.core :as charm])

(def my-input (charm/text-input :prompt "Name: "
                                :placeholder "Enter your name"))

;; In update function
(let [[input cmd] (charm/text-input-update my-input msg)]
  ;; Handle key presses
  )

;; In view function
(charm/text-input-view my-input)  ; => "Name: |Enter your name"

Creation Options

(charm/text-input & options)
OptionTypeDefaultDescription
:promptstring"> "Text displayed before input
:placeholderstringnilText shown when empty
:valuestring""Initial value
:echo-modekeyword:normal:normal, :password, or :none
:echo-charchar\*Character for password mode
:char-limitint0Max characters (0 = unlimited)
:widthint0Display width (0 = unlimited)
:focusedbooleantrueStart focused
:prompt-stylestylenilStyle for prompt
:text-stylestylenilStyle for input text
:placeholder-stylestylegrayStyle for placeholder
:cursor-stylestylereverseStyle for cursor
:idanyrandomUnique identifier

Echo Modes

;; Normal - shows typed text
(charm/text-input :echo-mode charm/echo-normal)

;; Password - shows asterisks
(charm/text-input :echo-mode charm/echo-password)
(charm/text-input :echo-mode charm/echo-password :echo-char \●)

;; None - hides all input
(charm/text-input :echo-mode charm/echo-none)

Key Bindings

ActionKeys
Move leftLeft, Ctrl+B
Move rightRight, Ctrl+F
Word leftAlt+Left, Ctrl+Left, Alt+B
Word rightAlt+Right, Ctrl+Right, Alt+F
Line startHome, Ctrl+A
Line endEnd, Ctrl+E
Delete char leftBackspace, Ctrl+H
Delete char rightDelete, Ctrl+D
Delete word leftAlt+Backspace, Ctrl+W
Delete word rightAlt+Delete, Alt+D
Delete to startCtrl+U
Delete to endCtrl+K

Functions

text-input-update

(charm/text-input-update input msg) ; => [input cmd]

Handle key messages. Only processes input when focused.

text-input-view

(charm/text-input-view input) ; => "Name: John|"

Render the input with prompt and cursor.

text-input-value

(charm/text-input-value input) ; => "John"

Get current value as a string.

text-input-focus / text-input-blur

(charm/text-input-focus input)  ; Focus the input
(charm/text-input-blur input)   ; Unfocus the input

text-input-reset

(charm/text-input-reset input)  ; Clear the value

text-input-set-value

(charm/text-input-set-value input "new value")

Set the value and move cursor to end.

Full Example

(ns my-app
  (:require [charm.core :as charm]))

(defn init []
  [{:username (charm/text-input :prompt "Username: " :focused true)
    :password (charm/text-input :prompt "Password: "
                                :echo-mode charm/echo-password
                                :focused false)
    :current :username}
   nil])

(defn update-fn [state msg]
  (cond
    (charm/key-match? msg "tab")
    (let [next-field (if (= :username (:current state)) :password :username)]
      [(-> state
           (update :username (if (= next-field :username)
                               charm/text-input-focus
                               charm/text-input-blur))
           (update :password (if (= next-field :password)
                               charm/text-input-focus
                               charm/text-input-blur))
           (assoc :current next-field))
       nil])

    (charm/key-match? msg "enter")
    ;; Submit form
    [state charm/quit-cmd]

    :else
    (let [field (:current state)
          [input cmd] (charm/text-input-update (get state field) msg)]
      [(assoc state field input) cmd])))

(defn view [state]
  (str (charm/text-input-view (:username state)) "\n"
       (charm/text-input-view (:password state)) "\n\n"
       "Tab to switch fields, Enter to submit"))

(charm/run {:init init :update update-fn :view view})

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close