diff --git a/lib/youplot/command.rb b/lib/youplot/command.rb index d6f1a24..6851aa5 100644 --- a/lib/youplot/command.rb +++ b/lib/youplot/command.rb @@ -32,9 +32,15 @@ module YouPlot plot = create_plot output_plot(plot) else - # Sometimes the input file does not end with a newline code. - while (input = Kernel.gets(nil)) - main(input) + if options[:progressive] + while (input = Kernel.gets) + main_progress(input) + end + else + # Sometimes the input file does not end with a newline code. + while (input = Kernel.gets(nil)) + main(input) + end end end end @@ -52,6 +58,18 @@ module YouPlot output_plot(plot) end + def main_progress(input) + output_data(input) + + @raw_data ||= String.new + @raw_data << input + + @data = read_dsv(@raw_data) + + plot = create_plot + output_plot_progressive(plot) + end + def read_dsv(input) input = input.dup.force_encoding(options[:encoding]).encode('utf-8') if options[:encoding] DSV.parse(input, options[:delimiter], options[:headers], options[:transpose]) @@ -106,5 +124,27 @@ module YouPlot end end end + + def output_plot_progressive(plot) + case options[:output] + when IO + # RefactorMe + @output_stringio = StringIO.new(String.new) + def @output_stringio.tty?; true; end + out = @output_stringio.clone + plot.render(out) + lines = out.string.lines + lines.each do |line| + options[:output].print line.chomp + options[:output].print "\e[0K" + options[:output].puts + end + options[:output].flush + n = out.string.lines.size + options[:output].print "\e[#{n}F" + else + raise "In progressive mode, output to a file is not possible." + end + end end end diff --git a/lib/youplot/command/options.rb b/lib/youplot/command/options.rb index 12ed749..5c46f35 100644 --- a/lib/youplot/command/options.rb +++ b/lib/youplot/command/options.rb @@ -7,6 +7,7 @@ module YouPlot :transpose, :headers, :pass, + :progressive, :output, :fmt, :encoding, diff --git a/lib/youplot/command/parser.rb b/lib/youplot/command/parser.rb index fbadae6..02a29ae 100644 --- a/lib/youplot/command/parser.rb +++ b/lib/youplot/command/parser.rb @@ -82,7 +82,7 @@ module YouPlot opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v| params.labels = v end - opt.on('--progress', TrueClass, 'progressive') do |v| + opt.on('--progress', TrueClass, 'progressive mode [experimental]') do |v| @options[:progressive] = v end opt.on('--encoding VAL', String, 'Specify the input encoding') do |v|