red-data-tools / unicode_plot.rb

Plot your data by Unicode characters
MIT License
249 stars 12 forks source link

Add newline after rendering #23

Closed schneems closed 4 years ago

schneems commented 4 years ago

Currently when you execute this code:

require 'unicode_plot'
require 'stringio'

x = Array.new(100) { rand(10) } + Array.new(100) { rand(30) + 10 }
plot = UnicodePlot.histogram(x, title: "Histogram")
plot.render($stdout)

You will get this output:

β›„ 2.7.0 πŸš€  /tmp
$ ruby scratch.rb
                                Histogram
                β”Œ                                        ┐
   [ 0.0,  5.0) ─▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 49
   [ 5.0, 10.0) ─▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 51
   [10.0, 15.0) ─▇▇▇▇▇▇▇▇▇▇ 14
   [15.0, 20.0) ─▇▇▇▇▇▇▇▇▇▇ 14
   [20.0, 25.0) ─▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 21
   [25.0, 30.0) ─▇▇▇▇▇▇▇▇▇▇▇▇ 17
   [30.0, 35.0) ─▇▇▇▇▇▇▇▇▇▇ 14
   [35.0, 40.0) ─▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 20
                β””                                        β”˜
                                Frequencyβ›„ 2.7.0 πŸš€  /tmp

Can we add an extra newline so that the label on the bottom of the graph isn't rendered on the same line as the next shell input?

mrkn commented 4 years ago

OK. I'll make the modification to let render put a newline at the end of the output.

Thank you for your feedback!

mrkn commented 4 years ago

Another solution is using IO#puts like below:

x = Array.new(100) { rand(10) } + Array.new(100) { rand(30) + 10 }
plot = UnicodePlot.histogram(x, title: "Histogram")
$stdout.puts(plot)

But, in this method the renderer cannot get real tty's property, so the result isn't colored. This restriction in using Plot#to_s is also a bug I want to fix.

schneems commented 4 years ago

Thank you for this fix and for this fantastic library!