Select the types of activity you want to include in your feed.
tty: extract tty-eio package with Progress.animate helper
Adds a separate `tty-eio` package containing `Tty_eio.Progress.animate`, an Eio-fiber-based redraw loop. Splitting it out keeps the core `tty` library scheduler-agnostic.
···11+let animate ~sw ~clock bar =
22+ Eio.Fiber.fork_daemon ~sw (fun () ->
33+ while not (Tty.Progress.is_finished bar) do
44+ Eio.Time.sleep clock 0.1;
55+ if not (Tty.Progress.is_finished bar) then Tty.Progress.redraw bar
66+ done;
77+ `Stop_daemon)
+6
lib/eio/progress.mli
···11+(** Eio-based animation loop for {!Tty.Progress} bars. *)
22+33+val animate :
44+ sw:Eio.Switch.t -> clock:_ Eio.Time.clock -> Tty.Progress.t -> unit
55+(** [animate ~sw ~clock bar] forks a daemon fiber on [sw] that redraws [bar]
66+ every 100 ms until {!Tty.Progress.is_finished} returns [true]. *)
···122122 position, spawn a loop that calls {!redraw} on your own scheduler:
123123 {[
124124 Eio.Fiber.fork_daemon ~sw (fun () ->
125125- while not (Progress.is_finished bar) do
126126- Eio.Time.sleep clock 0.1;
127127- Progress.redraw bar
128128- done;
129129- `Stop_daemon)
125125+ while not (Progress.is_finished bar) do
126126+ Eio.Time.sleep clock 0.1;
127127+ Progress.redraw bar
128128+ done;
129129+ `Stop_daemon)
130130 ]} *)
131131132132val redraw : t -> unit
+12-10
test/test_progress.ml
···102102 ignore (read_bar ());
103103 Progress.message b "middle";
104104 Progress.tick b;
105105- Alcotest.(check bool) "shows updated message" true
105105+ Alcotest.(check bool)
106106+ "shows updated message" true
106107 (contains "middle" (read_bar ()))
107108108109let test_phase () =
···194195 ignore (read_bar ());
195196 Progress.log b "fetching foo";
196197 let output = Format.flush_str_formatter () |> strip_ansi in
197197- Alcotest.(check bool) "status text appears" true
198198+ Alcotest.(check bool)
199199+ "status text appears" true
198200 (contains "fetching foo" output);
199201 Alcotest.(check bool) "bar still renders" true (contains "With status" output)
200202201203let test_log_ring_drops () =
202204 let b = bar ~status_lines:2 ~total:10 "Ring" in
203203- ignore (read_bar ());
204205 Progress.log b "first";
206206+ ignore (Format.flush_str_formatter ());
205207 Progress.log b "second";
208208+ ignore (Format.flush_str_formatter ());
206209 Progress.log b "third";
207210 let output = Format.flush_str_formatter () |> strip_ansi in
208211 Alcotest.(check bool) "oldest dropped" false (contains "first" output);
···223226 assert the final block has the right shape by checking newlines. *)
224227 ignore final_block;
225228 let total_newlines = count_lines output in
226226- Alcotest.(check bool) "block writes multi-line output" true
227227- (total_newlines >= 2)
229229+ Alcotest.(check bool)
230230+ "block writes multi-line output" true (total_newlines >= 2)
228231229232let test_log_noop_disabled () =
230233 let b =
···253256 Alcotest.test_case "stays on one line" `Quick test_stays_on_one_line;
254257 (* Redraw and animation *)
255258 Alcotest.test_case "redraw emits \\r" `Quick test_redraw_emits_cr;
256256- Alcotest.test_case "redraw keeps position" `Quick test_redraw_keeps_position;
259259+ Alcotest.test_case "redraw keeps position" `Quick
260260+ test_redraw_keeps_position;
257261 Alcotest.test_case "is_finished" `Quick test_is_finished;
258262 Alcotest.test_case "redraw after finish" `Quick test_redraw_after_finish;
259263 Alcotest.test_case "redraw while disabled" `Quick
260264 test_redraw_while_disabled;
261265 Alcotest.test_case "animation loop" `Quick test_animation_loop;
262266 (* Status zone *)
263263- Alcotest.test_case "status off by default" `Quick
264264- test_status_lines_off;
267267+ Alcotest.test_case "status off by default" `Quick test_status_lines_off;
265268 Alcotest.test_case "log emits status row" `Quick test_log_emits_status_row;
266266- Alcotest.test_case "log ring drops oldest" `Quick
267267- test_log_ring_drops;
269269+ Alcotest.test_case "log ring drops oldest" `Quick test_log_ring_drops;
268270 Alcotest.test_case "log block clears cleanly" `Quick
269271 test_log_block_clears_cleanly;
270272 Alcotest.test_case "log silent when disabled" `Quick
+31
tty-eio.opam
···11+# This file is generated by dune, edit dune-project instead
22+opam-version: "2.0"
33+synopsis: "Eio bindings for tty progress bars"
44+description: """
55+Helpers that animate Tty.Progress bars from an Eio fiber. Split out
66+ so the core tty library stays scheduler-agnostic."""
77+maintainer: ["Thomas Gazagnaire <thomas@gazagnaire.org>"]
88+authors: ["Thomas Gazagnaire <thomas@gazagnaire.org>"]
99+license: "MIT"
1010+tags: ["org:blacksun" "cli" "format" "eio"]
1111+depends: [
1212+ "dune" {>= "3.0"}
1313+ "ocaml" {>= "5.1"}
1414+ "tty" {= version}
1515+ "eio" {>= "1.0"}
1616+ "odoc" {with-doc}
1717+]
1818+build: [
1919+ ["dune" "subst"] {dev}
2020+ [
2121+ "dune"
2222+ "build"
2323+ "-p"
2424+ name
2525+ "-j"
2626+ jobs
2727+ "@install"
2828+ "@runtest" {with-test}
2929+ "@doc" {with-doc}
3030+ ]
3131+]