From d34594c53e42b3c83ce061143a105045cfa876c6 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 23 Jun 2018 15:56:14 +0700 Subject: [PATCH] Support for more escape codes --- source/net/filebot/Logging.java | 17 ++++++----------- source/net/filebot/cli/ArgumentProcessor.java | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/source/net/filebot/Logging.java b/source/net/filebot/Logging.java index f5fe2214..e397b01d 100644 --- a/source/net/filebot/Logging.java +++ b/source/net/filebot/Logging.java @@ -138,13 +138,10 @@ public final class Logging { @Override public String format(LogRecord record) { - StringWriter buffer = new StringWriter(); + EscapeCode color = getColor(record.getLevel().intValue()); // BEGIN COLOR - EscapeCode color = getColor(record.getLevel().intValue()); - if (color != null) { - buffer.append(color.begin); - } + StringWriter buffer = new StringWriter().append(color.begin); // MESSAGE String message = record.getMessage(); @@ -166,11 +163,7 @@ public final class Logging { } // END COLOR - if (color != null) { - buffer.append(color.end); - } - - return buffer.append(System.lineSeparator()).toString(); + return buffer.append(color.end).append(System.lineSeparator()).toString(); } public EscapeCode getColor(int level) { @@ -187,7 +180,7 @@ public final class Logging { return EscapeCode.CHERRY_RED; // SEVERE } - return null; // NO COLOR + return EscapeCode.NONE; // NO COLOR } } @@ -232,6 +225,8 @@ public final class Logging { public static final EscapeCode UNDERLINE = newFontStyle(4); public static final EscapeCode STRIKEOUT = newFontStyle(9); + public static final EscapeCode NONE = new EscapeCode("", ""); + public static EscapeCode newColorStyle(int color) { return new EscapeCode("38;5;" + color); } diff --git a/source/net/filebot/cli/ArgumentProcessor.java b/source/net/filebot/cli/ArgumentProcessor.java index 3af56a1f..0d64fe0b 100644 --- a/source/net/filebot/cli/ArgumentProcessor.java +++ b/source/net/filebot/cli/ArgumentProcessor.java @@ -19,10 +19,11 @@ import javax.script.SimpleBindings; import org.apache.commons.io.IOUtils; import net.filebot.LicenseError; +import net.filebot.Logging.EscapeCode; public class ArgumentProcessor { - public int run(ArgumentBean args) throws Exception { + public int run(ArgumentBean args) { try { // interactive mode enables basic selection and confirmation dialogs in the CLI CmdlineInterface cli = args.isInteractive() ? new CmdlineOperationsTextUI() : new CmdlineOperations(); @@ -41,7 +42,7 @@ public class ArgumentProcessor { } catch (LicenseError e) { log.severe("License Error: " + e.getMessage()); if (LICENSE.isFile()) { - log.info(format(IOUtils.toString(getClass().getResource("Stegosaurus.format"), UTF_8), getPurchaseURL())); + printStegosaurus(); log.severe("FileBot requires a valid license. Please run `filebot --license *.psm` to install your FileBot license."); } return 2; @@ -130,6 +131,17 @@ public class ArgumentProcessor { }).sum() == 0 ? 1 : 0; } + private void printStegosaurus() { + try { + String format = IOUtils.toString(getClass().getResource("Stegosaurus.format"), UTF_8); + EscapeCode style = EscapeCode.isSupported() ? EscapeCode.UNDERLINE : EscapeCode.NONE; + String url = getPurchaseURL(); + log.info(format(format, style.apply(url))); + } catch (Exception e) { + debug.log(Level.WARNING, e::toString); + } + } + public void runScript(CmdlineInterface cli, ArgumentBean args) throws Throwable { Bindings bindings = new SimpleBindings(); bindings.put(ScriptShell.SHELL_ARGS_BINDING_NAME, args);