diff --git a/src/util/es/Console.js b/src/util/es/Console.js index 8fcf13dce..41f54c40d 100644 --- a/src/util/es/Console.js +++ b/src/util/es/Console.js @@ -31,7 +31,25 @@ class Console extends sys.Obj { err(msg) { console.error(msg); return this; } - table(obj) { console.table(obj); return this; } + table(obj) + { + var grid = [] + var t = fan.util.ConsoleTable.make(obj); + for (var r=0; r 1) rows = list[1..-1] + return + } + + // list of something + if (list != null) + { + // turn each item in list to a Str:Str map + maps := Str:Str[,] + list.each |item| { maps.add(map(item)) } + + // create list of columns union + cols := Str:Str[:] { ordered = true } + maps.each |map| + { + map.each |v, k| { cols[k] = k } + } + headers = cols.vals + + // now turn each row Str:Str into a Str[] of cells + maps.each |map| + { + row := Str[,] + row.capacity = headers.size + cols.each |k| { row.add(map[k] ?: "") } + rows.add(row) + } + return + } + + // scalar value + headers = ["val"] + rows = [[str(x)]] + } + + Str[] headers := [,] + Str[][] rows := [,] + + once Int[] widths() + { + widths := Int[,] + widths.capacity = headers.size + headers.each |h, c| + { + w := h.size + rows.each |row| + { + w = w.max(row[c].size) + } + widths.add(w) + } + return widths + } + + Void dump(Console c) + { + c.info(row(headers)) + c.info(underlines) + rows.each |x| { c.info(row(x)) } + } + + private Str row(Str[] cells) + { + s := StrBuf() + cells.each |cell, i| + { + if (i > 0) s.add(" ") + s.add(cell) + s.add(Str.spaces(widths[i] - cell.size)) + } + return s.toStr + } + + private Str underlines() + { + s := StrBuf() + headers.each |h, i| + { + if (i > 0) s.add(" ") + widths[i].times { s.addChar('-') } + } + return s.toStr + } + + static Str:Str map(Obj? x) + { + if (x == null) return ["val":"null"] + + m := x.typeof.method("each", false) + if (m == null || x is Str) return ["val":str(x)] + + acc := Str:Str[:] { ordered = true } + f := |v, k| { acc[str(k)] = str(v)} + m.callOn(x, [f]) + return acc + } + + private static Str str(Obj? x) + { + if (x == null) return "null" + s := x.toStr + if (s.contains("\n")) s = s.splitLines.first + if (s.size > 80) s = s[0..80] + ".." + return s + } +} + diff --git a/src/util/java/Console.java b/src/util/java/Console.java index 03a8bdbd4..54b5cd6b4 100644 --- a/src/util/java/Console.java +++ b/src/util/java/Console.java @@ -89,15 +89,15 @@ private Console log(String level, Object msg) public Console table(Object obj) { - info("TODO: " + obj); + ConsoleTable.make(obj).dump(this); return this; } public Console group(Object obj) { return group(obj, false); } public Console group(Object obj, boolean collapse) { - indent++; info(obj); + indent++; return this; } private int indent; diff --git a/src/util/js/Console.js b/src/util/js/Console.js index 996105eea..2206781a3 100644 --- a/src/util/js/Console.js +++ b/src/util/js/Console.js @@ -30,7 +30,25 @@ fan.util.Console.prototype.width = function() { return null; } fan.util.Console.prototype.height = function() { return null; } -fan.util.Console.prototype.table = function(obj) { console.table(obj); return this; } +fan.util.Console.prototype.table = function(obj) +{ + var grid = [] + var t = fan.util.ConsoleTable.make(obj); + for (var r=0; r