From ae6f56ba8e80a1c05aa076404dd14145b306780a Mon Sep 17 00:00:00 2001 From: rich Date: Wed, 15 Feb 2006 13:25:58 +0000 Subject: [PATCH] print_readable/save_out_readable changed: * 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 | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/csv.ml b/csv.ml index 0ff0371..2a22bf9 100644 --- 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 -- 1.8.3.1