Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns progress-light.core
(:require [clj-progress.core :as progress]
[clojure.core.async :as async :refer [>!! timeout chan alt!! thread]])
(:gen-class))
(def tick-ch
(chan))
(def done-ch
(chan))
(defn monitor-progress
([max-ticks]
"monitor up to max-ticks; inform every 1000 milliseconds (i.e. 1 second)"
(monitor-progress max-ticks 1000))
([max-ticks inform-every]
"monitior up to max-ticks; inform every progress-every milliseconds"
(progress/init "Processing" max-ticks)
(thread (loop [progress 0
timer (timeout inform-every)]
(alt!!
tick-ch (recur (inc progress) timer)
done-ch (progress/done) ;; and exit thread
timer (do (when (pos? progress) ;; can't tick-to 0
(progress/tick-to progress))
(recur progress (timeout inform-every))))))))
(defn tick [& [obj]] (>!! tick-ch true) obj)
(defn done [& [obj]] (>!! done-ch true) obj)
(defn -main [& args]
(monitor-progress 1000)
(dotimes [n 1000] (Thread/sleep 10) (tick))
(done)
(System/exit 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.