From f52c99d579463f6f0e9e00037e9a7777e0014bdb Mon Sep 17 00:00:00 2001 From: orbifx Date: Fri, 15 Oct 2021 17:53:38 +0100 Subject: [PATCH] Prettier txt messages --- http/http.ml | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/http/http.ml b/http/http.ml index bc0aee3..195d393 100644 --- a/http/http.ml +++ b/http/http.ml @@ -48,23 +48,27 @@ let pull_text url dir id = (*TODO: adapt Archive predication function to work with free sets*) let parse_index _is_selected fn url = function | Msgpck.List (info :: _fields :: [texts]) -> - let version, archive_name, _archivists = - match info with Msgpck.List (v::n::a) -> Msgpck.(to_int v, to_string n, a) | _ -> invalid_arg "Pack header" + let _version, archive_name, _archivists = + match info with Msgpck.List (v::n::a) -> Msgpck.(to_int v, to_string n, a) + | _ -> invalid_arg "Pack header" in - print_endline @@ archive_name ^ " (pack format version " ^ string_of_int version ^ ")"; + print_string archive_name; (match Msgpck.to_list texts with - | [] -> prerr_endline "Empty index" + | [] -> print_endline ", has empty index" | texts -> - (try Unix.mkdir "peers" 0o740 with Unix.Unix_error (EEXIST, _, _) -> () | _ -> prerr_endline "Error making peers"); + (try Unix.mkdir "peers" 0o740 with + Unix.Unix_error (EEXIST, _, _) -> () | _ -> prerr_endline "Error making peers"); let dir = "peers/" ^ match Uri.host url with | None -> "local/" ^ Uri.path url | Some s -> s ^ Uri.path url ^ "/" in (match Bos.OS.Dir.create ~mode:0o740 (Fpath.v dir) with | Ok _ -> () | _ -> prerr_endline "Error making domain dir"); - print_endline @@ "Copying into: " ^ dir; - let text_num = List.length texts in + print_endline (" => " ^ dir); + let numof_texts= string_of_int @@ List.length texts in + let text_num_len = String.length numof_texts in let of_pck i x = - print_string @@ "\rDownloading " ^ string_of_int (i+1) ^ "/" ^ string_of_int text_num ^ " "; flush stdout; + Printf.printf "\r%*d/%s" text_num_len (i+1) numof_texts; + flush stdout; match x with | Msgpck.List (id::time::title::_authors::_topics) -> (match Logarion.Id.of_bytes Msgpck.(to_bytes id) with @@ -106,14 +110,18 @@ let pull_msgs url _authors _topics = match http_apply response url with in let s = Scanf.Scanning.from_string body in let msgs = MsgSet.empty in + let date_string t = Ptime.to_date t |> + fun (y, m, d) -> Printf.sprintf "%04d-%02d-%02d" y m d in let msgs = fold_msgs s msgs (fun msgs t m -> match Ptime.of_rfc3339 t with | Ok (v,_,_) -> let open MsgSet in - let msgs = if cardinal msgs > 3 then remove (max_elt msgs) msgs else msgs in + let msgs = if cardinal msgs > 1 then remove (max_elt msgs) msgs else msgs in add (v,m) msgs | _ -> msgs) in - print_endline ("\n" ^ Uri.to_string url); - MsgSet.iter (fun (t,m) -> print_endline (Ptime.to_rfc3339 t ^ "\t" ^ m)) msgs + print_endline ("\n┌────=[ " ^ Uri.to_string url); + MsgSet.iter + (fun (t,m) -> print_endline + ("│ " ^ date_string t ^ "\n│\n│ " ^ m ^ "\n└─────────")) msgs let pull_fn url = match Uri.of_string url with | x when x = Uri.empty -> (fun _ _ -> ())