All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vproxy.base.util.display.TableBuilder Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package io.vproxy.base.util.display;

import java.util.ArrayList;
import java.util.List;

public class TableBuilder {
    private final List lines = new ArrayList<>();

    public TR tr() {
        TR tr = new TR();
        lines.add(tr);
        return tr;
    }

    @Override
    public String toString() {
        List maxLen = new ArrayList<>();
        for (var tr : lines) {
            for (int i = 0; i < tr.columns.size(); i++) {
                String td = tr.columns.get(i);
                int l = td.length();
                if (maxLen.size() <= i) {
                    maxLen.add(l);
                } else {
                    int x = maxLen.get(i);
                    if (x < l) {
                        maxLen.set(i, l);
                    }
                }
            }
        }
        for (int i = 0; i < maxLen.size(); ++i) {
            int x = maxLen.get(i);
            if (x % 8 == 0) {
                x += 4;
            } else {
                x += 4 - x % 4;
            }
            maxLen.set(i, x);
        }
        StringBuilder sb = new StringBuilder();
        for (var tr : lines) {
            for (int i = 0; i < tr.columns.size(); ++i) {
                String td = tr.columns.get(i);
                sb.append(td);
                int spaces = maxLen.get(i) - td.length();
                sb.append(" ".repeat(spaces));
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy