Skip to content

Commit

Permalink
#179 Final touches to remove full JRE dependency and require only Com…
Browse files Browse the repository at this point in the history
…pact Profile.

- Rename inner class `picocli.CommandLine.Point` to `picocli.CommandLine.Help.TextTable.Cell`
- Add javadoc, update existing javadoc
- Add to release notes with credit to [webfolderio](https://github.com/webfolderio).
  • Loading branch information
remkop committed Oct 1, 2017
1 parent 39f1944 commit 30fad63
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Args {
- #195 Usage help should show Map types if paramLabel not specified
- #185 Missing option exception text should not use field names but be more descriptive and consistent with usage help. Thanks to [AlexFalappa](https://github.com/AlexFalappa).
- #186 Confusing usage message for collection options
- #179 Remove full JRE dependency: require only Compact Profile. Replace use of `java.awt.Point` with `picocli.CommandLine.Help.TextTable.Cell`. Thanks to [webfolderio](https://github.com/webfolderio).
- #181 Fixed bug where incorrect help message is displayed for short options with paramLabel when arity > 1
- #184 Improved CommandLine.setSeparator javadoc to clarify that this affects parsing only and link to the `@Command` `separator` annotation attribute.

Expand Down
58 changes: 33 additions & 25 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,21 @@ public int compare(Field f1, Field f2) {
* longer than the column's width.</p>
*/
public static class TextTable {
/**
* Helper class to index positions in a {@code Help.TextTable}.
* @since 2.0
*/
public static class Cell {
/** Table column index (zero based). */
public final int column;
/** Table row index (zero based). */
public final int row;
/** Constructs a new Cell with the specified coordinates in the table.
* @param column the zero-based table column
* @param row the zero-based table row */
public Cell(int column, int row) { this.column = column; this.row = row; }
}

/** The column definitions of this table. */
public final Column[] columns;

Expand Down Expand Up @@ -3379,8 +3394,11 @@ public TextTable(Ansi ansi, Column... columns) {
/** Returns the {@code Text} slot at the specified row and column to write a text value into.
* @param row the row of the cell whose Text to return
* @param col the column of the cell whose Text to return
* @return the Text object at the specified row and column */
public Text cellAt(int row, int col) { return columnValues.get(col + (row * columns.length)); }
* @return the Text object at the specified row and column
* @since 2.0 */
public Text textAt(int row, int col) { return columnValues.get(col + (row * columns.length)); }
/** @deprecated use {@link #textAt(int, int)} instead */
public Text cellAt(int row, int col) { return textAt(row, col); }

/** Returns the current number of rows of this {@code TextTable}.
* @return the current number of rows in this TextTable */
Expand Down Expand Up @@ -3417,10 +3435,10 @@ public void addRowValues(Text... values) {
addEmptyRow();
for (int col = 0; col < values.length; col++) {
int row = rowCount() - 1;// write to last row: previous value may have wrapped to next row
Point cell = putValue(row, col, values[col]);
Cell cell = putValue(row, col, values[col]);

// add row if a value spanned/wrapped and there are still remaining values
if ((cell.y != row || cell.x != col) && col != values.length - 1) {
if ((cell.row != row || cell.column != col) && col != values.length - 1) {
addEmptyRow();
}
}
Expand All @@ -3432,29 +3450,29 @@ public void addRowValues(Text... values) {
* @param row the target row in the table
* @param col the target column in the table to write to
* @param value the value to write
* @return a Point whose {@code x} value is the last column written to and whose {@code y} value is the
* last row written to
* @return a Cell indicating the position in the table that was last written to (since 2.0)
* @throws IllegalArgumentException if the specified row exceeds the table's {@linkplain
* TextTable#rowCount() row count}
* @since 2.0 (previous versions returned a {@code java.awt.Point} object)
*/
public Point putValue(int row, int col, Text value) {
public Cell putValue(int row, int col, Text value) {
if (row > rowCount() - 1) {
throw new IllegalArgumentException("Cannot write to row " + row + ": rowCount=" + rowCount());
}
if (value == null || value.plain.length() == 0) { return new Point(col, row); }
if (value == null || value.plain.length() == 0) { return new Cell(col, row); }
Column column = columns[col];
int indent = column.indent;
switch (column.overflow) {
case TRUNCATE:
copy(value, cellAt(row, col), indent);
return new Point(col, row);
copy(value, textAt(row, col), indent);
return new Cell(col, row);
case SPAN:
int startColumn = col;
do {
boolean lastColumn = col == columns.length - 1;
int charsWritten = lastColumn
? copy(BreakIterator.getLineInstance(), value, cellAt(row, col), indent)
: copy(value, cellAt(row, col), indent);
? copy(BreakIterator.getLineInstance(), value, textAt(row, col), indent)
: copy(value, textAt(row, col), indent);
value = value.substring(charsWritten);
indent = 0;
if (value.length > 0) { // value did not fit in column
Expand All @@ -3467,19 +3485,19 @@ public Point putValue(int row, int col, Text value) {
indent = column.indent + indentWrappedLines;
}
} while (value.length > 0);
return new Point(col, row);
return new Cell(col, row);
case WRAP:
BreakIterator lineBreakIterator = BreakIterator.getLineInstance();
do {
int charsWritten = copy(lineBreakIterator, value, cellAt(row, col), indent);
int charsWritten = copy(lineBreakIterator, value, textAt(row, col), indent);
value = value.substring(charsWritten);
indent = column.indent + indentWrappedLines;
if (value.length > 0) { // value did not fit in column
++row; // write remainder of value in next row
addEmptyRow();
}
} while (value.length > 0);
return new Point(col, row);
return new Cell(col, row);
}
throw new IllegalStateException(column.overflow.toString());
}
Expand Down Expand Up @@ -4155,14 +4173,4 @@ public MissingTypeConverterException(String msg) {
super(msg);
}
}

public static class Point {
public int x;
public int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
}
6 changes: 3 additions & 3 deletions src/test/java/picocli/CustomLayoutDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Zip {
}

class TwoOptionsPerRowLayout extends Layout { // define a custom layout
Point previous = new Point(0, 0);
TextTable.Cell previous = new TextTable.Cell(0, 0);

private TwoOptionsPerRowLayout(Help.ColorScheme colorScheme, TextTable textTable,
IOptionRenderer optionRenderer,
Expand All @@ -141,11 +141,11 @@ public void layout(Field field, Text[][] values) {

// We want to show two options on one row, next to each other,
// unless the first option spanned multiple columns (in which case there are not enough columns left)
int col = previous.x + 1;
int col = previous.column + 1;
if (col == 1 || col + columnValues.length > table.columns.length) { // if true, write into next row

// table also adds an empty row if a text value spanned multiple columns
if (table.rowCount() == 0 || table.rowCount() == previous.y + 1) { // avoid adding 2 empty rows
if (table.rowCount() == 0 || table.rowCount() == previous.row + 1) { // avoid adding 2 empty rows
table.addEmptyRow(); // create the slots to write the text values into
}
col = 0; // we are starting a new row, reset the column to write into
Expand Down

0 comments on commit 30fad63

Please sign in to comment.