1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
open! Import
module Percentage = struct
let clamp (lower, upper) = min upper >> max lower
let of_float =
let percentage x = clamp (0, 100) (Float.to_int (x *. 100.)) in
let pp ppf x = Format.fprintf ppf "%3.0d%%" (percentage x) in
let to_string x = Format.asprintf "%3.0d%%" (percentage x) in
Printer.create ~string_len:4 ~to_string ~pp ()
end
module Bytes = struct
let rec power = function 1 -> 1024L | n -> Int64.mul 1024L (power (n - 1))
let conv exp = Int64.(of_int >> mul (power exp))
let kib = conv 1
let mib = conv 2
let gib = conv 3
let tib = conv 4
let pib = conv 5
(** Pretty-printer for byte counts *)
let generic (type a) (module Integer : Integer.S with type t = a) =
let process_components x k =
let mantissa, unit, rpad =
match[@ocamlformat "disable"] Integer.to_float x with
| n when Float.(n < 1024. ) -> (n , "B", " ")
| n when Float.(n < 1024. ** 2.) -> (n /. 1024. , "KiB", "")
| n when Float.(n < 1024. ** 3.) -> (n /. (1024. ** 2.), "MiB", "")
| n when Float.(n < 1024. ** 4.) -> (n /. (1024. ** 3.), "GiB", "")
| n when Float.(n < 1024. ** 5.) -> (n /. (1024. ** 4.), "TiB", "")
| n when Float.(n < 1024. ** 6.) -> (n /. (1024. ** 5.), "PiB", "")
| n -> (n /. (1024. ** 6.), "EiB", "")
in
let mantissa = Float.trunc (mantissa *. 10.) /. 10. in
let lpad =
match mantissa with
| n when Float.(n < 10.) -> " "
| n when Float.(n < 100.) -> " "
| n when Float.(n < 1000.) -> " "
| _ -> ""
in
k ~mantissa ~unit ~rpad ~lpad
in
let pp ppf x =
process_components x (fun ~mantissa ~unit ~rpad:_ ~lpad:_ ->
Fmt.pf ppf "%.1f %s" mantissa unit)
in
let to_string x =
process_components x (fun ~mantissa ~unit ~rpad ~lpad ->
Printf.sprintf "%s%.1f %s%s" lpad mantissa unit rpad)
in
let string_len = 10 in
Printer.create ~to_string ~string_len ~pp ()
let of_int = generic (module Integer.Int)
let of_int63 = generic (module Integer.Int63)
let of_int64 = generic (module Integer.Int64)
let of_float = generic (module Integer.Float)
let pp_int63 = Printer.to_pp of_int63
end
module Duration = struct
let mm_ss =
let to_string span =
let seconds = Mtime.span_to_s span in
if Float.compare seconds 0. < 0 then "--:--"
else
Printf.sprintf "%02.0f:%02.0f"
(Float.div seconds 60. |> Float.floor)
(Float.rem seconds 60. |> Float.floor)
in
Printer.create ~string_len:5 ~to_string ()
let hh_mm_ss =
let to_string span =
let seconds = Mtime.span_to_s span in
if Float.compare seconds 0. < 0 then "--:--:--"
else
Printf.sprintf "%02.0f:%02.0f:%02.0f"
(Float.div seconds 3600. |> Float.floor)
(Float.(rem (div seconds 60.) 60.) |> Float.floor)
(Float.rem seconds 60. |> Float.floor)
in
Printer.create ~string_len:8 ~to_string ()
end