aesthetic in Bencode

This commit is contained in:
Simon Cruanes 2013-06-12 19:35:48 +02:00
parent b70c94377e
commit 4dd3c63ba5
3 changed files with 18 additions and 0 deletions

View file

@ -6,5 +6,6 @@
#require "threads";; #require "threads";;
#load "thread_containers.cma";; #load "thread_containers.cma";;
open Gen.Infix;; open Gen.Infix;;
#install_printer Bencode.pretty;;
(* vim:syntax=ocaml: (* vim:syntax=ocaml:
*) *)

View file

@ -83,6 +83,20 @@ let fmt formatter t =
to_buf b t; to_buf b t;
Format.pp_print_string formatter (Buffer.contents b) Format.pp_print_string formatter (Buffer.contents b)
let rec pretty fmt t = match t with
| I i -> Format.fprintf fmt "%d" i
| S s -> Format.fprintf fmt "@[<h>\"%s\"@]" s
| L l ->
Format.fprintf fmt "@[<hov 2>[@,";
List.iter (fun t' -> Format.fprintf fmt "%a@ " pretty t') l;
Format.fprintf fmt "]@]";
| D d ->
Format.fprintf fmt "@[<hov 2>{@,";
SMap.iter
(fun k t' -> Format.fprintf fmt "%a -> %a@ " pretty (S k) pretty t')
d;
Format.fprintf fmt "}@]";
(** {2 Deserialization (decoding)} *) (** {2 Deserialization (decoding)} *)
(** Deserialization is based on the {! decoder} type. Parsing can be (** Deserialization is based on the {! decoder} type. Parsing can be

View file

@ -49,6 +49,9 @@ val to_string : t -> string
val to_chan : out_channel -> t -> unit val to_chan : out_channel -> t -> unit
val fmt : Format.formatter -> t -> unit val fmt : Format.formatter -> t -> unit
val pretty : Format.formatter -> t -> unit
(** Print the tree itself, not its encoding *)
(** {2 Deserialization (decoding)} *) (** {2 Deserialization (decoding)} *)
(** Deserialization is based on the {! decoder} type. Parsing can be (** Deserialization is based on the {! decoder} type. Parsing can be