youplot 0.3.4 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,297 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'optparse'
4
- require_relative 'options'
5
- require_relative 'plot_params'
6
-
7
- module YouPlot
8
- class Command
9
- class Parser
10
- class Error < StandardError; end
11
-
12
- attr_reader :command, :options, :params,
13
- :main_parser, :sub_parser
14
-
15
- def initialize
16
- @command = nil
17
-
18
- @options = Options.new(
19
- delimiter: "\t",
20
- transpose: false,
21
- headers: nil,
22
- pass: false,
23
- output: $stderr,
24
- fmt: 'xyy',
25
- encoding: nil,
26
- color_names: false,
27
- debug: false
28
- )
29
-
30
- @params = PlotParams.new
31
- end
32
-
33
- def create_default_parser
34
- OptionParser.new do |parser|
35
- parser.program_name = 'YouPlot'
36
- parser.version = YouPlot::VERSION
37
- parser.summary_width = 24
38
- parser.on_tail('') # Add a blank line at the end
39
- parser.separator('')
40
- parser.on('Common options:')
41
- parser.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
42
- 'for inserting YouPlot in the middle of Unix pipes') do |v|
43
- options[:pass] = v || $stdout
44
- end
45
- parser.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
46
- 'If no option is specified, plot will print to stderr') do |v|
47
- options[:output] = v || $stdout
48
- end
49
- parser.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v|
50
- options[:delimiter] = v
51
- end
52
- parser.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
53
- options[:headers] = v
54
- end
55
- parser.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
56
- options[:transpose] = v
57
- end
58
- parser.on('-t', '--title STR', String, 'print string on the top of plot') do |v|
59
- params.title = v
60
- end
61
- parser.on('-x', '--xlabel STR', String, 'print string on the bottom of the plot') do |v|
62
- params.xlabel = v
63
- end
64
- parser.on('-y', '--ylabel STR', String, 'print string on the far left of the plot') do |v|
65
- params.ylabel = v
66
- end
67
- parser.on('-w', '--width INT', Integer, 'number of characters per row') do |v|
68
- params.width = v
69
- end
70
- parser.on('-h', '--height INT', Numeric, 'number of rows') do |v|
71
- params.height = v
72
- end
73
- parser.on('-b', '--border STR', String, 'specify the style of the bounding box') do |v|
74
- params.border = v.to_sym
75
- end
76
- parser.on('-m', '--margin INT', Numeric, 'number of spaces to the left of the plot') do |v|
77
- params.margin = v
78
- end
79
- parser.on('--padding INT', Numeric, 'space of the left and right of the plot') do |v|
80
- params.padding = v
81
- end
82
- parser.on('-c', '--color VAL', String, 'color of the drawing') do |v|
83
- params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
84
- end
85
- parser.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
86
- params.labels = v
87
- end
88
- parser.on('-p', '--progress', TrueClass, 'progressive mode [experimental]') do |v|
89
- options[:progressive] = v
90
- end
91
- parser.on('--encoding STR', String, 'Specify the input encoding') do |v|
92
- options[:encoding] = v
93
- end
94
- # Optparse adds the help option, but it doesn't show up in usage.
95
- # This is why you need the code below.
96
- parser.on('--help', 'print sub-command help menu') do
97
- puts parser.help
98
- exit if YouPlot.run_as_executable?
99
- end
100
- parser.on('--debug', TrueClass, 'print preprocessed data') do |v|
101
- options[:debug] = v
102
- end
103
- # yield opt if block_given?
104
- end
105
- end
106
-
107
- def create_main_parser
108
- @main_parser = create_default_parser
109
- main_parser.banner = \
110
- <<~MSG
111
-
112
- Program: YouPlot (Tools for plotting on the terminal)
113
- Version: #{YouPlot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
114
- Source: https://github.com/kojix2/youplot
115
-
116
- Usage: uplot <command> [options] <in.tsv>
117
-
118
- Commands:
119
- barplot bar draw a horizontal barplot
120
- histogram hist draw a horizontal histogram
121
- lineplot line draw a line chart
122
- lineplots lines draw a line chart with multiple series
123
- scatter s draw a scatter plot
124
- density d draw a density plot
125
- boxplot box draw a horizontal boxplot
126
- colors color show the list of available colors
127
-
128
- count c draw a baplot based on the number of
129
- occurrences (slow)
130
-
131
- General options:
132
- --help print command specific help menu
133
- --version print the version of YouPlot
134
- MSG
135
-
136
- # Help for the main parser is simple.
137
- # Simply show the banner above.
138
- main_parser.on('--help', 'print sub-command help menu') do
139
- puts main_parser.banner
140
- puts
141
- exit if YouPlot.run_as_executable?
142
- end
143
- end
144
-
145
- def sub_parser_add_symbol
146
- sub_parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
147
- params.symbol = v
148
- end
149
- end
150
-
151
- def sub_parser_add_xscale
152
- xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
153
- sub_parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v|
154
- params.xscale = v
155
- end
156
- end
157
-
158
- def sub_parser_add_canvas
159
- sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v|
160
- params.canvas = v.to_sym
161
- end
162
- end
163
-
164
- def sub_parser_add_xlim
165
- sub_parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
166
- params.xlim = v.take(2)
167
- end
168
- end
169
-
170
- def sub_parser_add_ylim
171
- sub_parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
172
- params.ylim = v.take(2)
173
- end
174
- end
175
-
176
- def sub_parser_add_grid
177
- sub_parser.on_head('--[no-]grid', TrueClass, 'draws grid-lines at the origin') do |v|
178
- params.grid = v
179
- end
180
- end
181
-
182
- def create_sub_parser
183
- @sub_parser = create_default_parser
184
- sub_parser.banner = \
185
- <<~MSG
186
-
187
- Usage: YouPlot #{command} [options] <in.tsv>
188
-
189
- Options for #{command}:
190
- MSG
191
-
192
- case command
193
-
194
- # If you type only `uplot` in the terminal.
195
- when nil
196
- warn main_parser.banner
197
- warn "\n"
198
- exit 1 if YouPlot.run_as_executable?
199
-
200
- when :barplot, :bar
201
- sub_parser_add_symbol
202
- sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
203
- options[:fmt] = v
204
- end
205
- sub_parser_add_xscale
206
-
207
- when :count, :c
208
- sub_parser_add_symbol
209
- sub_parser_add_xscale
210
-
211
- when :histogram, :hist
212
- sub_parser_add_symbol
213
- sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
214
- params.closed = v
215
- end
216
- sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
217
- params.nbins = v
218
- end
219
-
220
- when :lineplot, :line
221
- sub_parser_add_canvas
222
- sub_parser_add_grid
223
- sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
224
- options[:fmt] = v
225
- end
226
- sub_parser_add_ylim
227
- sub_parser_add_xlim
228
-
229
- when :lineplots, :lines
230
- sub_parser_add_canvas
231
- sub_parser_add_grid
232
- sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
233
- 'xyy : header is like x, y1, y2, y2, y3...') do |v|
234
- options[:fmt] = v
235
- end
236
- sub_parser_add_ylim
237
- sub_parser_add_xlim
238
-
239
- when :scatter, :s
240
- sub_parser_add_canvas
241
- sub_parser_add_grid
242
- sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
243
- 'xyy : header is like x, y1, y2, y2, y3...') do |v|
244
- options[:fmt] = v
245
- end
246
- sub_parser_add_ylim
247
- sub_parser_add_xlim
248
-
249
- when :density, :d
250
- sub_parser_add_canvas
251
- sub_parser_add_grid
252
- sub_parser.on('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
253
- 'xyy : header is like x, y1, y2, y2, y3...') do |v|
254
- options[:fmt] = v
255
- end
256
- sub_parser_add_ylim
257
- sub_parser_add_xlim
258
-
259
- when :boxplot, :box
260
- sub_parser_add_xlim
261
-
262
- when :colors, :color, :colours, :colour
263
- sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
264
- options[:color_names] = v
265
- end
266
-
267
- else
268
- error_message = "uplot: unrecognized command '#{command}'"
269
- if YouPlot.run_as_executable?
270
- warn error_message
271
- exit 1
272
- else
273
- raise Error, error_message
274
- end
275
- end
276
- end
277
-
278
- def parse_options(argv = ARGV)
279
- begin
280
- create_main_parser.order!(argv)
281
- rescue OptionParser::ParseError => e
282
- warn "uplot: #{e.message}"
283
- exit 1 if YouPlot.run_as_executable?
284
- end
285
-
286
- @command = argv.shift&.to_sym
287
-
288
- begin
289
- create_sub_parser&.parse!(argv)
290
- rescue OptionParser::ParseError => e
291
- warn "uplot: #{e.message}"
292
- exit 1 if YouPlot.run_as_executable?
293
- end
294
- end
295
- end
296
- end
297
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module YouPlot
4
- class Command
5
- # UnicodePlot parameters.
6
- # * Normally in a Ruby program, you might use hash for the parameter object.
7
- # * Here, I use Struct for 2 safety reason.
8
- # * The keys are static in Struct.
9
- # * Struct does not conflict with keyword arguments. Hash dose.
10
- PlotParams = Struct.new(
11
- # Sort me!
12
- :title,
13
- :width,
14
- :height,
15
- :border,
16
- :margin,
17
- :padding,
18
- :color,
19
- :xlabel,
20
- :ylabel,
21
- :labels,
22
- :symbol,
23
- :xscale,
24
- :nbins,
25
- :closed,
26
- :canvas,
27
- :xlim,
28
- :ylim,
29
- :grid,
30
- :name
31
- ) do
32
- def to_hc
33
- to_h.compact
34
- end
35
- end
36
- end
37
- end