Skip to content

Commit

Permalink
Merge pull request #24 from hlship/hls/20241127-job-status-demo
Browse files Browse the repository at this point in the history
Add the job-status-demo
  • Loading branch information
hlship authored Nov 27, 2024
2 parents 72dc1e6 + 6949854 commit 9ba0943
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,15 @@ This handles invalid input gracefully:
Error in flow kill-port: PORT: Not a number
```

You might be tempted to use `#(Long/parseLong %)` as the parse function; this works, but the message produced comes from the exception message, and is not very friendly:
You might be tempted to use `#(Long/parseLong %)` as the parse function; this works, but the message produced comes from
the message of the thrown exception, and is not very friendly:

```
> flow kill-port abc
Error in flow kill-port: PORT: Error in PORT: For input string: "abc"
```

## Job Board
## Job Board (experimental)

For tools that run for a while, visual feedback can be provided to the user using the _job board_
in the `net.lewisship.cli-tools.job-status` namespace.
Expand All @@ -494,6 +495,8 @@ The job board updates status lines as they change, and highlights lines that hav

This is built on the `tput` command line tool, so it works on OS X and Linux, but **not on Windows**.

The above `job-status-demo`, like `colors`, can be added by including the `net.lewisship.cli-tools.job-status-demo` namespace.


## License

Expand Down
71 changes: 71 additions & 0 deletions src/net/lewisship/cli_tools/job_status_demo.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
(ns net.lewisship.cli-tools.job-status-demo
{:command-ns 'net.lewisship.cli-tools.builtins}
(:require [net.lewisship.cli-tools :refer [defcommand] :as c]
[net.lewisship.cli-tools.job-status :as j]))

(defn sleep
([ms] (Thread/sleep ms))
([job-id ms]
(Thread/sleep ms)
job-id))

(defn thread*
[f]
(let [*complete (promise)
thread (Thread. (fn []
(deliver *complete (f))))]
(doto thread
(.setDaemon true)
.start)

*complete))

(defmacro thread
[& body]
`(thread* (fn [] (do ~@body))))

(defn done
[job-id]
(-> job-id
j/complete))

(defn simple-job
[summary delay]
(thread (-> (j/new-job)
(j/summary (str summary " ..."))
(sleep delay)
(j/status :success)
(j/summary (str summary " \u2713 "))
done)))

(defn progress-job
[summary target delay]
(let [job (-> (j/new-job)
(j/summary [{:width 30} summary])
(j/start-progress target))]
(dotimes [_ target]
(sleep delay)
(j/tick job))

(-> job
(j/complete-progress)
(j/status :success)
done)))

(defcommand job-status-demo
"Demo the use of the job status board."
[]
(j/start-board)
(let [futures [(thread (simple-job "Atomic turbines to speed" 2000))
(thread (sleep 500)
(progress-job "Loading Bat-fuel" 15 250))
(thread (sleep 1000)
(progress-job "Rotating Batmobile platform" 180 10))
(thread (simple-job "Initializing on-board Bat-computer" 1000))]]
(run! deref futures))
(-> (j/new-job)
(j/status :warning)
(j/summary "Please fasten your Bat-seatbelts")
done)

(Thread/sleep 3000))

0 comments on commit 9ba0943

Please sign in to comment.