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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Here's an example of the standard **text** output:
+------------+----------+------------+
```

The `PrettyTable` class handles a table as shown above. The `TableBuilder` class can be used to turn a `Map` or `List` object into a `PrettyTable`. `PrettyObject` also renders similar data types directly to a string with special handling for **json** and **yaml**.
The `PrettyTable` class handles a table as shown above. The `TableBuilder` class can be used to turn a `Map` or `List` object into a `PrettyTable`. `DisplayBuilder` also renders similar data types directly to a string with special handling for **json** and **yaml**.

## PrettyTable Usage
Here are the basics for how to create a `PrettyTable` as shown above:
Expand Down Expand Up @@ -411,13 +411,13 @@ import rickwporter.prettytable.BuilderConfig;
| `setUrlPrefixes` | `http://`, `https://`, `ftp://` |
| `setColumns` | `null` |

## PrettyObject Usage
`PrettyObject` renders a `Map` or a `List` to a string. The constructor takes an optional `BuilderConfig`. The output format and the data are passed to `render`.
## DisplayBuilder Usage
`DisplayBuilder` renders a `Map` or a `List` to a string. The constructor takes an optional `BuilderConfig`. The output format and the data are passed to `render`.

The `PrettyObject` adds some special handling for other data types, and **json** and **yaml** output formats. The **json** and **yaml** output formats render the object directly, while the other types build/render the data into a `PrettyTable` using the `TableBuilder` with provided configuration.
The `DisplayBuilder` adds some special handling for other data types, and **json** and **yaml** output formats. The **json** and **yaml** output formats render the object directly, while the other types build/render the data into a `PrettyTable` using the `TableBuilder` with provided configuration.

```Java
import rickwporter.prettytable.PrettyObject;
import rickwporter.prettytable.DisplayBuilder;
import rickwporter.prettytable.OutputFormat;

Map<String, Object> item = new LinkedHashMap<String, Object>();
Expand All @@ -426,7 +426,7 @@ import rickwporter.prettytable.OutputFormat;
item.put("jkl", Arrays.asList("mno", "pqr", "stu"));
item.put("yxa", null);

PrettyObject pretty = new PrettyObject();
DisplayBuilder pretty = new DisplayBuilder();
System.out.println(pretty.render(OutputFormat.TEXT, item));
```

Expand Down Expand Up @@ -461,12 +461,12 @@ A `BuilderConfig` changes the table formats. **json** and **yaml** keep the obje
import rickwporter.prettytable.BuilderConfig;

BuilderConfig config = new BuilderConfig().setPropertyLabel("Field");
System.out.println(new PrettyObject(config).render(OutputFormat.RICH, item));
System.out.println(new DisplayBuilder(config).render(OutputFormat.RICH, item));
```

## Example

[`examples/pets-cli`](examples/pets-cli) is a [picocli](https://picocli.info/) client for the modified [OpenAPI Petstore](examples/pets.yaml). It prints JSON responses with `TableBuilder`. See the example README for how to build and run it.
[`examples/pets-cli`](examples/pets-cli) is a [picocli](https://picocli.info/) client for the modified [OpenAPI Petstore](examples/pets.yaml). It prints JSON responses with `DisplayBuilder`. See the example README for how to build and run it.

## Development

Expand Down
2 changes: 1 addition & 1 deletion examples/pets-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pets

A [picocli](https://picocli.info/) command line client for the Swagger Petstore in [`pet.yaml`](pet.yaml). Responses are printed with `TableBuilder`.
A [picocli](https://picocli.info/) command line client for the Swagger Petstore in [`pet.yaml`](pet.yaml). Responses are printed with `DisplayBuilder`.

The default server is `http://petstore.swagger.io/v1`. A flag on the command line wins over the environment variable.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import picocli.CommandLine.ParentCommand;
import picocli.CommandLine.ScopeType;
import picocli.CommandLine.Spec;
import rickwporter.prettytable.PrettyObject;
import rickwporter.prettytable.DisplayBuilder;
import rickwporter.prettytable.OutputFormat;

