com.codetaco.cli.impl.usage.UsageBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cli Show documentation
Show all versions of cli Show documentation
A command line parser for Java
The newest version!
package com.codetaco.cli.impl.usage;
import com.codetaco.cli.impl.ICmdLine;
abstract public class UsageBuilder {
static final private int MaxLineLength = 80;
public static String newline = System.getProperty("line.separator");
public static Object getWriter(final ICmdLine arg, final int verboseLevel) {
final UsageBuilder ub;
switch (verboseLevel) {
case 0:
ub = new UsageBuilderLevel1();
break;
case 1:
ub = new UsageBuilderLevel1();
break;
case 2:
ub = new UsageBuilderLevel2();
break;
case 3:
ub = new UsageBuilderLevel3();
break;
default:
ub = new UsageBuilderLevel1();
break;
}
ub.prettyPrint(arg);
return ub;
}
final private StringBuilder sb;
int currentIndentLevelForWrapping;
int currentLineLength;
int allignment;
UsageBuilder() {
sb = new StringBuilder();
}
boolean allign() {
boolean aSpaceWasAdded = false;
for (int x = currentLineLength; x < allignment; x++) {
append(" ");
aSpaceWasAdded = true;
}
return aSpaceWasAdded;
}
void allign(final int column) {
allignment = column;
if (!allign()) {
append(" ");
}
}
public UsageBuilder append(
final String value) {
final String[] lines = value.split("\\n", -1);
String remainderOfLongLine = null;
String aLine = null;
for (int line = 0; remainderOfLongLine != null || line < lines.length; line++) {
if (remainderOfLongLine != null) {
aLine = remainderOfLongLine;
/*
* don't use up the next line until remainder is finished.
*/
line--;
} else {
aLine = lines[line];
if (line != 0) {
newLine();
}
}
if (currentLineLength == 0) {
indent();
}
if (currentLineLength + aLine.length() >= MaxLineLength) {
final String[] parts = split(aLine, MaxLineLength - currentLineLength);
sb.append(parts[0]);
newLine();
remainderOfLongLine = parts[1];
continue;
}
remainderOfLongLine = null;
sb.append(aLine);
currentLineLength += aLine.length();
}
return this;
}
int getIndentSize() {
return 4;
}
void indent() {
indent(currentIndentLevelForWrapping);
}
void indent(final int indentLevel) {
if (allignment > 0) {
for (int a = currentLineLength; a < allignment; a++) {
sb.append(" ");
}
currentLineLength = allignment;
} else {
for (int i = 0; i < indentLevel; i++) {
for (int s = 0; s < getIndentSize(); s++) {
sb.append(" ");
}
}
currentLineLength = indentLevel * getIndentSize();
}
}
UsageBuilder newLine() {
return newLine(currentIndentLevelForWrapping);
}
UsageBuilder newLine(
final int indentLevel) {
currentIndentLevelForWrapping = indentLevel;
currentLineLength = 0;
sb.append(newline);
return this;
}
abstract void prettyPrint(final ICmdLine arg);
public void setIndentLevel(final int indentLevel) {
currentIndentLevelForWrapping = indentLevel;
}
String[] split(final String line, final int availableBytesLeft) {
int splitPoint = 0;
for (int b = 0; b < availableBytesLeft; b++)
/*
* look for the last space within the printable part of the line.
*/ {
if (line.charAt(b) == ' ') {
splitPoint = b;
}
}
return new String[]{
line.substring(0, splitPoint).trim(),
line.substring(splitPoint).trim()
};
}
@Override
public String toString() {
return sb.toString().trim();
}
void unallign() {
allignment = 0;
}
}