youplot 0.3.5 → 0.4.3

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