Liking cljdoc? Tell your friends :D

Progress Bar

Progress indicator with 7 built-in bar styles.

Quick Example

(require '[charm.components.progress :as progress])

(def my-bar (progress/progress-bar :width 40))

;; Update progress (0.0 to 1.0)
(def updated-bar (progress/set-progress my-bar 0.5))

;; In view function
(progress/progress-view updated-bar)  ; => "████████████████████░░░░░░░░░░░░░░░░░░░░"

Creation Options

(progress/progress-bar & options)
OptionTypeDefaultDescription
:widthint40Total width in characters
:percentfloat0.0Initial progress (0.0-1.0)
:bar-stylekeyword/map:defaultBar style name or custom map
:show-percentbooleanfalseShow percentage text
:full-stylestylecyanStyle for filled portion
:empty-stylestylenilStyle for empty portion
:percent-stylestylenilStyle for percentage text
:idanyrandomUnique identifier

Bar Styles

StyleFullEmptyPreview
:default████░░░░
:ascii#-####----
:thin━━━━────
:thick████▒▒▒▒
:blocks▓▓▓▓░░░░
:arrows>(space)>>>>
:dots●●●●○○○○
:brackets=(space)[==== ]

Custom Bar Style

(progress/progress-bar :bar-style {:full "▰"
                                   :empty "▱"})

;; With brackets
(progress/progress-bar :bar-style {:full "="
                                   :empty " "
                                   :left "["
                                   :right "]"})

Functions

progress-view

(progress/progress-view bar) ; => "████████░░░░░░░░"

Render the progress bar as a string.

set-progress

(progress/set-progress bar 0.75) ; Set to 75%

Set progress as a float from 0.0 to 1.0.

set-progress-int

(progress/set-progress-int bar 75) ; Set to 75%

Set progress as an integer from 0 to 100.

increment / decrement

(progress/increment bar)      ; +1%
(progress/increment bar 0.05) ; +5%
(progress/decrement bar 0.1)  ; -10%

percent

(progress/percent bar)     ; => 0.75 (float)
(progress/percent-int bar) ; => 75 (int)

complete?

(progress/complete? bar) ; => true if >= 100%

reset

(progress/reset bar) ; Reset to 0%

Full Example

(ns my-app
  (:require
   [charm.components.progress :as progress]
   [charm.message :as msg]
   [charm.program :as program]))

(defn tick-cmd []
  {:type :cmd
   :fn (fn []
         (Thread/sleep 100)
         {:type :tick})})

(defn init []
  [{:bar (progress/progress-bar :width 50
                                :bar-style :default
                                :show-percent true)
    :running false}
   nil])

(defn update-fn [state msg]
  (cond
    (msg/key-match? msg "q")
    [state program/quit-cmd]

    (msg/key-match? msg " ")
    [(assoc state :running (not (:running state)))
     (when-not (:running state) (tick-cmd))]

    (= :tick (:type msg))
    (let [bar (progress/increment (:bar state) 0.02)]
      (if (progress/complete? bar)
        [(assoc state :bar bar :running false) nil]
        [(assoc state :bar bar)
         (when (:running state) (tick-cmd))]))

    :else
    [state nil]))

(defn view [state]
  (str "Download Progress\n\n"
       (progress/progress-view (:bar state)) "\n\n"
       (if (progress/complete? (:bar state))
         "Complete!"
         (if (:running state)
           "Downloading... (Space to pause)"
           "Press Space to start, Q to quit"))))

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

Styled Progress Bar

(progress/progress-bar :width 40
                       :bar-style :default
                       :show-percent true
                       :full-style (style/style :fg style/green)
                       :empty-style (style/style :fg 240)
                       :percent-style (style/style :fg style/yellow :bold true))

Multiple Progress Bars

(defn view [state]
  (str "File 1: " (progress/progress-view (:bar1 state)) "\n"
       "File 2: " (progress/progress-view (:bar2 state)) "\n"
       "File 3: " (progress/progress-view (:bar3 state))))

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