Move justify List into StringUtils (tests needed)
[nikiroo-utils.git] / src / be / nikiroo / utils / main / justify.java
index cf589059a5ebf398cea085fcfc06913067e32d73..2a83389ea047f75838ce7c46b5e288aedb0f7d1c 100644 (file)
@@ -35,61 +35,19 @@ public class justify {
                        width = Integer.parseInt(args[1]);
                }
 
-               // TODO: move to utils?
-               List<String> lines = new ArrayList<String>();
                Scanner scan = new Scanner(System.in);
-               scan.useDelimiter("[\r\n]");
+               scan.useDelimiter("\r\n|[\r\n]");
                try {
-                       StringBuilder previous = null;
-                       StringBuilder tmp = new StringBuilder();
+                       List<String> lines = new ArrayList<String>();
                        while (scan.hasNext()) {
-                               String current = scan.next();
-                               tmp.setLength(0);
-                               for (String word : current.split(" ")) {
-                                       if (word.isEmpty()) {
-                                               continue;
-                                       }
-
-                                       if (tmp.length() > 0) {
-                                               tmp.append(' ');
-                                       }
-                                       tmp.append(word.trim());
-                               }
-                               current = tmp.toString();
-
-                               if (previous == null) {
-                                       previous = new StringBuilder();
-                               } else {
-                                       if (current.isEmpty() || isFullLine(previous)) {
-                                               lines.add(previous.toString());
-                                               previous.setLength(0);
-                                       } else {
-                                               previous.append(' ');
-                                       }
-                               }
-
-                               previous.append(current);
+                               lines.add(scan.next());
                        }
 
-                       if (previous != null) {
-                               lines.add(previous.toString());
+                       for (String line : StringUtils.justifyText(lines, width, align)) {
+                               System.out.println(line);
                        }
                } finally {
                        scan.close();
                }
-
-               // TODO: supports bullet lines "- xxx" and sub levels
-               for (String line : lines) {
-                       for (String subline : StringUtils.justifyText(line, width, align)) {
-                               System.out.println(subline);
-                       }
-               }
-       }
-
-       static private boolean isFullLine(StringBuilder line) {
-               return line.length() == 0 //
-                               || line.charAt(line.length() - 1) == '.'
-                               || line.charAt(line.length() - 1) == '"'
-                               || line.charAt(line.length() - 1) == 'ยป';
        }
 }