Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
TylerRick opened this issue May 9, 2018 · 3 comments
Closed

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

TylerRick opened this issue May 9, 2018 · 3 comments

Comments

@TylerRick
Copy link

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
Copy link

Fustrate commented Jun 7, 2018

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
Copy link
Author

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants