// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/StringUtils.java
// public static @NotNull String rightPad(@NotNull String s, int length, char padding) {
//     if (s.length() >= length) return s;
// 
//     StringBuilder sb = new StringBuilder(length);
//     sb.append(s);
// 
//     for (int i = length - s.length(); i > 0; i--)
//         sb.append(padding);
// 
//     return sb.toString();
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/StringUtils.java
// public static @NotNull String truncate(@NotNull String s, int length) {
//     return truncate(s, length, "...");
// }


import static java.util.Collections.nCopies;
import static java.util.stream.Collectors.toList;
import static org.dalesbred.internal.utils.StringUtils.rightPad;
import static org.dalesbred.internal.utils.StringUtils.truncate;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import static java.lang.Math.max;
import static java.lang.Math.min;
    }

    static void write(@NotNull ResultTable table, @NotNull Appendable out) throws IOException {
        List<List<String>> formattedValues = format(table);
        int[] columnLengths = columnLengths(table.getColumnNames(), formattedValues);

        writeRow(out, table.getColumnNames(), columnLengths, ' ');
        writeRow(out, nCopies(table.getColumnCount(), ""), columnLengths, '-');

        for (List<String> row : formattedValues)
            writeRow(out, row, columnLengths, ' ');
    }

    private static void writeRow(@NotNull Appendable out, @NotNull List<String> values, int[] columnLengths, char padding) throws IOException {
        assert values.size() == columnLengths.length;

        out.append('|');
        for (int i = 0; i < columnLengths.length; i++)
            out.append(' ').append(formatCell(values.get(i), columnLengths[i], padding)).append(" |");

        out.append('\n');
    }

    private static @NotNull List<List<String>> format(@NotNull ResultTable table) {
        return table.getRows().stream()
                .map(it -> it.asList().stream().map(String::valueOf).collect(toList()))
                .collect(toList());
    }

    private static @NotNull String formatCell(@NotNull String v, int length, char padding) {
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/StringUtils.java
// public static @NotNull String rightPad(@NotNull String s, int length, char padding) {
//     if (s.length() >= length) return s;
// 
//     StringBuilder sb = new StringBuilder(length);
//     sb.append(s);
// 
//     for (int i = length - s.length(); i > 0; i--)
//         sb.append(padding);
// 
//     return sb.toString();
// }
// 
// Path: dalesbred/src/main/java/org/dalesbred/internal/utils/StringUtils.java
// public static @NotNull String truncate(@NotNull String s, int length) {
//     return truncate(s, length, "...");
// }


// Path: dalesbred/src/main/java/org/dalesbred/result/TableFormatter.java
import static java.util.Collections.nCopies;
import static java.util.stream.Collectors.toList;
import static org.dalesbred.internal.utils.StringUtils.rightPad;
import static org.dalesbred.internal.utils.StringUtils.truncate;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import static java.lang.Math.max;
import static java.lang.Math.min;

    }

    static void write(@NotNull ResultTable table, @NotNull Appendable out) throws IOException {
        List<List<String>> formattedValues = format(table);
        int[] columnLengths = columnLengths(table.getColumnNames(), formattedValues);

        writeRow(out, table.getColumnNames(), columnLengths, ' ');
        writeRow(out, nCopies(table.getColumnCount(), ""), columnLengths, '-');

        for (List<String> row : formattedValues)
            writeRow(out, row, columnLengths, ' ');
    }

    private static void writeRow(@NotNull Appendable out, @NotNull List<String> values, int[] columnLengths, char padding) throws IOException {
        assert values.size() == columnLengths.length;

        out.append('|');
        for (int i = 0; i < columnLengths.length; i++)
            out.append(' ').append(formatCell(values.get(i), columnLengths[i], padding)).append(" |");

        out.append('\n');
    }

    private static @NotNull List<List<String>> format(@NotNull ResultTable table) {
        return table.getRows().stream()
                .map(it -> it.asList().stream().map(String::valueOf).collect(toList()))
                .collect(toList());
    }

    private static @NotNull String formatCell(@NotNull String v, int length, char padding) {
        return rightPad(truncate(v, length), length, padding);