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
243 changes: 243 additions & 0 deletions src/main/java/rickwporter/prettytable/BuilderConfig.java

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/main/java/rickwporter/prettytable/HorizontalAlign.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package rickwporter.prettytable;

/**
* Horizontal placement of cell text in text, rich, and HTML output.
*/
public enum HorizontalAlign {
/** Centers the cell text. The HTML style is {@code text-align:center}. */
CENTER("text-align:center"),

/** Aligns the cell text to the left. The HTML style is {@code text-align:left}. */
LEFT("text-align:left"),

/** Aligns the cell text to the right. The HTML style is {@code text-align:right}. */
RIGHT("text-align:right");

private final String htmlStyle;
Expand All @@ -11,6 +19,11 @@ public enum HorizontalAlign {
this.htmlStyle = html;
}

/**
* Returns the CSS declaration written onto HTML header and body cells.
*
* @return a style such as {@code text-align:left}
*/
public String getHtmlStyle() {
return this.htmlStyle;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/rickwporter/prettytable/OutputFormat.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package rickwporter.prettytable;

/**
* Output produced by {@link PrettyTable} and {@link PrettyObject}.
*/
public enum OutputFormat {
/** ASCII rules and vertical bars. */
TEXT,

/** Unicode box-drawing rules and vertical bars. */
RICH,

/** Comma-separated values. */
CSV,

/** An HTML table. */
HTML,

/** A JSON array of the header row followed by one object per data row. */
JSON,

/** A YAML list of the header row followed by one mapping per data row. */
YAML,
}
25 changes: 25 additions & 0 deletions src/main/java/rickwporter/prettytable/PrettyObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,30 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

/**
* Renders a value as a string.
* {@code null} becomes an empty string. A string, number, or boolean is rendered with
* {@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 {
private static final String INDENT = " ";
private BuilderConfig config;

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

/**
* Creates a renderer that uses {@code options} for table output.
* JSON and YAML ignore the config and write the value as given. A {@code null} config uses
* {@link BuilderConfig} defaults.
*
* @param options labels, limits, and format settings, or {@code null}
*/
public PrettyObject(BuilderConfig options) {
this.config = options == null ? new BuilderConfig() : options;
}
Expand Down Expand Up @@ -55,6 +71,15 @@ String renderYaml(Object object, YAMLMapper mapper) {
}
}

/**
* Renders {@code object} in {@code format}.
*
* @param format output format
* @param object value to render, or {@code null}
* @return the rendered text, or an empty string when {@code object} is {@code null}
* @throws IllegalArgumentException if a table format is given a value that is not a map or list
* @throws IllegalStateException if JSON or YAML writing fails
*/
public String render(OutputFormat format, Object object) {
if (object == null) {
return "";
Expand Down
Loading
Loading