Add experimental progressive mode

This commit is contained in:
kojix2 2020-12-21 17:09:47 +09:00 committed by GitHub
parent 1669024325
commit f0861bcac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 4 deletions

View File

@ -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

View File

@ -7,6 +7,7 @@ module YouPlot
:transpose,
:headers,
:pass,
:progressive,
:output,
:fmt,
:encoding,

View File

@ -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|