eldargab / easy-table

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

Center Alignment #10

Closed jrodl3r closed 9 years ago

jrodl3r commented 9 years ago

How difficult do you think it would be to add center alignment on print + transposed output? :beers:

eldargab commented 9 years ago

Do you mean something like this?

aaaaaa  bbbbbbb
  aa      bbb
 aaaa    bbbbb
jrodl3r commented 9 years ago

yes

eldargab commented 9 years ago

Note, that we can only position with precision up to one character. There is no way to center align precisely when you have both even and odd lengths in the same column. If this is not a concern, the following will do the job:

function center(str, width) {
  if (!width) return str
  var pad = (width - str.length) >>> 1
  return new Array(pad + 1).join(' ') + str
}

// use it like
t.cell('foo', 'bar baz', center)