From 00473c6ac09d85a6b6e1ce0cbe132e31407a2d00 Mon Sep 17 00:00:00 2001 From: Laszlo Ersek Date: Thu, 6 Jan 2022 15:09:08 +0100 Subject: [PATCH] convert/libosinfo_utils: introduce "string_of_osinfo_device_list" For debugging purposes, we'll want to print the list of devices returned by the previously introduced "osinfo_os#get_devices" method. Format the device list as a nice table. Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1942325 Signed-off-by: Laszlo Ersek Message-Id: <20220106140910.13695-8-lersek@redhat.com> Acked-by: Richard W.M. Jones --- convert/libosinfo_utils.ml | 35 +++++++++++++++++++++++++++++++++++ convert/libosinfo_utils.mli | 3 +++ 2 files changed, 38 insertions(+) diff --git a/convert/libosinfo_utils.ml b/convert/libosinfo_utils.ml index 1fc138cc..d5eb082b 100644 --- a/convert/libosinfo_utils.ml +++ b/convert/libosinfo_utils.ml @@ -42,3 +42,38 @@ let string_of_osinfo_device_driver { Libosinfo.architecture; location; (if signed then "signed" else "unsigned") priority (String.concat " " files) + +let string_of_osinfo_device_list dev_list = + + (* Turn the fields of an "osinfo_device" record into a list. *) + let listify { Libosinfo.id; vendor; vendor_id; product; product_id; name; + class_; bus_type; subsystem } = + [ id; vendor; vendor_id; product; product_id; name; + class_; bus_type; subsystem ] + + (* Given a list of strings, and a list of previously known maximum widths, + * "increase" each width, if necessary, to the length of the corresponding + * string. + *) + and grow_widths = List.map2 (fun s -> max (String.length s)) + in + + (* Compute the maximum width for each field in "dev_list". *) + let max_widths = + List.fold_right grow_widths (List.map listify dev_list) + [ 0; 0; 0; 0; 0; 0; 0; 0; 0 ] + + (* Given a list of strings and a list of field widths, format "string1 | + * string2 | ... | stringN" such that each field is right-padded to the + * corresponding width. + *) + and columnate strings widths = + String.concat " | " (List.map2 (Printf.sprintf "%-*s") widths strings) + in + + (* Format "dev_list" as a table by (a) printing one "osinfo_device" record + * per line, and (b) right-padding each field of each "osinfo_device" record + * to the maximum width of that field. + *) + String.concat "\n" + (List.map (fun dev -> columnate (listify dev) max_widths) dev_list) diff --git a/convert/libosinfo_utils.mli b/convert/libosinfo_utils.mli index b3714d22..5a703334 100644 --- a/convert/libosinfo_utils.mli +++ b/convert/libosinfo_utils.mli @@ -27,3 +27,6 @@ val get_os_by_short_id : string -> Libosinfo.osinfo_os val string_of_osinfo_device_driver : Libosinfo.osinfo_device_driver -> string (** Convert a [osinfo_device_driver] to a printable string for debugging. *) + +val string_of_osinfo_device_list : Libosinfo.osinfo_device list -> string +(** Convert an [osinfo_device] list to a printable string for debugging. *) -- 2.31.1