new 'justify' tool + rename ImageUtils -> img2aa
authorNiki Roo <niki@nikiroo.be>
Tue, 10 Apr 2018 06:54:16 +0000 (08:54 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 10 Apr 2018 06:54:16 +0000 (08:54 +0200)
ImageUtils.sh [deleted file]
img2aa [new file with mode: 0755]
src/be/nikiroo/utils/main/StartStringUtils.java [deleted file]
src/be/nikiroo/utils/main/img2aa.java [moved from src/be/nikiroo/utils/main/StartImageUtils.java with 92% similarity]
src/be/nikiroo/utils/main/justify.java [new file with mode: 0644]
src/be/nikiroo/utils/test/Test.java

diff --git a/ImageUtils.sh b/ImageUtils.sh
deleted file mode 100755 (executable)
index 8e12b66..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-exec java -cp nikiroo-utils.jar be.nikiroo.utils.main.StartImageUtils "$@"
-
diff --git a/img2aa b/img2aa
new file mode 100755 (executable)
index 0000000..e689894
--- /dev/null
+++ b/img2aa
@@ -0,0 +1,3 @@
+#!/bin/sh
+exec nikiroo-utils be.nikiroo.utils.main.img2aa "$@"
+
diff --git a/src/be/nikiroo/utils/main/StartStringUtils.java b/src/be/nikiroo/utils/main/StartStringUtils.java
deleted file mode 100644 (file)
index ba5ccc6..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-package be.nikiroo.utils.main;
-
-public class StartStringUtils {
-
-}
similarity index 92%
rename from src/be/nikiroo/utils/main/StartImageUtils.java
rename to src/be/nikiroo/utils/main/img2aa.java
index 99f753c2d83958a4e7cf0f13a928139815bc1687..9cc6f0c6e3afcd37bd8508150cfb0a6734670a8c 100644 (file)
@@ -15,11 +15,17 @@ import be.nikiroo.utils.ui.ImageTextAwt;
 import be.nikiroo.utils.ui.ImageTextAwt.Mode;
 import be.nikiroo.utils.ui.ImageUtilsAwt;
 
-public class StartImageUtils {
+/**
+ * Image to ASCII conversion.
+ * 
+ * @author niki
+ */
+public class img2aa {
        /**
-        * ImageUtils main entry point.
+        * Syntax: (--mode=MODE) (--width=WIDTH) (--height=HEIGHT) (--size=SIZE)
+        * (--output=OUTPUT) (--invert) (--help)
         * <p>
-        * Syntax: see --help
+        * See "--help".
         * 
         * @param args
         */
@@ -47,8 +53,8 @@ public class StartImageUtils {
                                        height = Integer.parseInt(arg.substring("--height="
                                                        .length()));
                                } else if (arg.startsWith("--size=")) {
-                                       String content = arg.substring("--size=".length())
-                                                       .replace("X", "x");
+                                       String content = arg.substring("--size=".length()).replace(
+                                                       "X", "x");
                                        width = Integer.parseInt(content.split("x")[0]);
                                        height = Integer.parseInt(content.split("x")[1]);
                                } else if (arg.startsWith("--ouput=")) {
diff --git a/src/be/nikiroo/utils/main/justify.java b/src/be/nikiroo/utils/main/justify.java
new file mode 100644 (file)
index 0000000..cf58905
--- /dev/null
@@ -0,0 +1,95 @@
+package be.nikiroo.utils.main;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.StringUtils.Alignment;
+
+/**
+ * Text justification (left, right, center, justify).
+ * 
+ * @author niki
+ */
+public class justify {
+       /**
+        * Syntax: $0 ([left|right|center|justify]) (max width)
+        * <p>
+        * <ul>
+        * <li>mode: left, right, center or full justification (defaults to left)</li>
+        * <li>max width: the maximum width of a line, or "" for "no maximum"
+        * (defaults to "no maximum")</li>
+        * </ul>
+        * 
+        * @param args
+        */
+       public static void main(String[] args) {
+               int width = -1;
+               StringUtils.Alignment align = Alignment.LEFT;
+
+               if (args.length >= 1) {
+                       align = Alignment.valueOf(args[0].toUpperCase());
+               }
+               if (args.length >= 2) {
+                       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]");
+               try {
+                       StringBuilder previous = null;
+                       StringBuilder tmp = new StringBuilder();
+                       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);
+                       }
+
+                       if (previous != null) {
+                               lines.add(previous.toString());
+                       }
+               } 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) == 'ยป';
+       }
+}
index 7fe9bbc7c7a1c488e2fd2b1c56eff0ef0fd39f48..e0dfa0fab817788d5100db16451a1df74aeae99b 100644 (file)
@@ -2,8 +2,8 @@ package be.nikiroo.utils.test;
 
 import be.nikiroo.utils.Cache;
 import be.nikiroo.utils.Downloader;
-import be.nikiroo.utils.main.StartImageUtils;
-import be.nikiroo.utils.main.StartStringUtils;
+import be.nikiroo.utils.main.img2aa;
+import be.nikiroo.utils.main.justify;
 
 /**
  * Tests for nikiroo-utils.
@@ -35,8 +35,8 @@ public class Test extends TestLauncher {
                Downloader downloader = null;
 
                // To include the sources:
-               StartImageUtils siu;
-               StartStringUtils ssu;
+               img2aa siu;
+               justify ssu;
        }
 
        /**