From: Sylvain Le Gall <gildor@debian.org>
Date: Sun, 1 Sep 2019 21:33:40 +0200
Subject: Compute header width based on header file content.

---
 main.ml  | 25 ++++++++++++++++---------
 model.ml | 18 ++++++++++--------
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/main.ml b/main.ml
index 4f80c8f..23cd477 100644
--- a/main.ml
+++ b/main.ml
@@ -98,9 +98,16 @@ let read_headerfile filename =
     with
       End_of_file -> close_in ic; []
   in
-  loop ()
-
-
+  let header = 
+    loop ()
+  in
+  let header_width =
+    List.fold_left 
+      (fun w line -> max (String.length line) w)  
+      0
+      header
+  in
+    header, header_width
 
 (***************************************************************************)
 (** {2 Processing files} *)
@@ -151,14 +158,14 @@ let copy ic oc =
   in
   loop ()
 
-let create_header header filename =
+let create_header header header_width filename =
   let generator = find_generator filename in
   let skip_lst = find_skips filename in
   pipe_file (fun ic oc ->
     let () = Skip.skip skip_lst ic oc in
     let line = generator.Model.remove ic in
     let () = Skip.skip skip_lst ic oc in
-    generator.Model.create oc header;
+    generator.Model.create oc header header_width;
     output_string oc line;
     copy ic oc
   ) filename
@@ -184,24 +191,24 @@ let remove_header filename =
 (** {2 Main loop} *)
 
 type action =
-    Create of string list
+    Create of string list * int
   | Remove
 
 
 let main () =
 
-  let action = ref (Create []) in
+  let action = ref (Create ([], 0)) in
 
   let anonymous filename =
     match !action with
-      Create header -> create_header header filename
+      Create (header, header_width) -> create_header header header_width filename
     | Remove -> remove_header filename
   in
 
   Arg.parse [
 
   "-h",
-  Arg.String (fun s -> action := Create (read_headerfile s)),
+  Arg.String (fun s -> let (header, header_width) = (read_headerfile s) in action := Create (header, header_width)),
   "<file>  Create headers with text from <file>";
 
   "-c",
diff --git a/model.ml b/model.ml
index 6c42c41..5533c0a 100644
--- a/model.ml
+++ b/model.ml
@@ -26,7 +26,7 @@ exception Error of string
 
 type generator =
     { remove: in_channel -> string;
-      create: out_channel -> string list -> unit;
+      create: out_channel -> string list -> int -> unit;
     } 
 
 (***************************************************************************)
@@ -92,8 +92,9 @@ let make_frame ~open_comment ~close_comment ~line_char ~margin ~width =
       ""
   in
 
-  let create oc header =
-    let width' = width + 2 * String.length margin in
+  let create oc header header_width =
+    let real_width = max width header_width in
+    let width' = real_width + 2 * String.length margin in
     let white = String.make width' ' ' in
     let line = String.make width' line_char in
     Printf.fprintf oc "%s%s%s\n" open_comment line close_comment;
@@ -102,7 +103,7 @@ let make_frame ~open_comment ~close_comment ~line_char ~margin ~width =
       output_string oc open_comment;
       output_string oc margin;
       output_string oc string;
-      output oc white 0 (max 0 (width - String.length string));
+      output oc white 0 (max 0 (real_width - String.length string));
       output_string oc margin;
       output_string oc close_comment;
       output_char oc '\n'
@@ -160,9 +161,10 @@ let make_lines ~open_comment ~close_comment ~line_char ~begin_line
       ""
   in
 
-  let create oc header =
+  let create oc header header_width =
+    let real_width = max width header_width in
     Printf.fprintf oc "%s%s\n" open_comment 
-      (String.make (max 0 (width - String.length open_comment)) line_char);
+      (String.make (max 0 (real_width - String.length open_comment)) line_char);
     
     List.iter (function string ->
       output_string oc begin_line;
@@ -172,7 +174,7 @@ let make_lines ~open_comment ~close_comment ~line_char ~begin_line
 
     Printf.fprintf oc "%s%s%s\n\n" 
       begin_last
-      (String.make (max 0 (width - String.length begin_last
+      (String.make (max 0 (real_width - String.length begin_last
 			     - String.length close_comment)) line_char)
       close_comment;
 
@@ -198,7 +200,7 @@ let _ =
 let make_no () =
 
   { remove = (fun _ -> "");
-    create = (fun _ _ -> ())
+    create = (fun _ _ _ -> ())
   } 
 
 let _ =
