Skip to content

Remove use of Vector #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ progress bar formats. Supports rendering multiple progress bars simultaneously.\
")
(documentation https://CraigFe.github.io/progress/)
(depends
(ocaml (>= 4.08.0))
(ocaml (>= 5.2.0))
(terminal (= :version))
(fmt (>= 0.8.5))
(logs (>= 0.7.0))
(mtime (>= 2.0.0))
(uucp (>= 2.0.0))
(uutf (>= 1.0.0))
vector
(optint (>= 0.1.0))
(alcotest (and :with-test (>= 1.4.0)))
(astring :with-test)))
Expand Down
10 changes: 5 additions & 5 deletions examples/cargo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let packages =
; ("yojson", "1.7.0")
; ("zarith", "1.9.1")
]
|> Vector.of_list ~dummy:("", "")
|> Dynarray.of_list

let setup_logs () =
let reporter = Progress.logs_reporter () in
Expand All @@ -41,7 +41,7 @@ let setup_logs () =

let bar =
let open Progress.Line in
let total = Vector.length packages in
let total = Dynarray.length packages in
list
[ constf " %a" Fmt.(styled `Cyan string) "Building"
; using fst
Expand All @@ -54,9 +54,9 @@ let bar =
]

let rec package_worker (active_packages, reporter) =
match Vector.pop packages with
| exception Vector.Empty -> ()
| package, version ->
match Dynarray.pop_last_opt packages with
| None -> ()
| Some (package, version) ->
active_packages := package :: !active_packages;
Logs.app (fun f ->
f " %a %s %s" Fmt.(styled `Green string) "Compiling" package version);
Expand Down
2 changes: 1 addition & 1 deletion examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(modules
(:standard \ main))
(libraries progress unix logs logs.fmt logs.threaded fmt fmt.tty mtime
mtime.clock.os vector threads.posix))
mtime.clock.os threads.posix))

(executable
(name main)
Expand Down
6 changes: 3 additions & 3 deletions examples/utils.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let ( .%() ) v i = Vector.get v i
let ( .%()<- ) v i x = Vector.set v i x
let ( .%() ) v i = Dynarray.get v i
let ( .%()<- ) v i x = Dynarray.set v i x

let shuffle_vector =
let shuffle_subvector rand_int v i j =
Expand All @@ -10,7 +10,7 @@ let shuffle_vector =
v.%(k) <- tmp
done
in
fun v -> shuffle_subvector Random.int v 0 (Vector.length v)
fun v -> shuffle_subvector Random.int v 0 (Dynarray.length v)

let colors =
(* import matplotlib.cm
Expand Down
2 changes: 1 addition & 1 deletion examples/utils.mli
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val shuffle_vector : _ Vector.t -> unit
val shuffle_vector : _ Dynarray.t -> unit
val colour_picker : unit -> unit -> Progress.Color.t
3 changes: 1 addition & 2 deletions progress.opam
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ doc: "https://CraigFe.github.io/progress/"
bug-reports: "https://github.com/CraigFe/progress/issues"
depends: [
"dune" {>= "2.7"}
"ocaml" {>= "4.08.0"}
"ocaml" {>= "5.2.0"}
"terminal" {= version}
"fmt" {>= "0.8.5"}
"logs" {>= "0.7.0"}
"mtime" {>= "2.0.0"}
"uucp" {>= "2.0.0"}
"uutf" {>= "1.0.0"}
"vector"
"optint" {>= "0.1.0"}
"alcotest" {with-test & >= "1.4.0"}
"astring" {with-test}
Expand Down
3 changes: 1 addition & 2 deletions src/progress/engine/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
fmt
logs
logs.fmt
terminal_ansi
vector))
terminal_ansi))
22 changes: 11 additions & 11 deletions src/progress/engine/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,38 @@ module Mtime = struct
let span_to_s span = Mtime.Span.to_float_ns span *. 1e-9
end

module Vector = struct
include Vector
module Dynarray = struct
include Dynarray

let iter ~f t = iter f t

let iteri_from ~f i t =
for i = i to length t - 1 do
f i (unsafe_get t i)
f i (get t i)
done

let rec find_map_from i t ~f =
if i >= length t then None
else
let a = unsafe_get t i in
let a = get t i in
match f a with
| Some _ as some -> some
| None -> find_map_from (i + 1) t ~f

let find_map t ~f = find_map_from 0 t ~f

let insert t k v =
Vector.push t v (* Dummy insertion to expand *);
for i = Vector.length t - 1 downto k + 1 do
Vector.set t i (Vector.get t (pred i))
Dynarray.add_last t v (* Dummy insertion to expand *);
for i = Dynarray.length t - 1 downto k + 1 do
Dynarray.set t i (Dynarray.get t (pred i))
done;
Vector.set t k v
Dynarray.set t k v

let remove (type a) (t : a t) k =
for i = k to Vector.length t - 2 do
Vector.set t i (Vector.get t (succ i))
for i = k to Dynarray.length t - 2 do
Dynarray.set t i (Dynarray.get t (succ i))
done;
ignore (Vector.pop t : a)
ignore (Dynarray.pop_last t : a)

