cf589059a5ebf398cea085fcfc06913067e32d73
1 package be
.nikiroo
.utils
.main
;
3 import java
.util
.ArrayList
;
5 import java
.util
.Scanner
;
7 import be
.nikiroo
.utils
.StringUtils
;
8 import be
.nikiroo
.utils
.StringUtils
.Alignment
;
11 * Text justification (left, right, center, justify).
15 public class justify
{
17 * Syntax: $0 ([left|right|center|justify]) (max width)
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>
27 public static void main(String
[] args
) {
29 StringUtils
.Alignment align
= Alignment
.LEFT
;
31 if (args
.length
>= 1) {
32 align
= Alignment
.valueOf(args
[0].toUpperCase());
34 if (args
.length
>= 2) {
35 width
= Integer
.parseInt(args
[1]);
38 // TODO: move to utils?
39 List
<String
> lines
= new ArrayList
<String
>();
40 Scanner scan
= new Scanner(System
.in
);
41 scan
.useDelimiter("[\r\n]");
43 StringBuilder previous
= null;
44 StringBuilder tmp
= new StringBuilder();
45 while (scan
.hasNext()) {
46 String current
= scan
.next();
48 for (String word
: current
.split(" ")) {
53 if (tmp
.length() > 0) {
56 tmp
.append(word
.trim());
58 current
= tmp
.toString();
60 if (previous
== null) {
61 previous
= new StringBuilder();
63 if (current
.isEmpty() || isFullLine(previous
)) {
64 lines
.add(previous
.toString());
65 previous
.setLength(0);
71 previous
.append(current
);
74 if (previous
!= null) {
75 lines
.add(previous
.toString());
81 // TODO: supports bullet lines "- xxx" and sub levels
82 for (String line
: lines
) {
83 for (String subline
: StringUtils
.justifyText(line
, width
, align
)) {
84 System
.out
.println(subline
);
89 static private boolean isFullLine(StringBuilder line
) {
90 return line
.length() == 0 //
91 || line
.charAt(line
.length() - 1) == '.'
92 || line
.charAt(line
.length() - 1) == '"'
93 || line
.charAt(line
.length() - 1) == 'ยป';