Compare commits

..

10 Commits

Author SHA1 Message Date
Jonathan Bernard
4b3c20a913 Added AnsiEscapeCodeSequence.strip() to get the undecorated string. 2016-03-11 07:33:37 -06:00
Jonathan Bernard
bbef072f8b Added AnsiEscapeCodeSequence: better support for ANSI Escape codes. 2016-02-10 02:21:54 -06:00
Jonathan Bernard
dcef7512ce Add support for Markdown in javadoc (groovydoc is a different story). 2016-02-09 15:05:22 -06:00
Jonathan Bernard
51a0cdbc08 LightOptionParser: updated tests to match new behavior. 2015-12-31 23:18:57 -06:00
Jonathan Bernard
7fb35060c2 LightOptionParser: multiple options with arguments.
Previously if there was an option which took multiple arguments, the parser
always returned a flat array of all values given for that option, regardless of
how many arguments it expected. E.g. the following:

    cmd --file-pair f1 f2 --file-pair a b

resulted in the following:

    file-pair: [ f1, f2, a, b ]

For the case where the option is defined as taking only one argument, this
behavior is unchanged, but for the cases where the option is defined as taking
more than one option, an array of arrays is returned instead. For the above
example the result is now:

    file-pair: [ [f1, f2], [a, b] ]

The behavior is the same for options with variable arguments. The following:

    cmd --set f1 f2 --set a b c --set last

results in:

    file-pair: [ [f1, f2], [a, b, c], [last] ]
2015-12-11 23:08:53 -06:00
Jonathan Bernard
5ce1cfab88 Merged in v3.11 2015-11-14 00:25:30 -06:00
Jonathan Bernard
5b25794f5d Removed GroovyDirectoryServer (became it's own project). 2015-09-24 08:32:54 -05:00
Jonathan Bernard
f85050f0f2 Changed ConsoleProgressBar to use longs internally. 2015-08-06 20:32:41 -05:00
Jonathan Bernard
f2bc4b34d0 Reworked GroovyDirectoryServer into a proper class. 2015-07-08 13:56:05 -05:00
Jonathan Bernard
be4d89abff Added useful dependencies for GroovyDirectoryServer. 2015-07-07 13:46:36 -05:00
6 changed files with 189 additions and 51 deletions

View File

@ -1,18 +1,31 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'ch.raffael.pegdown-doclet:pegdown-doclet:1.2'
}
}
apply plugin: "groovy"
apply plugin: "ch.raffael.pegdown-doclet"
apply plugin: "maven"
group = "com.jdbernard"
version = "3.9"
version = "4.4"
repositories {
mavenCentral() }
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.6'
compile localGroovy()
compile 'org.slf4j:slf4j-api:1.7.10'
compile 'ch.qos.logback:logback-core:1.1.2'
compile 'ch.qos.logback:logback-classic:1.1.2'
compile 'org.eclipse.jetty.aggregate:jetty-all:7.6.15.v20140411'
testCompile 'junit:junit:4.12'
}

View File

@ -1,33 +0,0 @@
#!/usr/bin/env groovy
package com.jdbernard.net
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.*
import groovy.servlet.*
def startJetty(int port) {
def server = new Server(port)
def handler = new ServletContextHandler(ServletContextHandler.SESSIONS)
handler.contextPath = '/'
handler.resourceBase = '.'
// Groovy Scripts
handler.addServlet(GroovyServlet, '*.groovy')
// Files
def filesHolder = handler.addServlet(DefaultServlet, '/')
filesHolder.setInitParameter('resourceBase', '.')
server.handler = handler
server.start()
}
if (args.length < 1) {
println "Usage: webServer.groovy <port>"
System.exit(1) }
println "Starting Jetty, press Ctrl-C to stop."
startJetty(Integer.parseInt(args[0]))

View File

