mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-06 07:10:10 +08:00
Add comments to command class
This commit is contained in:
parent
85d818d8ca
commit
2eca86268c
@ -33,21 +33,34 @@ module YouPlot
|
|||||||
@options ||= parser.options
|
@options ||= parser.options
|
||||||
@params ||= parser.params
|
@params ||= parser.params
|
||||||
|
|
||||||
|
# color command
|
||||||
if %i[colors color colours colour].include? @command
|
if %i[colors color colours colour].include? @command
|
||||||
plot = create_plot
|
plot = create_plot
|
||||||
output_plot(plot)
|
output_plot(plot)
|
||||||
elsif options[:progressive]
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
# progressive mode
|
||||||
|
if options[:progressive]
|
||||||
stop = false
|
stop = false
|
||||||
Signal.trap(:INT) { stop = true }
|
Signal.trap(:INT) { stop = true }
|
||||||
options[:output].print "\e[?25l" # make cursor invisible
|
|
||||||
|
# make cursor invisible
|
||||||
|
options[:output].print "\e[?25l"
|
||||||
|
|
||||||
|
# mainloop
|
||||||
while (input = Kernel.gets)
|
while (input = Kernel.gets)
|
||||||
n = main_progressive(input)
|
n = main_progressive(input)
|
||||||
break if stop
|
break if stop
|
||||||
|
|
||||||
options[:output].print "\e[#{n}F"
|
options[:output].print "\e[#{n}F"
|
||||||
end
|
end
|
||||||
|
|
||||||
options[:output].print "\e[0J"
|
options[:output].print "\e[0J"
|
||||||
options[:output].print "\e[?25h" # make cursor visible
|
# make cursor visible
|
||||||
|
options[:output].print "\e[?25h"
|
||||||
|
|
||||||
|
# normal mode
|
||||||
else
|
else
|
||||||
# Sometimes the input file does not end with a newline code.
|
# Sometimes the input file does not end with a newline code.
|
||||||
while (input = Kernel.gets(nil))
|
while (input = Kernel.gets(nil))
|
||||||
@ -59,23 +72,32 @@ module YouPlot
|
|||||||
private
|
private
|
||||||
|
|
||||||
def main(input)
|
def main(input)
|
||||||
|
# Outputs input data to a file or stdout.
|
||||||
output_data(input)
|
output_data(input)
|
||||||
|
|
||||||
@data = read_dsv(input)
|
@data = parse_dsv(input)
|
||||||
|
|
||||||
|
# Debug mode, show parsed results
|
||||||
pp @data if options[:debug]
|
pp @data if options[:debug]
|
||||||
|
|
||||||
|
# When run as a program instead of a library
|
||||||
if YouPlot.run_as_executable?
|
if YouPlot.run_as_executable?
|
||||||
begin
|
begin
|
||||||
plot = create_plot
|
plot = create_plot
|
||||||
rescue ArgumentError => e
|
rescue ArgumentError => e
|
||||||
|
# Show only one line of error.
|
||||||
warn e.backtrace[0]
|
warn e.backtrace[0]
|
||||||
|
# Show error message in purple
|
||||||
warn "\e[35m#{e}\e[0m"
|
warn "\e[35m#{e}\e[0m"
|
||||||
|
# Explicitly terminated with exit code: 1
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# When running YouPlot as a library (e.g. for testing)
|
||||||
else
|
else
|
||||||
plot = create_plot
|
plot = create_plot
|
||||||
end
|
end
|
||||||
|
|
||||||
output_plot(plot)
|
output_plot(plot)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -95,24 +117,28 @@ module YouPlot
|
|||||||
@raw_data << input
|
@raw_data << input
|
||||||
|
|
||||||
# FIXME
|
# FIXME
|
||||||
@data = read_dsv(@raw_data)
|
@data = parse_dsv(@raw_data)
|
||||||
|
|
||||||
plot = create_plot
|
plot = create_plot
|
||||||
output_plot_progressive(plot)
|
output_plot_progressive(plot)
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_dsv(input)
|
def parse_dsv(input)
|
||||||
|
# If encoding is specified, convert to UTF-8
|
||||||
if options[:encoding]
|
if options[:encoding]
|
||||||
input.force_encoding(options[:encoding])
|
input.force_encoding(options[:encoding])
|
||||||
.encode!('utf-8')
|
.encode!('utf-8')
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
DSV.parse(input, options[:delimiter], options[:headers], options[:transpose])
|
data = DSV.parse(input, options[:delimiter], options[:headers], options[:transpose])
|
||||||
rescue CSV::MalformedCSVError => e
|
rescue CSV::MalformedCSVError => e
|
||||||
warn 'Failed to parse the text. '
|
warn 'Failed to parse the text. '
|
||||||
warn 'Please try to set the correct character encoding with --encoding option.'
|
warn 'Please try to set the correct character encoding with --encoding option.'
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_plot
|
def create_plot
|
||||||
|
Loading…
Reference in New Issue
Block a user