Liking cljdoc? Tell your friends :D

List

Scrollable list component with keyboard navigation and item selection.

Quick Example

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

(def my-list (charm/item-list ["Apple" "Banana" "Cherry"]))

;; In update function
(let [[list cmd] (charm/list-update my-list msg)]
  ;; Handle navigation
  )

;; In view function
(charm/list-view my-list)
;; > Apple
;;   Banana
;;   Cherry

Creation Options

(charm/item-list items & options)
OptionTypeDefaultDescription
itemsvectorrequiredItems to display
:heightint0Visible height (0 = show all)
:widthint0Width constraint (0 = unlimited)
:cursorint0Initial selected index
:titlestringnilOptional list title
:show-titlebooleantrueShow title if provided
:cursor-prefixstring"> "Prefix for selected item
:item-prefixstring" "Prefix for unselected items
:show-descriptionsbooleanfalseShow item descriptions
:infinite-scrollbooleanfalseWrap around at ends
:cursor-stylestylecyan+boldStyle for selected item
:item-stylestylenilStyle for unselected items
:title-stylestyleboldStyle for title
:idanyrandomUnique identifier

Item Formats

Items can be strings or maps:

;; Simple strings
(charm/item-list ["Apple" "Banana" "Cherry"])

;; Maps with title and description
(charm/item-list [{:title "Apple" :description "A red fruit"}
                  {:title "Banana" :description "A yellow fruit"}]
                 :show-descriptions true)

;; Maps with custom keys
(charm/item-list [{:name "Alice" :role "Admin"}
                  {:name "Bob" :role "User"}])
;; Uses :name as title, :role as description

Key Bindings

ActionKeys
Move upUp, k, Ctrl+P
Move downDown, j, Ctrl+N
Page upPgUp, Ctrl+U
Page downPgDn, Ctrl+D
Go to startHome, g
Go to endEnd, G

Functions

list-update

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

Handle navigation messages.

list-view

(charm/list-view list) ; => "> Apple\n  Banana\n  Cherry"

Render the list as a string.

list-selected-item

(charm/list-selected-item list) ; => "Apple" or {:title "Apple" ...}

Get the currently selected item.

list-selected-index

(charm/list-selected-index list) ; => 0

Get the index of the selected item.

list-set-items

(charm/list-set-items list new-items)

Replace all items, adjusting cursor if needed.

list-select

(charm/list-select list 2)  ; Select item at index 2

Navigation Functions

(charm/list-cursor-up list)
(charm/list-cursor-down list)
(charm/list-page-up list)
(charm/list-page-down list)
(charm/list-go-to-start list)
(charm/list-go-to-end list)

Full Example

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

(def items
  [{:title "New File" :description "Create a new file"}
   {:title "Open File" :description "Open an existing file"}
   {:title "Save" :description "Save current file"}
   {:title "Exit" :description "Quit the application"}])

(defn init []
  [{:menu (charm/item-list items
                           :height 10
                           :show-descriptions true
                           :cursor-style (charm/style :fg charm/yellow :bold true))}
   nil])

(defn update-fn [state msg]
  (cond
    (charm/key-match? msg "enter")
    (let [selected (charm/list-selected-item (:menu state))]
      (println "Selected:" (:title selected))
      (if (= "Exit" (:title selected))
        [state charm/quit-cmd]
        [state nil]))

    (charm/key-match? msg "q")
    [state charm/quit-cmd]

    :else
    (let [[menu cmd] (charm/list-update (:menu state) msg)]
      [(assoc state :menu menu) cmd])))

(defn view [state]
  (str "Select an option:\n\n"
       (charm/list-view (:menu state))
       "\n\nEnter to select, q to quit"))

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

Scrolling

When :height is set, the list scrolls to keep the cursor visible:

(charm/item-list (range 100)
                 :height 10)  ; Shows 10 items, scrolls with cursor

Filtering

;; Filter items by predicate
(charm/list-filter-items list #(str/includes? (:title %) "search"))

;; Find and select first match
(charm/list-select-first-match list #(= "target" (:title %)))

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