@@ -33,21 +33,34 @@ def run
3333 @options ||= parser . options
3434 @params ||= parser . params
3535
36+ # color command
3637 if %i[ colors color colours colour ] . include? @command
3738 plot = create_plot
3839 output_plot ( plot )
39- elsif options [ :progressive ]
40+ return
41+ end
42+
43+ # progressive mode
44+ if options [ :progressive ]
4045 stop = false
4146 Signal . trap ( :INT ) { stop = true }
42- options [ :output ] . print "\e [?25l" # make cursor invisible
47+
48+ # make cursor invisible
49+ options [ :output ] . print "\e [?25l"
50+
51+ # mainloop
4352 while ( input = Kernel . gets )
4453 n = main_progressive ( input )
4554 break if stop
4655
4756 options [ :output ] . print "\e [#{ n } F"
4857 end
58+
4959 options [ :output ] . print "\e [0J"
50- options [ :output ] . print "\e [?25h" # make cursor visible
60+ # make cursor visible
61+ options [ :output ] . print "\e [?25h"
62+
63+ # normal mode
5164 else
5265 # Sometimes the input file does not end with a newline code.
5366 while ( input = Kernel . gets ( nil ) )
@@ -59,23 +72,32 @@ def run
5972 private
6073
6174 def main ( input )
75+ # Outputs input data to a file or stdout.
6276 output_data ( input )
6377
64- @data = read_dsv ( input )
78+ @data = parse_dsv ( input )
6579
80+ # Debug mode, show parsed results
6681 pp @data if options [ :debug ]
6782
83+ # When run as a program instead of a library
6884 if YouPlot . run_as_executable?
6985 begin
7086 plot = create_plot
7187 rescue ArgumentError => e
88+ # Show only one line of error.
7289 warn e . backtrace [ 0 ]
90+ # Show error message in purple
7391 warn "\e [35m#{ e } \e [0m"
92+ # Explicitly terminated with exit code: 1
7493 exit 1
7594 end
95+
96+ # When running YouPlot as a library (e.g. for testing)
7697 else
7798 plot = create_plot
7899 end
100+
79101 output_plot ( plot )
80102 end
81103
@@ -95,24 +117,28 @@ def main_progressive(input)
95117 @raw_data << input
96118
97119 # FIXME
98- @data = read_dsv ( @raw_data )
120+ @data = parse_dsv ( @raw_data )
99121
100122 plot = create_plot
101123 output_plot_progressive ( plot )
102124 end
103125
104- def read_dsv ( input )
126+ def parse_dsv ( input )
127+ # If encoding is specified, convert to UTF-8
105128 if options [ :encoding ]
106129 input . force_encoding ( options [ :encoding ] )
107130 . encode! ( 'utf-8' )
108131 end
132+
109133 begin
110- DSV . parse ( input , options [ :delimiter ] , options [ :headers ] , options [ :transpose ] )
134+ data = DSV . parse ( input , options [ :delimiter ] , options [ :headers ] , options [ :transpose ] )
111135 rescue CSV ::MalformedCSVError => e
112136 warn 'Failed to parse the text. '
113137 warn 'Please try to set the correct character encoding with --encoding option.'
114138 raise e
115139 end
140+
141+ data
116142 end
117143
118144 def create_plot
0 commit comments