Skip to content

Commit

Permalink
Update OutputFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Nov 18, 2016
1 parent ef427dc commit 6f073c1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ private YamlFormatter(Yaml yaml, final OutputFormatter base) {
this.base = base;
}

@Override
public OutputFormatter withBase(final OutputFormatter base) {
return new YamlFormatter(this.yaml, base);
}

/**
* @param base base formatter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ public class ANSIColorOutput implements CommandOutput, OutputFormatter {
private static final Object RED = ESC + "[31m";
private static final Object YELLOW = ESC + "[33m";

private OutputFormatter base;
SystemOutput sink;

public ANSIColorOutput(final SystemOutput sink) {
this.sink = sink;
}

public ANSIColorOutput(final SystemOutput sink, OutputFormatter base) {
this.sink = sink;
this.base = base;
}

@Override
public OutputFormatter withBase(final OutputFormatter base) {
return new ANSIColorOutput(sink, base);
}

@Override
public String format(final Object o) {
return toColors(o);
Expand All @@ -28,13 +39,17 @@ public void output(final Object object) {
}

public static String toColors(final Object object) {
return toColors(object, null);
}

public static String toColors(final Object object, OutputFormatter base) {
if (null == object) {
return null;
}
if (ColorString.class.isAssignableFrom(object.getClass())) {
ColorString object1 = (ColorString) object;
Set<ColorArea> colors = object1.getColors();
String string = object1.toString();
String string = null != base ? base.format(object1) : object1.toString();
int cur = 0;
int count = 0;
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -66,7 +81,7 @@ public static String toColors(final Object object) {
}
return sb.toString();
} else {
return object.toString();
return null != base ? base.format(object) : object.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public NiceFormatter(final OutputFormatter base) {
this.base = base;
}

@Override
public OutputFormatter withBase(final OutputFormatter base) {
this.base = base;
return this;
}

@Override
public String format(final Object o) {
if (o instanceof Map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
*/
public interface OutputFormatter {
String format(Object o);

OutputFormatter withBase(OutputFormatter base);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public class ToStringFormatter implements OutputFormatter {
public String format(final Object o) {
return o != null ? o.toString() : null;
}

@Override
public OutputFormatter withBase(final OutputFormatter base) {
return base;
}
}
25 changes: 21 additions & 4 deletions toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class ToolBelt {
private CommandInput inputParser;
private Set<String> helpCommands;
private CommandOutput commandOutput;
private OutputFormatter baseFormatter;
private OutputFormatter formatter;
private boolean ansiColor;

/**
* Create a simple CLI tool for the object, using {@link SimpleCommandInput} to parse
Expand Down Expand Up @@ -94,7 +96,6 @@ public static ToolBelt belt(String name) {
protected ToolBelt(String name) {
commands = new CommandSet(name);
helpCommands = new HashSet<>();
formatter = new NiceFormatter(new ToStringFormatter());
}

/**
Expand Down Expand Up @@ -147,8 +148,7 @@ public ToolBelt systemOutput() {
* @return this builder
*/
public ToolBelt ansiColorOutput(boolean enabled) {
commandOutput(enabled ? new ANSIColorOutput(new SystemOutput()) : new SystemOutput());
formatter = new NiceFormatter(enabled ? new ANSIColorOutput(null) : new ToStringFormatter());
ansiColor = true;
return this;
}

Expand All @@ -171,6 +171,16 @@ public ToolBelt commandOutput(CommandOutput output) {
return this;
}

/**
* Format output data with this formatter
*
* @return this
*/
public ToolBelt formatter(OutputFormatter formatter) {
this.formatter = formatter;
return this;
}

private static class CommandContext {
private CommandInput inputParser;
private CommandOutput output;
Expand Down Expand Up @@ -586,8 +596,15 @@ public ToolBelt finalOutput(CommandOutput output) {
return this;
}
public CommandOutput finalOutput() {
if (null == commandOutput) {
commandOutput(ansiColor ? new ANSIColorOutput(new SystemOutput()) : new SystemOutput());
}
baseFormatter = new NiceFormatter(ansiColor ? new ANSIColorOutput(null) : new ToStringFormatter());
if (null == builtOutput) {
builtOutput = new FormattedOutput(commandOutput, formatter);
builtOutput = new FormattedOutput(
commandOutput,
null != formatter ? formatter.withBase(baseFormatter) : baseFormatter
);
}
return builtOutput;
}
Expand Down

0 comments on commit 6f073c1

Please sign in to comment.