Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435(* The hash table for utility locations *)open Importmodule M = Map.Make (String)
type entry = { hits : int; loc : string }
let entry_jsont = let make hits loc = { hits; loc } in Jsont.Object.map ~kind:"Entry" make |> Jsont.Object.mem "hits" Jsont.int ~enc:(fun t -> t.hits) |> Jsont.Object.mem "loc" Jsont.string ~enc:(fun t -> t.loc) |> Jsont.Object.finish
type t = entry M.t
let jsont = M.jsont entry_jsontlet empty = M.empty
let add ~utility ~loc t = match M.find_opt utility t with | Some { hits; loc = loc' } when String.equal loc loc' -> M.add utility { hits = hits + 1; loc } t | None | Some _ -> M.add utility { hits = 1; loc } t
let lookup ~utility t = M.find_opt utility t |> Option.map (fun v -> v.loc)
let pp ppf t = let entries = M.to_list t in match entries with | [] -> () | _ -> let pp_entry ppf (_, { hits; loc }) = Fmt.pf ppf "%-7i %s@." hits loc in let pp_header ppf () = Fmt.pf ppf "%-7s %s@." "hits" "command" in Fmt.pf ppf "@[<v>%a%a@]" pp_header () Fmt.(list pp_entry) entries