A quick script to test a progress bar override for Eio flows
eio_progress.ml
1[@@@opam eio_main progress]
2
3let progress_source report (Eio.Resource.T (v, bindings) : _ Eio.Flow.source) =
4 let module X = (val (Eio.Resource.get bindings Eio.Flow.Pi.Source)) in
5 let module V = struct
6 include X
7 let single_read v bufs =
8 let r = X.single_read v bufs in
9 report (Int64.of_int r);
10 r
11 end in
12 Eio.Resource.T (v, Eio.Flow.Pi.source (module V))
13
14let load path =
15 Eio.Path.with_open_in path @@ fun flow ->
16 let size = Eio.File.size flow in
17 Progress.with_reporter (Progress.counter ?message:(Option.map snd @@ Eio.Path.split path) (Optint.Int63.to_int64 size)) @@ fun r ->
18 let flow = progress_source r flow in
19 try
20 if Optint.Int63.(compare size (of_int Sys.max_string_length)) = 1 then
21 raise @@ Eio.Fs.err File_too_large;
22 let buf = Cstruct.create (Optint.Int63.to_int size) in
23 let rec loop buf got =
24 match Eio.Flow.single_read flow buf with
25 | n -> loop (Cstruct.shift buf n) (n + got)
26 | exception End_of_file -> got
27 in
28 let got = loop buf 0 in
29 Cstruct.to_string ~len:got buf
30 with Eio.Exn.Io _ as ex ->
31 let bt = Printexc.get_raw_backtrace () in
32 Eio.Exn.reraise_with_context ex bt "loading %a" Eio.Path.pp path
33
34let () =
35 Eio_main.run @@ fun env ->
36 let s = load Eio.Path.(env#fs / Sys.argv.(1)) in
37 ignore s