Commit | Line | Data |
---|---|---|
9e50696e NR |
1 | package be.nikiroo.utils.main; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | import java.util.Scanner; | |
6 | ||
7 | import be.nikiroo.utils.StringUtils; | |
8 | import be.nikiroo.utils.StringUtils.Alignment; | |
9 | ||
10 | /** | |
11 | * Text justification (left, right, center, justify). | |
12 | * | |
13 | * @author niki | |
14 | */ | |
15 | public 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 | } |