Table rendering is not working
a14m opened this issue · 11 comments
I have a simple script as part of a gem... the prompt, commands are working fine but rendering the tables doesn't work at all...
TTY::Command.new(printer: :quiet).run('clear')
TTY::Prompt.new.say("Updated at: #{Time.now}")
table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
table.render :ascii
this doesn't output only the table to the terminal... but puts table.to_s
outputs the following
"+-------+-------+\n|header1|header2|\n+-------+-------+\n|a1 |a2 |\n|b1 |b2 |\n+-------+-------+"
macOS Sierra (10.12.1)
ruby 2.3.2
Hi Raz, thanks for giving tty a try! The components that you are looking at can also be used separately, e.i. you can install tty-command
, tty-prompt
and tty-table
among many.
The idea behind table rendering is that the render
or to_s
converts the data into a string format. As is the case in your example, it is a perfectly formatted table. It is up to you what you want to do with it, one thing would be to print it to console like:
puts table.render(:ascii)
sorry but how do I print the table to the tty ? because
puts table.render(:ascii)
prints the following
"+-------+-------+\n|header1|header2|\n+-------+-------+\n|a1 |a2 |\n|b1 |b2 |\n+-------+-------+"
but I need it to print the table to the TTY correctly... not as 1 line
The render
or to_s
messages return a string, nothing more or nothing else. So calling
table.render(:ascii)
will return actual string which is what you have in your example. Can you see the \n
characters? These are line breaks. Any time console sees these it will render content after them on a new line, and hence a nice table. So my question to you would be, how do you render a string in Ruby to console?
which bash version you have ?
My bash version is 3.2.57(1)-release
. Could you please paste the code that you are actually using for creating and rendering the table. I'd like to see what you are exactly doing and maybe then we will solve this mystery.
yeah it's code used in the description of the issue (just added puts to table render)
and I found a solution to show it correctly using Command.new.run('echo', table.render(:unicode))
didn't work with :ascii cause the pipe is causing issues with the echo command
Aha that makes sense, because Command.new.run(cmd)
escapes characters, that would totally explain why it was directly rendering the string. Sorry if I sounded a bit patronising by I couldn't for the world understand why the simple code that you have pasted wouldn't work. It is not the first time tty-table
was used, I use it for many other scripts etc....
sorry I didn't understand your last comment but the following code
command = TTY::Command.new(printer: :quiet)
command.run('clear')
TTY::Prompt.new.say("Updated at: #{Time.now}")
table = TTY::Table.new ['header1','header2'], [['a1', 'a2'], ['b1', 'b2']]
p 'using puts'
p table.render(:unicode)
p 'using command'
command.run('echo', table.render(:unicode))
outputs the following
Updated at: 2016-11-28 00:06:41 +0100
"using puts"
"┌───────┬───────┐\n│header1│header2│\n├───────┼───────┤\n│a1 │a2 │\n│b1 │b2 │\n└───────┴───────┘"
"using command"
┌───────┬───────┐
│header1│header2│
├───────┼───────┤
│a1 │a2 │
│b1 │b2 │
└───────┴───────┘
Why are you using the Kernel.p
method? It simply calls inspect
on Ruby string and prints to standard out without evaluating the actual string. When you want to print to console why don't you use puts
or print
. Forget about tty-table
, you can see your issue in the following simple example:
multiline = "line1\nline2\nline3"
p multiline
# => "line1\nline2\nline3"
print multiline
# =>
# line1
# line2
# line3
You don't have to use echo
at all. Simply print to standard out.