@Command(
Expand Down Expand Up @@ -137,7 +137,7 @@ protected int usage(String message) {
}

private String render(Object body) {
PrettyObject pretty = new PrettyObject();
DisplayBuilder pretty = new DisplayBuilder();
return pretty.render(this.parent.format, body);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rickwporter/prettytable/BuilderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Objects;

/**
* Labels, truncation limits, and column selection for {@link TableBuilder} and {@link PrettyObject}.
* Labels, truncation limits, and column selection for {@link TableBuilder} and {@link DisplayBuilder}.
* Each setter returns this config so calls can be chained. String setters reject {@code null}.
* List setters store a copy of the given list.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* {@link String#valueOf(Object)}. JSON and YAML write the value with Jackson. Every other format
* builds a {@link PrettyTable} from a {@link java.util.Map} or {@link java.util.List}.
*/
public final class PrettyObject {
public final class DisplayBuilder {
private static final String INDENT = " ";
private BuilderConfig config;

/**
* Creates a renderer that uses {@link BuilderConfig} defaults.
*/
public PrettyObject() {
public DisplayBuilder() {
this(null);
}

Expand All @@ -32,7 +32,7 @@ public PrettyObject() {
*
* @param options labels, limits, and format settings, or {@code null}
*/
public PrettyObject(BuilderConfig options) {
public DisplayBuilder(BuilderConfig options) {
this.config = options == null ? new BuilderConfig() : options;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rickwporter/prettytable/OutputFormat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rickwporter.prettytable;

/**
* Output produced by {@link PrettyTable} and {@link PrettyObject}.
* Output produced by {@link PrettyTable} and {@link DisplayBuilder}.
*/
public enum OutputFormat {
/** ASCII rules and vertical bars. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

public class PrettyObjectTest {
public class DisplayBuilderTest {
private String loadFileContent(String filename) {
URL resource = getClass().getClassLoader().getResource(filename);
if (resource == null) {
Expand Down Expand Up @@ -64,10 +64,10 @@ public void testSimpleDict() {
extensions.put(OutputFormat.JSON, "json");
extensions.put(OutputFormat.YAML, "yaml");

PrettyObject pretty = new PrettyObject();
DisplayBuilder pretty = new DisplayBuilder();
Map<Object, Object> data = this.simpleDict();
for (Map.Entry<OutputFormat, String> entry : extensions.entrySet()) {
String filename = "PrettyObject_simple_dict." + entry.getValue();
String filename = "DisplayBuilder_simple_dict." + entry.getValue();
Assertions.assertEquals(
this.loadFileContent(filename),
pretty.render(entry.getKey(), data),
Expand All @@ -77,7 +77,7 @@ public void testSimpleDict() {

@Test
public void testNullValue() {
PrettyObject uut = new PrettyObject();
DisplayBuilder uut = new DisplayBuilder();

for (OutputFormat format : OutputFormat.values()) {
Assertions.assertEquals("", uut.render(format, null));
Expand All @@ -86,7 +86,7 @@ public void testNullValue() {

@Test
public void testSimpleValues() {
PrettyObject uut = new PrettyObject();
DisplayBuilder uut = new DisplayBuilder();

for (OutputFormat format : OutputFormat.values()) {
Assertions.assertEquals("1", uut.render(format, 1));
Expand All @@ -100,8 +100,8 @@ public void testConfig() {
Map<Object, Object> data = this.simpleDict();

String label = "My label";
PrettyObject config = new PrettyObject(new BuilderConfig().setPropertyLabel(label));
PrettyObject unconfig = new PrettyObject();
DisplayBuilder config = new DisplayBuilder(new BuilderConfig().setPropertyLabel(label));
DisplayBuilder unconfig = new DisplayBuilder();

String labeled = config.render(OutputFormat.RICH, data);
String unlabeled = unconfig.render(OutputFormat.RICH, data);
Expand All @@ -117,7 +117,7 @@ public void testJsonProcessingException() throws JsonProcessingException {
ObjectMapper mapper = mock(ObjectMapper.class);
when(mapper.writeValueAsString(any())).thenThrow(failure);

PrettyObject uut = new PrettyObject();
DisplayBuilder uut = new DisplayBuilder();
IllegalStateException ex = Assertions.assertThrows(
IllegalStateException.class,
() -> uut.renderJson(this.simpleDict(), mapper)
Expand All @@ -132,7 +132,7 @@ public void testYamlProcessingException() throws JsonProcessingException {
YAMLMapper mapper = mock(YAMLMapper.class);
when(mapper.writeValueAsString(any())).thenThrow(failure);

PrettyObject uut = new PrettyObject();
DisplayBuilder uut = new DisplayBuilder();
IllegalStateException ex = Assertions.assertThrows(
IllegalStateException.class,
() -> uut.renderYaml(this.simpleDict(), mapper)
Expand Down
Loading