TylerBrock / mongo-hacker

MongoDB Shell Enhancements for Hackers
tylerbrock.github.io/mongo-hacker
MIT License
1.79k stars 235 forks source link

Handle multiple (ie. more than 2) columns #150

Closed pvdb closed 8 years ago

pvdb commented 8 years ago

Hey @TylerBrock,

In https://github.com/TylerBrock/mongo-hacker/pull/148#discussion_r51252848 I wrote:

[it] requires a pretty significant refactor of the printPaddedColumns() function in hacks/helpers/js, mainly to support multiple columns (instead of just two, which it does currently)

... here it is, in all its glory, the refactoring of printPaddedColumns() to support multiple (ie. more than 2) colums.

The printPaddedColumns() function has changed so much that the GitHub diff is probably meaningless, so probably better to just grok the new version.

Thanks to the new implementation, though, I was able to get rid of the now-superfluous mergePaddedValues() function, as that function just made up for the lack of proper multi-column support in printPaddedColumns().

The main benefits of this new version are (1) how it simplifies printing out multiple padded columns, and (2) how it supports a variable number of columns (1 upto ∞, in theory at least :smile:):

before

printPaddedColumns(collectionNames, mergePaddedValues(collectionSizes, collectionStorageSizes));

after

printPaddedColumns(collectionNames, collectionSizes, collectionStorageSizes);

... which will enable me to merge this into #148 to implement the "dynamic" padding you wanted to fix first.

Going forward, this function can be used to print and layout even more columns, not just the three that we have at the moment:

macpvandenb(mongod-3.2.1) test> printPaddedColumns(["first", "second", "third"], ["foo", 2, 3], [4, "bar", 6], [7, 8, "qux"], [0, "blegga", 0])
first  → foo /   4 /   7 /      0
second →   2 / bar /   8 / blegga
third  →   3 /   6 / qux /      0
null
macpvandenb(mongod-3.2.1) test> _

BTW: this PR is again a pure refactoring of existing logic, it doesn't change anything functionally, as you can verify by running the show dbs, count collections, show collections, and count documents shell commands... they behave exactly as before, and their output is identical. So much win! :smile:

TylerBrock commented 8 years ago

This is great! Thank you for doing the work on this and sorry for the delay!

pvdb commented 8 years ago

Thank you for doing the work on this...

NP, it's a good way for me to level up on JS!