feat(bin): better human-size display

This commit is contained in:
Simon Cruanes 2019-11-22 13:09:59 -06:00
parent b98be15813
commit 440834ca13

View file

@ -1,4 +1,3 @@
module S = Tiny_httpd module S = Tiny_httpd
module Pf = Printf module Pf = Printf
@ -31,13 +30,16 @@ let contains_dot_dot s =
(* Human readable size *) (* Human readable size *)
let human_size (x:int) : string = let human_size (x:int) : string =
if x >= 1_000_000 then Printf.sprintf "%d.%dM" (x / 1_000_000) ((x/1000) mod 1_000) if x >= 1_000_000_000 then Printf.sprintf "%d.%dG" (x / 1_000_000_000) ((x/1_000_000) mod 1_000_000)
else if x >= 1_000_000 then Printf.sprintf "%d.%dM" (x / 1_000_000) ((x/1000) mod 1_000)
else if x >= 1_000 then Printf.sprintf "%d.%dk" (x/1000) ((x/100) mod 10) else if x >= 1_000 then Printf.sprintf "%d.%dk" (x/1000) ((x/100) mod 10)
else Printf.sprintf "%db" x else Printf.sprintf "%db" x
let header_html = "Content-Type", "text/html" let header_html = "Content-Type", "text/html"
let (//) = Filename.concat let (//) = Filename.concat
(* TODO: percent encoding/decoding *)
let html_list_dir ~top ~parent d : string = let html_list_dir ~top ~parent d : string =
let entries = Sys.readdir @@ (top // d) in let entries = Sys.readdir @@ (top // d) in
Array.sort compare entries; Array.sort compare entries;