eldargab / easy-table

Nice text table for Node.js
314 stars 31 forks source link

Added .sort() and ability to add a divider with .newLine() #1

Closed Jimbly closed 12 years ago

Jimbly commented 12 years ago

Added .sort() and ability to add a divider with .newLine() when it is called with no cell data.

eldargab commented 12 years ago

Thank you for the sort suggestion. I'll merge it. But I disagree with the divider feature. Probably you want to accomplish groupBy like things. If so we can consider to implement it. I thought about group by at some point but decided to postpone just because didn't have such use case yet.

Jimbly commented 12 years ago

The specific thing I used the divider for is putting it before adding a totals row at the bottom. In theory could add a function to add a total to the table automatically, but since some fields are strings that didn't seem to make as much sense as me totaling the fields myself and just adding the final divider and row (after sorting).

eldargab commented 12 years ago

Is it easier just to draw plain line at the end? :)

eldargab commented 12 years ago

Something like:

var s = t.toString()
var l = s.split(/\n/)[0].length
s += Table.RightPadder('-')('', l)
Jimbly commented 12 years ago

I needed the line between the last row of data and the row of totals (which needs to be a Table row so each column's total gets spaced right, uses the right formatter, etc). Specifically, I'm putting together a node module for dumping and displaying memory usage in a native process. Output ends up being something like this:

Addr               Size       Count  Traffic  Module                            Symbol                            Offs
-----------------  ---------  -----  -------  --------------------------------  --------------------------------  ------
0x139671333806034   15 bytes      1    44795  /lib/libc.so.6                    __strdup                          +0x22
0x8424450          112 bytes      1        1  node                                                                und...
0x8169177            3.42 KB      4  1541348  node                              _ZN2v88internal8Malloced3NewEm    +0x9
0x139671344824909    1.18 MB    321   261273  /usr/lib/libstdc++.so.6           _Znwm                             +0x1d
0x139671296020065    2.91 MB    424   202854  ...les/native/bullet/bullet.node                                    und...
-----------------  ---------  -----  -------  --------------------------------  --------------------------------  ------
TOTAL                4.10 MB    751  2053229                                                                      und...
eldargab commented 12 years ago

Looks nice... Let's just add total function with api like:

table.totals(['Size', 'Count', 'Traffic'])
Jimbly commented 12 years ago

I'm a little wary of putting data-processing logic inside a module that's primarily display formatting, but I already crossed that line with .sort (could almost as easily call .sort on the input data before sending it into the Table), so, here's .totals() now as well. Though in my use case, I think there's a reasonable chance I'm going to want to use the totals for additional logic, so I'll end up either calculating them again outside the Table, or need to put in an accessor to get the values out.