Blame SOURCES/0007-convert-libosinfo_utils-introduce-string_of_osinfo_d.patch

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