Skip to content

Automatically split lines in TextTable #696

@remkop

Description

@remkop

Currently, client code needs to manually split lines with embedded newlines before calling TextTable.addRowValues or TextTable.putValue.

It would be more convenient if TextTable does this automatically. For example:

//import static picocli.CommandLine.Help.Column.Overflow.*;
//import static picocli.CommandLine.Help.TextTable.forColumns;

public static void main(String[] args) {
    String key = "<query>";
    String value = String.join("\n",
            "Shows results of SQL <query>",
            "The query itself can contain the variables ${table}, ${columns} "
                + "and ${tabletype}, or system properties referenced as ${<system-property-name>}",
            "Queries without any variables are executed exactly once",
            "Queries with variables are executed once for each table, "
                + "with the variables substituted");

    System.out.println("AUTO-SPLIT (does not work yet)");
    System.out.println("------------------------------");
    autoSplit(key, value);

    System.out.println("MANUAL SPLIT");
    System.out.println("------------");
    manualSplit(key, value);
}

private CommandLine.Help.TextTable emptyTable() {
    return forColumns(CommandLine.Help.Ansi.OFF,
        new CommandLine.Help.Column(15, 1, SPAN),
        new CommandLine.Help.Column(65, 1, WRAP));
}

static void autoSplit(String key, String value) {
    CommandLine.Help.TextTable textTable = emptyTable();

    // What we want to do:
    textTable.addRowValues(key, value);
    System.out.println(textTable);
}

static void manualSplit(String key, String value) {
    CommandLine.Help.TextTable textTable = emptyTable();

    // what we actually need to do to deal with the embedded newlines
    CommandLine.Help.Ansi.Text name = CommandLine.Help.Ansi.AUTO.text(key);
    CommandLine.Help.Ansi.Text description = CommandLine.Help.Ansi.AUTO.text(value);

    // split the description
    CommandLine.Help.Ansi.Text[] lines = description.splitLines();

    // first line
    textTable.addRowValues(name, lines[0]);

    // remaining lines
    CommandLine.Help.Ansi.Text EMPTY = CommandLine.Help.Ansi.OFF.text("");
    for (int i = 1; i < lines.length; i++) {
        textTable.addRowValues(EMPTY, lines[i]);
    }

    System.out.println(textTable);
}

If we don't manually split the lines, the text table will not take the embedded newlines into account, giving unexpected results:

AUTO-SPLIT (does not work yet)
------------------------------
 <query>        Shows results of SQL <query>
The query itself can contain the
                  variables ${table}, ${columns} and ${tabletype}, or system
                  properties referenced as ${<system-property-name>}
Queries
                  without any variables are executed exactly once
Queries with
                  variables are executed once for each table, with the
                  variables substituted

MANUAL SPLIT
------------
 <query>        Shows results of SQL <query>
                The query itself can contain the variables ${table}, ${columns}
                  and ${tabletype}, or system properties referenced as
                  ${<system-property-name>}
                Queries without any variables are executed exactly once
                Queries with variables are executed once for each table, with
                  the variables substituted

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions