From 9e50696ec75e70b4eb592a9a1b6fceacf169977f Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 10 Apr 2018 08:54:16 +0200 Subject: [PATCH] new 'justify' tool + rename ImageUtils -> img2aa --- ImageUtils.sh | 3 - img2aa | 3 + .../nikiroo/utils/main/StartStringUtils.java | 5 - .../{StartImageUtils.java => img2aa.java} | 16 +++- src/be/nikiroo/utils/main/justify.java | 95 +++++++++++++++++++ src/be/nikiroo/utils/test/Test.java | 8 +- 6 files changed, 113 insertions(+), 17 deletions(-) delete mode 100755 ImageUtils.sh create mode 100755 img2aa delete mode 100644 src/be/nikiroo/utils/main/StartStringUtils.java rename src/be/nikiroo/utils/main/{StartImageUtils.java => img2aa.java} (92%) create mode 100644 src/be/nikiroo/utils/main/justify.java diff --git a/ImageUtils.sh b/ImageUtils.sh deleted file mode 100755 index 8e12b66..0000000 --- a/ImageUtils.sh +++ /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 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 index ba5ccc6..0000000 --- a/src/be/nikiroo/utils/main/StartStringUtils.java +++ /dev/null @@ -1,5 +0,0 @@ -package be.nikiroo.utils.main; - -public class StartStringUtils { - -} diff --git a/src/be/nikiroo/utils/main/StartImageUtils.java b/src/be/nikiroo/utils/main/img2aa.java similarity index 92% rename from src/be/nikiroo/utils/main/StartImageUtils.java rename to src/be/nikiroo/utils/main/img2aa.java index 99f753c..9cc6f0c 100644 --- a/src/be/nikiroo/utils/main/StartImageUtils.java +++ b/src/be/nikiroo/utils/main/img2aa.java @@ -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) *

- * 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 index 0000000..cf58905 --- /dev/null +++ b/src/be/nikiroo/utils/main/justify.java @@ -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) + *

+ *

+ * + * @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 lines = new ArrayList(); + 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) == '»'; + } +} diff --git a/src/be/nikiroo/utils/test/Test.java b/src/be/nikiroo/utils/test/Test.java index 7fe9bbc..e0dfa0f 100644 --- a/src/be/nikiroo/utils/test/Test.java +++ b/src/be/nikiroo/utils/test/Test.java @@ -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; } /** -- 2.27.0