Mon 21 Jul 22:43:21 CEST 2025
This commit is contained in:
parent
2d03cd1a48
commit
bf93f7381f
38
js/doc/table.js
Normal file
38
js/doc/table.js
Normal file
|
@ -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;
|
Loading…
Reference in New Issue
Block a user