eldargab / easy-table

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

When printing data transposed easy-table determines the width of the value cell based on the longest value #19

Closed waldekmastykarz closed 6 years ago

waldekmastykarz commented 6 years ago

When printing table transposed, easy-table determines the width of the value cell, based on the longest value. If one of the values is very long, this results in awkward output with a lot of white space surrounding shorter values. Preferably, easy-table should determine only the max-width of the key-cell and print values with variable width.

Repro steps:

var Table = require("easy-table")

var data = [
  { id: 123123, desc: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus semper dignissim elit non pharetra. Pellentesque efficitur magna in ante maximus, quis tempor sem auctor. Praesent tincidunt rhoncus nulla, et vehicula ante consectetur in. Proin iaculis sit amet velit sit amet fermentum. Curabitur sollicitudin elementum lorem vel gravida. Integer fermentum vitae massa sodales gravida. Pellentesque dictum elit eu commodo varius. In lectus dui, rutrum tempus nunc vel, efficitur pulvinar libero. Vestibulum viverra, lorem vitae fringilla luctus, diam dui tempor est, eu fermentum tortor massa ac enim.', price: 1000.00 }
]

var t = new Table

data.forEach(function(product) {
  t.cell('Product Id', product.id)
  t.cell('Description', product.desc)
  t.cell('Price, USD', product.price)
  t.newRow()
})

console.log(t.printTransposed({
  separator: ': '
}))

Output (the exact number of white space depends on the terminal width. Here new lines added to illustrate the problem):

Product Id : 123123

Description: Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Phasellus semper dignissim elit non
pharetra. Pellentesque efficitur magna in ante maximus, quis
tempor sem auctor. Praesent tincidunt rhoncus nulla, et
vehicula ante consectetur in. Proin iaculis sit amet velit
sit amet fermentum. Curabitur sollicitudin elementum lorem
vel gravida. Integer fermentum vitae massa sodales gravida.
Pellentesque dictum elit eu commodo varius. In lectus dui,
rutrum tempus nunc vel, efficitur pulvinar libero.
Vestibulum viverra, lorem vitae fringilla luctus, diam dui
tempor est, eu fermentum tortor massa ac enim.
Price, USD : 1000

Preferable output:

Product Id : 123123
Description: Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Phasellus semper dignissim elit non
pharetra. Pellentesque efficitur magna in ante maximus, quis
tempor sem auctor. Praesent tincidunt rhoncus nulla, et
vehicula ante consectetur in. Proin iaculis sit amet velit
sit amet fermentum. Curabitur sollicitudin elementum lorem
vel gravida. Integer fermentum vitae massa sodales gravida.
Pellentesque dictum elit eu commodo varius. In lectus dui,
rutrum tempus nunc vel, efficitur pulvinar libero.
Vestibulum viverra, lorem vitae fringilla luctus, diam dui
tempor est, eu fermentum tortor massa ac enim.
Price, USD : 1000
eldargab commented 6 years ago

The problem is with multiline content. It's not currently supported (#18).

waldekmastykarz commented 6 years ago

@eldargab the issue you referenced is caused by content with a line break. In my case, the content is single line, just too long. If you think it's the same, I'd suggest you keep this issue in mind as an extra test case.

eldargab commented 6 years ago

Ah, I see...

I think this problem is out of scope. In general .printTransposed() is about turning rows to columns and vice versa. If your task is just to print key-value correspondence it's much simpler to devise a special function for that, since such function doesn't need any logic specific to easy-table.

waldekmastykarz commented 6 years ago

Gotcha, in that case I misunderstood the purpose of printTransposed(). I'm using a custom function at the moment, but it's good to have it confirmed that easy-table was not meant to be used for key-value pairs.