use of com.github.lindenb.jvarkit.table.TableFactory 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);
}
}
Aggregations