From bf93f7381f29749c7cd405d78876f1c1fa6851f4 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:14:34 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- js/doc/table.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 js/doc/table.js diff --git a/js/doc/table.js b/js/doc/table.js new file mode 100644 index 0000000..3d4ac36 --- /dev/null +++ b/js/doc/table.js @@ -0,0 +1,38 @@ +var doc = Require('doc/doc'); +var com = Require('com/compat'); + +function Table (data,options) { + var totalWidth=(process.stdout.columns)-2; + if (com.obj.isArray(options)) options={head:options}; + options=options||{}; + var head=options.head,table; + if (com.obj.isMatrix(data)) { + } else if (com.obj.isArray(data) && com.obj.isObject(data[0])) { + options.head=true; + head=Object.keys(data[0]); + data=data.map(function (row) { + return head.map(function (key) { return row[key] }) + }); + } else return new Error('Table: Inavlid data'); + if (!options.colWidths) { + totalWidth-= ((head||data[0]).length-1); + options.colWidths=(head||data[0]).map(function (x,i) { + return Math.max(4,Math.floor(totalWidth/(head||data[0]).length)); + }); + } + if (head) + table = new doc.Table({ + head : head, + colWidths :options.colWidths, + }); + else + table = new doc.Table({ + colWidths : options.colWidths, + }); + data.forEach(function (row,rowi) { + table.push(row); + }); + print(table.toString()); +} + +module.exports = Table;