]> gitweb.factorcode.org Git - factor.git/blob - extra/rosetta-code/align-columns/align-columns.factor
factor: trim using lists
[factor.git] / extra / rosetta-code / align-columns / align-columns.factor
1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io kernel math math.functions sequences splitting strings ;
4 IN: rosetta.align-columns
5
6 ! http://rosettacode.org/wiki/Align_columns
7
8 ! Given a text file of many lines, where fields within a line
9 ! are delineated by a single 'dollar' character, write a program
10 ! that aligns each column of fields by ensuring that words in each
11 ! column are separated by at least one space. Further, allow for
12 ! each word in a column to be either left justified, right
13 ! justified, or center justified within its column.
14
15 ! Use the following text to test your programs:
16
17 ! Given$a$text$file$of$many$lines,$where$fields$within$a$line$
18 ! are$delineated$by$a$single$'dollar'$character,$write$a$program
19 ! that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
20 ! column$are$separated$by$at$least$one$space.
21 ! Further,$allow$for$each$word$in$a$column$to$be$either$left$
22 ! justified,$right$justified,$or$center$justified$within$its$column.
23
24 ! Note that:
25
26 ! * The example input texts lines may, or may not, have trailing
27 !   dollar characters.
28 ! * All columns should share the same alignment.
29 ! * Consecutive space characters produced adjacent to the end of
30 !   lines are insignificant for the purposes of the task.
31 ! * Output text will be viewed in a mono-spaced font on a plain
32 !   text editor or basic terminal.
33 ! * The minimum space between columns should be computed from
34 !   the text and not hard-coded.
35 ! * It is not a requirement to add separating characters between
36 !   or around columns.
37
38 CONSTANT: example-text "Given$a$text$file$of$many$lines,$where$fields$within$a$line$
39 are$delineated$by$a$single$'dollar'$character,$write$a$program
40 that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
41 column$are$separated$by$at$least$one$space.
42 Further,$allow$for$each$word$in$a$column$to$be$either$left$
43 justified,$right$justified,$or$center$justified$within$its$column."
44
45 : split-and-pad ( text -- lines )
46     split-lines [ "$" split harvest ] map
47     dup longest length
48     '[ _ "" pad-tail ] map ;
49
50 : column-widths ( columns -- widths )
51     [ longest length ] map ;
52
53 SINGLETONS: +left+ +middle+ +right+ ;
54
55 GENERIC: align-string ( str n alignment -- str' )
56
57 M: +left+ align-string  drop CHAR: space pad-tail ;
58 M: +right+ align-string drop CHAR: space pad-head ;
59
60 M: +middle+ align-string
61     drop
62     over length - 2 /
63     [ floor CHAR: space <string> ]
64     [ ceiling CHAR: space <string> ] bi surround ;
65
66 : align-columns ( columns alignment -- columns' )
67     [ dup column-widths ] dip '[
68         [ _ align-string ] curry map
69     ] 2map ;
70
71 : print-aligned ( text alignment -- )
72     [ split-and-pad flip ] dip align-columns flip
73     [ [ write bl ] each nl ] each ;
74
75 ! USAGE: example-text +left+ print-aligned