Search in sources :

Example 1 with Table

use of com.github.lindenb.jvarkit.table.Table in project jvarkit by lindenb.

the class PrettyTable method doWork.

@Override
public int doWork(final List<String> args) {
    final List<List<String>> rows = new ArrayList<>();
    PrintWriter out = null;
    BufferedReader br = null;
    try {
        final CharSplitter delim = CharSplitter.of(this.delimiter);
        int n_columns = -1;
        br = super.openBufferedReader(oneFileOrNull(args));
        String line;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isBlank(line))
                continue;
            final List<String> row = delim.splitAsStringList(line);
            if (rows.isEmpty()) {
                n_columns = row.size();
                LOG.info("n=" + n_columns);
                if (this.noHeader) {
                    final List<String> header = new ArrayList<>(n_columns);
                    while (header.size() < n_columns) {
                        header.add("$" + (1 + header.size()));
                    }
                    rows.add(header);
                }
            } else if (row.size() < n_columns) {
                while (row.size() < n_columns) row.add("");
            } else if (row.size() > n_columns) {
                n_columns = row.size();
                for (int y = 0; y < rows.size(); ++y) {
                    while (rows.get(y).size() < n_columns) rows.get(y).add("");
                }
            } else {
            // ok
            }
            System.err.println(">>>" + row.get(0));
            rows.add(row);
        }
        br.close();
        br = null;
        if (rows.isEmpty()) {
            LOG.error("empty input");
            return -1;
        }
        if (this.transpose) {
            final List<List<String>> rows2 = new ArrayList<>(n_columns);
            for (int x = 0; x < n_columns; ++x) {
                final List<String> row = new ArrayList<>(rows.size());
                for (int y = 0; y < rows.size(); ++y) {
                    row.add(rows.get(y).get(x));
                }
                rows2.add(row);
            }
            rows.clear();
            rows.addAll(rows2);
        }
        final TableFactory tableFactory = new TableFactory();
        final Table table = tableFactory.createTable(rows.get(0));
        for (int y = 1; y < rows.size(); ++y) {
            table.addRow(rows.get(y));
        }
        out = super.openFileOrStdoutAsPrintWriter(this.outputFile);
        if (this.format.equalsIgnoreCase("html")) {
            final HtmlExporter exporter = new HtmlExporter();
            exporter.setRemoveEmptyColumns(!this.keepEmptyComlumns);
            exporter.saveTableTo(table, out);
        } else {
            final TextExporter exporter = new TextExporter();
            exporter.setAllowUnicode(this.withUnicode);
            exporter.setRemoveEmptyColumns(!this.keepEmptyComlumns);
            exporter.saveTableTo(table, out);
        }
        out.flush();
        out.close();
        out = null;
        return 0;
    } catch (final Throwable err) {
        LOG.error(err);
        return -1;
    } finally {
        CloserUtil.close(out);
    }
}
Also used : Table(com.github.lindenb.jvarkit.table.Table) CharSplitter(com.github.lindenb.jvarkit.lang.CharSplitter) HtmlExporter(com.github.lindenb.jvarkit.table.HtmlExporter) ArrayList(java.util.ArrayList) TextExporter(com.github.lindenb.jvarkit.table.TextExporter) TableFactory(com.github.lindenb.jvarkit.table.TableFactory) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) PrintWriter(java.io.PrintWriter)

Aggregations

CharSplitter (com.github.lindenb.jvarkit.lang.CharSplitter)1 HtmlExporter (com.github.lindenb.jvarkit.table.HtmlExporter)1 Table (com.github.lindenb.jvarkit.table.Table)1 TableFactory (com.github.lindenb.jvarkit.table.TableFactory)1 TextExporter (com.github.lindenb.jvarkit.table.TextExporter)1 BufferedReader (java.io.BufferedReader)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1