Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/pets-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>rickwporter.prettytable</groupId>
<artifactId>pets-cli</artifactId>
<version>0.1.0</version>
<version>0.1.1</version>
<name>pets-cli</name>
<description>CLI for the OpenAPI Petstore, rendered with prettytable.</description>

Expand All @@ -13,7 +13,7 @@
<version.picocli>4.7.6</version.picocli>
<version.jackson>2.18.2</version.jackson>
<version.junit>5.14.1</version.junit>
<version.prettytable>1.0.0</version.prettytable>
<version.prettytable>1.1.0</version.prettytable>

<!-- Checkstyle -->
<version.plugin.checkstyle>3.6.0</version.plugin.checkstyle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import java.util.Set;
import java.util.concurrent.Callable;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
Expand All @@ -20,9 +25,8 @@
import picocli.CommandLine.ParentCommand;
import picocli.CommandLine.ScopeType;
import picocli.CommandLine.Spec;

import rickwporter.prettytable.PrettyTable;
import rickwporter.prettytable.PrettyTable.OutputFormat;
import rickwporter.prettytable.OutputFormat;
import rickwporter.prettytable.TableBuilder;

@Command(
Expand Down Expand Up @@ -138,19 +142,34 @@ protected int usage(String message) {
return CommandLine.ExitCode.USAGE;
}

public String renderJson(Object body) {
DefaultIndenter indent = new DefaultIndenter(" ", "\n");
DefaultPrettyPrinter printer = new DefaultPrettyPrinter()
.withArrayIndenter(indent)
.withObjectIndenter(indent);
try {
return new ObjectMapper().writer(printer).writeValueAsString(body);
} catch (JsonProcessingException ex) {
throw new IllegalStateException("Could not render JSON", ex);
}
}

private String render(Object body) {
if (body == null) {
return "";
}
if (body instanceof String || body instanceof Number || body instanceof Boolean) {
return String.valueOf(body);
}
PrettyTable table = new TableBuilder().build(body);
if (this.parent.format == OutputFormat.JSON) {
return this.renderJson(body);
}

PrettyTable table = new TableBuilder().build(this.parent.format, body);
if (table == null) {
return "";
}
OutputFormat format = this.parent.format;
return table.formattedString(format == null ? OutputFormat.RICH : format);
return table.toString();
}

protected int show(Object body) {
Expand Down
Loading