tj / terminal-table

Ruby ASCII Table Generator, simple and feature rich.
MIT License
1.53k stars 121 forks source link

Use nice UTF box-drawing characters for borders by default #99

Closed TylerRick closed 3 years ago

TylerRick commented 6 years ago

Instead of boring ASCII characters:

+------+----------------------------------------------------+---------+---------+--------+
|   id | name                                               |         |         |        |
+------+----------------------------------------------------+---------+---------+--------+

is there a reason we don't use the nice UTF box-drawing characters by default?

That is, something more like this:

╒══════╤════════════════════════════════════════════════════╤═════════╤═════════╤════════╕
│   id │ name                                               │         │         │        │
╘══════╧════════════════════════════════════════════════════╧═════════╧═════════╧════════╛

Or if we can't make them the default for some reason, could we at least make it possible to use them for all corners and intersections?

Different intersections require different glyphs, so this doesn't look super great:

table.style = {border_x: '═', border_i: '╪', border_y: '│'}    
╪══════╪════════════════════════════════════════════════════╪═════════╪═════════╪════════╪
│   id │ name                                               │         │         │        │
╪══════╪════════════════════════════════════════════════════╪═════════╪═════════╪════════╪

The best-looking approximation I've come up with is:

table.style = {border_x: '═', border_i: '═', border_y: '│'}    
══════════════════════════════════════════════════════════════════════════════════════════
│   id │ name                                               │         │         │        │
══════════════════════════════════════════════════════════════════════════════════════════
Fustrate commented 6 years ago

I was looking through the source code trying to find the best way to do this, and I decided instead to make a post-formatter that would convert the default +- borders to unicode:

def prettify_table(table)
  top_border, *middle, bottom_border = table.render.lines.map(&:strip)

  new_table = middle.map do |line|
    line[0] == '+' ? "├#{line[1...-1].tr('-+', '─┼')}┤" : line.tr('|', '│')
  end

  new_table.unshift "┌#{top_border[1...-1].tr('-+', '─┬')}┐"
  new_table.push "└#{bottom_border[1...-1].tr('-+', '─┴')}┘"

  # Move the T-shaped corners down two rows if there's a title
  if table.title
    new_table[0] = new_table[0].tr('┬', '─')
    new_table[2] = new_table[2].tr('┼', '┬')
  end

  new_table.join("\n")
end

It works for my admittedly basic needs, which are just titles and separators, but I figured I'd drop it here in case anyone else comes looking.

TylerRick commented 6 years ago

Nice! That could work too.

Just noticed another tool that can output tables: https://github.com/cldwalker/hirb/tree/master#views-anytime-anywhere. It uses similar + and - characters by default, same as terminal-table, but it does have an option to use box-drawing Unicode characters:

>> table [[:a, :b, :c]], :unicode => true
┌───┬───┬───┐
│ 0 │ 1 │ 2 │
├───┼───┼───┤
│ a ╎ b ╎ c │
└───┴───┴───┘
1 row in set
nanobowers commented 3 years ago

@TylerRick @nateberkopec I think this can be closed - fixed as part of #113