Move justify List into StringUtils (tests needed)
[nikiroo-utils.git] / src / be / nikiroo / utils / main / justify.java
CommitLineData
9e50696e
NR
1package be.nikiroo.utils.main;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Scanner;
6
7import be.nikiroo.utils.StringUtils;
8import be.nikiroo.utils.StringUtils.Alignment;
9
10/**
11 * Text justification (left, right, center, justify).
12 *
13 * @author niki
14 */
15public class justify {
16 /**
17 * Syntax: $0 ([left|right|center|justify]) (max width)
18 * <p>
19 * <ul>
20 * <li>mode: left, right, center or full justification (defaults to left)</li>
21 * <li>max width: the maximum width of a line, or "" for "no maximum"
22 * (defaults to "no maximum")</li>
23 * </ul>
24 *
25 * @param args
26 */
27 public static void main(String[] args) {
28 int width = -1;
29 StringUtils.Alignment align = Alignment.LEFT;
30
31 if (args.length >= 1) {
32 align = Alignment.valueOf(args[0].toUpperCase());
33 }
34 if (args.length >= 2) {
35 width = Integer.parseInt(args[1]);
36 }
37
9e50696e 38 Scanner scan = new Scanner(System.in);
67e9a06e 39 scan.useDelimiter("\r\n|[\r\n]");
9e50696e 40 try {
dc22eb95 41 List<String> lines = new ArrayList<String>();
9e50696e 42 while (scan.hasNext()) {
dc22eb95 43 lines.add(scan.next());
9e50696e
NR
44 }
45
dc22eb95
NR
46 for (String line : StringUtils.justifyText(lines, width, align)) {
47 System.out.println(line);
9e50696e
NR
48 }
49 } finally {
50 scan.close();
51 }
9e50696e
NR
52 }
53}