@ -11,17 +11,17 @@ package com.jdbernard.util
class ConsoleProgressBar {
int MAX_STEP = 30
int max = 10
long max = 10
def out = System.out
private int lastStepAmount = -1
private String lastLinePrinted = ""
private String lastInfo = ""
private long startTime
public void setMax(int max) {
public void setMax(long max) {
this.max = Math.max(max, 1) }
void update(int value, String info) {
void update(long value, String info) {
if (value == 0 || startTime == 0)
startTime = System.currentTimeMillis()
@ -43,8 +43,8 @@ class ConsoleProgressBar {
remTime /= 1000
def numEq = Math.max(curStep - 1, 0)
def remMin = curPercent < 0.05 ? '?' : (int) (remTime / 60)
def remSec = curPercent < 0.05 ? '?' : (int) (((remTime / 60.0) - remMin) * 60)
def remMin = curPercent < 0.05 ? '?' : (long) (remTime / 60)
def remSec = curPercent < 0.05 ? '?' : (long) (((remTime / 60.0) - remMin) * 60)
lastInfo = info
if (info.length() > 16) info = info[0..<16]

View File

@ -170,12 +170,13 @@ public class LightOptionParser {
else {
if (!returnOpts.containsKey(optName))
returnOpts[optName] = []
returnOpts[optName] += retVal
if (optDef.longName) {
if (!returnOpts.containsKey(optDef.longName))
returnOpts[optDef.longName] = []
returnOpts[optDef.longName] += retVal } } }
if (optDef.arguments == 1) returnOpts[optName] += retVal
else returnOpts[optName] << retVal
if (optDef.longName)
returnOpts[optDef.longName] = returnOpts[optName] } }
/// This was not as option, it is an unclaomed argument.
else { returnOpts.args << args[i]; i++ } }

View File

@ -0,0 +1,157 @@
/**
* # AnsiEscapeCodeSequence
* @author Jonathan Bernard (jdbernard@gmail.com)
* @org jdbernard.com/util/AnsiEscapeCodeSequence
* @copyright 2016 Jonathan Bernard
*/
package com.jdbernard.util;
import java.util.regex.Pattern;
/**
* The AnsiEscapeCodeSequence class is an extended wrapper around
* [ANSI escape codes].
*
* [ANSI escape codes]: http://en.wikipedia.org/wiki/ANSI_escape_code
*/
public class AnsiEscapeCodeSequence {
public static final String CSI = "\u001b[";
public static final Pattern ANSI_SEQ_PATTERN =
Pattern.compile("\u001b\\[[^a-zA-Z]+[a-zA-Z]");
public static enum Colors {
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, EXT, DEFAULT }
public static enum Erase { ToEnd, ToBeginning, All }
public static String strip(String input) {
return ANSI_SEQ_PATTERN.matcher(input).replaceAll(""); }
public StringBuilder value = new StringBuilder();
public String toString() { return value.toString(); }
// Multiple methods for various forms of SGR
public AnsiEscapeCodeSequence color(Colors fg, Colors bg, boolean bright) {
value.append(CSI);
value.append(bright ? "1" : "22");
if (fg != null) {
value.append(";3");
value.append(Integer.toString(fg.ordinal())); }
if (bg != null) {
value.append(";4");
value.append(Integer.toString(bg.ordinal())); }
value.append("m");
return this; }
public AnsiEscapeCodeSequence resetText() {
value.append(CSI).append("0m");
return this; }
public AnsiEscapeCodeSequence blinkSlow() {
value.append(CSI).append("5m");
return this; }
public AnsiEscapeCodeSequence blinkFast() {
value.append(CSI).append("6m");
return this; }
public AnsiEscapeCodeSequence invertColors() {
value.append(CSI).append("7m");
return this; }
public AnsiEscapeCodeSequence strikethrough() {
value.append(CSI).append("9m");
return this; }
// Other ANSI escape codes
public AnsiEscapeCodeSequence cursorUp() { return cursorUp(1); }
public AnsiEscapeCodeSequence cursorUp(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("A");
return this; }
public AnsiEscapeCodeSequence cursorDown() { return cursorDown(1); }
public AnsiEscapeCodeSequence cursorDown(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("B");
return this; }
public AnsiEscapeCodeSequence cursorForward() { return cursorForward(1); }
public AnsiEscapeCodeSequence cursorForward(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("C");
return this; }
public AnsiEscapeCodeSequence cursorBackward() { return cursorBackward(1); }
public AnsiEscapeCodeSequence cursorBackward(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("D");
return this; }
public AnsiEscapeCodeSequence cursorNextLine() { return cursorNextLine(1); }
public AnsiEscapeCodeSequence cursorNextLine(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("E");
return this; }
public AnsiEscapeCodeSequence cursorPrevLine() { return cursorPrevLine(1); }
public AnsiEscapeCodeSequence cursorPrevLine(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("F");
return this; }
public AnsiEscapeCodeSequence cursorHorizontalAbsolute(int column) {
value.append(CSI).append(Integer.toString(column)).append("G");
return this; }
public AnsiEscapeCodeSequence cursorPosition(int row, int column) {
value.append(CSI)
.append(Integer.toString(row))
.append(";")
.append(Integer.toString(column))
.append("H");
return this; }
public AnsiEscapeCodeSequence eraseDisplay() {
return eraseDisplay(Erase.ToEnd); }
public AnsiEscapeCodeSequence eraseDisplay(Erase how) {
value.append(CSI)
.append(Integer.toString(how.ordinal()))
.append("J");
return this; }
public AnsiEscapeCodeSequence eraseLine() { return eraseLine(Erase.ToEnd); }
public AnsiEscapeCodeSequence eraseLine(Erase how) {
value.append(CSI)
.append(Integer.toString(how.ordinal()))
.append("K");
return this; }
public AnsiEscapeCodeSequence scrollUp() { return scrollUp(1); }
public AnsiEscapeCodeSequence scrollUp(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("S");
return this; }
public AnsiEscapeCodeSequence scrollDown() { return scrollDown(1); }
public AnsiEscapeCodeSequence scrollDown(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("T");
return this; }
public AnsiEscapeCodeSequence cursorHVPosition(int row, int column) {
value.append(CSI)
.append(Integer.toString(row))
.append(";")
.append(Integer.toString(column))
.append("f");
return this; }
public AnsiEscapeCodeSequence saveCursor() {
value.append(CSI).append("s");
return this; }
public AnsiEscapeCodeSequence restoreCursor() {
value.append(CSI).append("u");
return this; }
}

View File

@ -58,8 +58,8 @@ public class LightOptionParserTests extends GroovyTestCase {
assert opts.c == ["cfgFile"]
assert opts['config-file'] == ["cfgFile"]
assert opts.args == ["arg1", "arg2"]
assert opts.i == ["in1", "in2", "in3", "in4"]
assert opts["input-file"] == ["in1", "in2", "in3", "in4"]
assert opts.o == ["out1", "out2"]
assert opts["output-file2"] == ["out1", "out2"] }
assert opts.i == [["in1", "in2", "in3"], ["in4"]]
assert opts["input-file"] == [["in1", "in2", "in3"], ["in4"]]
assert opts.o == [["out1", "out2"]]
assert opts["output-file2"] == [["out1", "out2"]] }
}