Skip to content

Instantly share code, notes, and snippets.

@Bill
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bill/33df1cc70abb30964397 to your computer and use it in GitHub Desktop.
Save Bill/33df1cc70abb30964397 to your computer and use it in GitHub Desktop.
(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