From 86402b208d4e925a15146a2541c937590320e9e0 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 8 Aug 2021 18:29:00 +0530 Subject: format --- src/chart.ml | 113 +++++++++++++++++++++++++++++--------------------------- src/chart.mli | 5 --- src/marker.ml | 45 +++++++++++----------- src/marker.mli | 4 -- src/options.ml | 15 ++++---- src/options.mli | 4 -- src/utils.ml | 32 ++++++++-------- 7 files changed, 103 insertions(+), 115 deletions(-) delete mode 100644 src/chart.mli delete mode 100644 src/marker.mli delete mode 100644 src/options.mli diff --git a/src/chart.ml b/src/chart.ml index 0826092..de79082 100644 --- a/src/chart.ml +++ b/src/chart.ml @@ -1,61 +1,66 @@ let to_string c = String.make 1 c + let space = to_string ' ' + let axis = "┬" + let axis_spc = "─" -type t = { vals: int list; opts: Options.t } +type t = { vals : int list; opts : Options.t } + let default = { vals = []; opts = Options.default } -let plot v c = { c with vals = v; } -let with_options v c = { v with opts = c; } -let pp channel {vals: int list; opts: Options.t } = - let open Utils in - let height = opts.height in - let min_val = list_min vals in - let max_val = list_max vals in - let range = max_val - min_val in - let scale v = ((v - min_val) * height) / range in - let new_vals = List.map scale vals in - let rec pad left right s = - if left = 0 - then s :: repeat right space - else space :: pad (left - 1) right s - in - let new_vals' = zip new_vals (List.tl new_vals) in - let make_marker prefix left right s = - prefix :: - (if left = 0 - then s :: repeat right space - else space :: pad (left - 1) right s) - in - let make_connector s e = - match Stdlib.compare s e with - | 0 -> make_marker axis_spc s (height - s) "-" - | x -> - let (ltr_chr, rtl_chr) = - match x with - | 1 -> ("└", "┐") - | _ -> ("┘", "┌") - in - let (st, en) = Stdlib.((min s e, max s e)) in - let l = en - st |> saturating_sub 1 in - let (left, right) = (repeat st space, repeat (height - l - st) space) in - List.flatten [[axis_spc]; left; [ltr_chr]; repeat l "│"; [rtl_chr]; right] + +let plot v c = { c with vals = v } + +let with_options v c = { c with opts = v } + +let pp formatter { vals : int list; opts : Options.t } = + let open Utils in + let height = opts.height in + let min_val = list_min vals in + let max_val = list_max vals in + let range = max_val - min_val in + let scale v = (v - min_val) * height / range in + let new_vals = List.map scale vals in + let rec pad left right s = + if left = 0 then s :: repeat right space + else space :: pad (left - 1) right s + in + let new_vals' = zip new_vals (List.tl new_vals) in + let make_marker prefix left right s = + prefix + :: + (if left = 0 then s :: repeat right space + else space :: pad (left - 1) right s) + in + let make_connector s e = + match Stdlib.compare s e with + | 0 -> make_marker axis_spc s (height - s) "-" + | x -> + let ltr_chr, rtl_chr = + match x with 1 -> ("└", "┐") | _ -> ("┘", "┌") in - let m = Printf.sprintf opts.marker in - let rec make_graph = function - | [] -> [] - | (s, e) :: [] -> - make_marker axis s (height - s) m - :: make_connector s e - :: make_marker axis e (height - e) m - :: [] - | (s, e) :: tl -> - make_marker axis s (height - s) m - :: make_connector s e - :: make_graph tl - in - make_graph new_vals' - |> rot_left - |> List.map (String.concat "") - |> String.concat "\n" - |> Printf.fprintf channel "%s" + let st, en = Stdlib.(min s e, max s e) in + let l = en - st |> saturating_sub 1 in + let left, right = (repeat st space, repeat (height - l - st) space) in + List.flatten + [ + [ axis_spc ]; left; [ ltr_chr ]; repeat l "│"; [ rtl_chr ]; right; + ] + in + let m = Format.asprintf "%a" Marker.pp opts.marker in + let rec make_graph = function + | [] -> [] + | [ (s, e) ] -> + [ + make_marker axis s (height - s) m; + make_connector s e; + make_marker axis e (height - e) m; + ] + | (s, e) :: tl -> + make_marker axis s (height - s) m :: make_connector s e :: make_graph tl + in + make_graph new_vals' |> rot_left + |> List.map (String.concat "") + |> String.concat "\n" + |> Format.fprintf formatter "%s" diff --git a/src/chart.mli b/src/chart.mli deleted file mode 100644 index 3c758c4..0000000 --- a/src/chart.mli +++ /dev/null @@ -1,5 +0,0 @@ -type t -val default : t -val plot : int list -> t -> t -val with_options : Options.t -> t -> t -val pp : out_channel -> t -> unit diff --git a/src/marker.ml b/src/marker.ml index 0c59804..7d37e2a 100644 --- a/src/marker.ml +++ b/src/marker.ml @@ -1,27 +1,26 @@ -type t = Point - | Pixel - | Circle - | TriangleDown - | TriangleUp - | TriangleLeft - | TriangleRight - | Custom of string +type t = + | Point + | Pixel + | Circle + | TriangleDown + | TriangleUp + | TriangleLeft + | TriangleRight + | Custom of string exception Invalid_marker_length -let of_string s = - if String.length s <> 1 - then raise Invalid_marker_length - else Custom(s) -let pp channel n = - (match n with - | Point -> "." - | Pixel -> "," - | Circle -> "o" - | TriangleDown -> "v" - | TriangleUp -> "^" - | TriangleLeft -> ">" - | TriangleRight -> "<" - | Custom(s) -> s) - |> Printf.fprintf channel "%s" +let of_string s = + if String.length s <> 1 then raise Invalid_marker_length else Custom s +let pp formatter n = + (match n with + | Point -> "." + | Pixel -> "," + | Circle -> "o" + | TriangleDown -> "v" + | TriangleUp -> "^" + | TriangleLeft -> ">" + | TriangleRight -> "<" + | Custom s -> s) + |> Format.fprintf formatter "%s" diff --git a/src/marker.mli b/src/marker.mli deleted file mode 100644 index 391916b..0000000 --- a/src/marker.mli +++ /dev/null @@ -1,4 +0,0 @@ -type t -val of_string : string -> t -val pp : out_channel -> t -> unit - diff --git a/src/options.ml b/src/options.ml index a57c28b..341c0bb 100644 --- a/src/options.ml +++ b/src/options.ml @@ -1,8 +1,7 @@ -type t = { height: int; ascii: bool; marker: Marker.t} -let default = - { height = 14 - ; ascii = false - ; marker = Marker.Point - } -let with_height v c = { c with height = v; } -let with_marker v c = { c with marker = v; } +type t = { height : int; ascii : bool; marker : Marker.t } + +let default = { height = 14; ascii = false; marker = Marker.Circle } + +let with_height v c = { c with height = v } + +let with_marker v c = { c with marker = v } diff --git a/src/options.mli b/src/options.mli deleted file mode 100644 index 64bee51..0000000 --- a/src/options.mli +++ /dev/null @@ -1,4 +0,0 @@ -type t -val default : t -val with_height : int -> t -> t -val with_marker : Marker.t -> t -> t diff --git a/src/utils.ml b/src/utils.ml index 62ae3a5..833c972 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -1,33 +1,31 @@ exception Empty_list + let fold fn = function - | [] -> raise Empty_list - | head :: tail -> List.fold_left fn head tail + | [] -> raise Empty_list + | head :: tail -> List.fold_left fn head tail let list_min ls = fold Stdlib.min ls + let list_max ls = fold Stdlib.max ls -let rec repeat n a = - if n <= 0 - then [] - else a :: repeat (n - 1) a +let rec repeat n a = if n <= 0 then [] else a :: repeat (n - 1) a -let empty ls = (ls = []) +let empty ls = ls = [] let rec zip l1 l2 = - match (l1, l2) with - | ([], _) -> [] - | (_, []) -> [] - | (h1 :: t1, h2 :: t2) -> (h1, h2) :: zip t1 t2 + match (l1, l2) with + | [], _ -> [] + | _, [] -> [] + | h1 :: t1, h2 :: t2 -> (h1, h2) :: zip t1 t2 let rec transpose = function - | [] :: _ -> [] - | ls -> List.(map hd ls) :: transpose List.(map tl ls) + | [] :: _ -> [] + | ls -> List.(map hd ls) :: transpose List.(map tl ls) let flip f a b = f b a let rot_right ls = List.(rev ls |> transpose) -let rot_left ls = List.(transpose ls |> rev) -let saturating_sub b a = - if a < b then 0 - else a - b +let rot_left ls = List.(transpose ls |> rev) + +let saturating_sub b a = if a < b then 0 else a - b -- cgit v1.2.3