print_readable/save_out_readable changed:
authorrich <rich>
Wed, 15 Feb 2006 13:25:58 +0000 (13:25 +0000)
committerrich <rich>
Wed, 15 Feb 2006 13:25:58 +0000 (13:25 +0000)
 * ignore single columns - these are usually titles and shouldn't cause
   the rest of the CSV output to expand
 * don't escape strings (why were we doing this?)

csv.ml

diff --git a/csv.ml b/csv.ml
index 0ff0371..2a22bf9 100644 (file)
--- a/csv.ml
+++ b/csv.ml
@@ -1,6 +1,6 @@
 (* csv.ml - comma separated values parser
  *
- * $Id: csv.ml,v 1.7 2005-11-25 14:06:58 rich Exp $
+ * $Id: csv.ml,v 1.8 2006-02-15 13:25:58 rich Exp $
  *)
 
 (* The format of CSV files:
@@ -321,12 +321,20 @@ let save ?separator file csv =
 
 let save_out_readable chan csv =
   (* Escape all the strings in the CSV file first. *)
+  (* XXX Why are we doing this?  I commented it out anyway.
   let csv = List.map (List.map String.escaped) csv in
-
-  let csv = square csv in
+  *)
 
   (* Find the width of each column. *)
   let widths =
+    (* Don't consider rows with only a single element - typically
+     * long titles.
+     *)
+    let csv = List.filter (function [_] -> false | _ -> true) csv in
+
+    (* Square the CSV file - makes the next step simpler to implement. *)
+    let csv = square csv in
+
     match csv with
       | [] -> []
       | r :: _ ->
@@ -348,15 +356,19 @@ let save_out_readable chan csv =
     | i -> f (); repeat f (i-1)
   in
   List.iter (
-    fun row ->
-      let row = List.combine widths row in
-      List.iter (
-       fun (width, cell) ->
-         output_string chan cell;
-         let n = String.length cell in
-         repeat (fun () -> output_char chan ' ') (width - n + 1)
-      ) row;
-      output_char chan '\n'
+    function
+    | [cell] ->                                (* Single column. *)
+       output_string chan cell;
+       output_char chan '\n'
+    | row ->                           (* Other. *)
+       let row = List.combine widths row in
+       List.iter (
+         fun (width, cell) ->
+           output_string chan cell;
+           let n = String.length cell in
+           repeat (fun () -> output_char chan ' ') (width - n + 1)
+       ) row;
+       output_char chan '\n'
   ) csv
 
 let print_readable = save_out_readable stdout