Package: headache / 1.03-27

03_header_width Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Author: Sylvain Le Gall <gildor@debian.org>
Description: Compute header width based on header file content.

Index: headache/main.ml
===================================================================
--- headache.orig/main.ml	2012-01-03 08:42:53.000000000 +0100
+++ headache/main.ml	2012-01-03 08:42:54.000000000 +0100
@@ -98,9 +98,16 @@
     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 @@
   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 @@
 (** {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",
Index: headache/model.ml
===================================================================
--- headache.orig/model.ml	2012-01-03 08:42:53.000000000 +0100
+++ headache/model.ml	2012-01-03 08:42:54.000000000 +0100
@@ -26,7 +26,7 @@
 
 type generator =
     { remove: in_channel -> string;
-      create: out_channel -> string list -> unit;
+      create: out_channel -> string list -> int -> unit;
     } 
 
 (***************************************************************************)
@@ -92,8 +92,9 @@
       ""
   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 @@
       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 @@
       ""
   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 @@
 
     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 make_no () =
 
   { remove = (fun _ -> "");
-    create = (fun _ _ -> ())
+    create = (fun _ _ _ -> ())
   } 
 
 let _ =