let get_exn = get
let get = `shadowed
Expand Down
36 changes: 18 additions & 18 deletions src/progress/engine/renderer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ end = struct
{ config : Config.t
; uid : Unique_id.t
; bars : (Bar_id.t, some_bar) Hashtbl.t
; rows : some_bar option Vector.t
; rows : some_bar option Dynarray.t
}

let config t = t.config
Expand All @@ -155,11 +155,11 @@ end = struct
| None -> None
| Some renderer -> Some (E { renderer; latest_width = 0; position = i })
in
Bar_list.mapi bars ~f:{ f } |> Vector.of_list ~dummy:None
Bar_list.mapi bars ~f:{ f } |> Dynarray.of_list
in
let bar_count = Bar_list.length bars in
let bars = Hashtbl.create bar_count in
Vector.iter rows
Dynarray.iter rows
~f:
(Option.iter (fun (E { renderer; _ } as t) ->
Hashtbl.add bars ~key:(Bar_renderer.id renderer) ~data:t));
Expand Down Expand Up @@ -190,8 +190,8 @@ end = struct

let rerender_all_from_top ~stage ~starting_at ~unconditional
({ config = { ppf; _ }; rows; _ } as t) =
let total_rows = Vector.length rows in
Vector.iteri_from starting_at rows ~f:(fun idx slot ->
let total_rows = Dynarray.length rows in
Dynarray.iteri_from starting_at rows ~f:(fun idx slot ->
let is_last = idx = total_rows - 1 in
let () =
match slot with
Expand Down Expand Up @@ -227,7 +227,7 @@ end = struct
match data with
| `Clean _ -> ()
| `Dirty data ->
let distance_from_base = Vector.length rows - bar.position - 1 in
let distance_from_base = Dynarray.length rows - bar.position - 1 in

(* NOTE: we add an initial carriage return to avoid overflowing the line if
the user has typed into the terminal between renders. *)
Expand All @@ -242,13 +242,13 @@ end = struct
Hashtbl.remove t.bars uid

let add_line ?(above = 0) t renderer =
let position = Vector.length t.rows - above in
let position = Dynarray.length t.rows - above in
let key = Bar_renderer.id renderer in
let bar = E { renderer; latest_width = 0; position } in
Hashtbl.add t.bars ~key ~data:bar;

Vector.insert t.rows position (Some bar);
Vector.iteri_from (position + 1) t.rows ~f:(fun i -> function
Dynarray.insert t.rows position (Some bar);
Dynarray.iteri_from (position + 1) t.rows ~f:(fun i -> function
| None -> ()
| Some (E bar) -> bar.position <- i);

Expand All @@ -267,7 +267,7 @@ end = struct
(* This can either mean that the line has already been finalised, or
that this key is unknown. *)
match
Vector.find_map t.rows ~f:(function
Dynarray.find_map t.rows ~f:(function
| None -> None
| Some (E bar) as some_bar ->
if Bar_id.equal key (Bar_renderer.id bar.renderer) then
Expand All @@ -278,16 +278,16 @@ end = struct
| None -> failwith "No such line in display")
in
if Hashtbl.mem t.bars key then Hashtbl.remove t.bars key;
Vector.remove t.rows position;
Vector.iteri_from position t.rows ~f:(fun i -> function
Dynarray.remove t.rows position;
Dynarray.iteri_from position t.rows ~f:(fun i -> function
| None -> ()
| Some (E bar) -> bar.position <- i);

(* The cursor is now one line below the bottom. Move to the correct starting
position for a re-render of the affected suffix of the display. *)
Format.pp_print_string t.config.ppf Terminal.Ansi.erase_display_suffix;
Terminal.Ansi.move_up t.config.ppf 1;
Terminal.Ansi.move_up t.config.ppf (Vector.length t.rows - position - 1);
Terminal.Ansi.move_up t.config.ppf (Dynarray.length t.rows - position - 1);
rerender_all_from_top ~stage:`update ~starting_at:position
~unconditional:true t

Expand All @@ -298,10 +298,10 @@ end = struct

let handle_width_change ({ config = { ppf; _ }; rows; _ } as display)
new_width =
let row_count = Vector.length rows in
let row_count = Dynarray.length rows in
let latest_widths =
Array.init row_count ~f:(fun i ->
Vector.get_exn rows i
Dynarray.get_exn rows i
|> Option.fold ~none:0 ~some:(fun (E t) -> t.latest_width))
in
let overflows =
Expand All @@ -319,12 +319,12 @@ end = struct
display

let tick ({ config = { ppf; _ }; rows; _ } as t) =
Terminal.Ansi.move_up ppf (Vector.length rows - 1);
Terminal.Ansi.move_up ppf (Dynarray.length rows - 1);
rerender_all_from_top ~stage:`tick ~starting_at:0 ~unconditional:false t

let pause { config = { ppf; _ }; rows; _ } =
Format.fprintf ppf "%s%!" Terminal.Ansi.erase_line;
for _ = 1 to Vector.length rows - 1 do
for _ = 1 to Dynarray.length rows - 1 do
Format.fprintf ppf "%a%s%!" Terminal.Ansi.move_up 1
Terminal.Ansi.erase_line
done
Expand All @@ -342,7 +342,7 @@ end = struct

let finalise
({ config = { ppf; hide_cursor; persistent; _ }; rows; _ } as display) =
Terminal.Ansi.move_up ppf (Vector.length rows - 1);
Terminal.Ansi.move_up ppf (Dynarray.length rows - 1);
if persistent then (
rerender_all_from_top ~stage:`finalise ~starting_at:0 ~unconditional:true
display;
Expand Down