mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-05 22:31:11 +08:00
Add experimental progressive mode
This commit is contained in:
parent
1669024325
commit
f0861bcac4
@ -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
|
||||
|
@ -7,6 +7,7 @@ module YouPlot
|
||||
:transpose,
|
||||
:headers,
|
||||
:pass,
|
||||
:progressive,
|
||||
:output,
|
||||
:fmt,
|
||||
:encoding,
|
||||
|
@ -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|
|
||||
|
Loading…
Reference in New Issue
Block a user