youplot 0.3.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YouPlot
4
- VERSION = '0.3.2'
4
+ VERSION = '0.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-17 00:00:00.000000000 Z
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unicode_plot
@@ -94,8 +94,7 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: "Create ASCII charts on the terminal with data from standard streams
98
- in the \npipeline. \n"
97
+ description: A command line tool for Unicode Plotting
99
98
  email:
100
99
  - 2xijok@gmail.com
101
100
  executables:
@@ -110,12 +109,12 @@ files:
110
109
  - exe/youplot
111
110
  - lib/youplot.rb
112
111
  - lib/youplot/backends/processing.rb
113
- - lib/youplot/backends/unicode_plot_backend.rb
112
+ - lib/youplot/backends/unicode_plot.rb
114
113
  - lib/youplot/command.rb
115
- - lib/youplot/command/cmd_options.rb
116
- - lib/youplot/command/parser.rb
117
- - lib/youplot/command/plot_params.rb
118
- - lib/youplot/dsv_reader.rb
114
+ - lib/youplot/dsv.rb
115
+ - lib/youplot/options.rb
116
+ - lib/youplot/parameters.rb
117
+ - lib/youplot/parser.rb
119
118
  - lib/youplot/version.rb
120
119
  homepage: https://github.com/kojix2/youplot
121
120
  licenses:
@@ -136,8 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
135
  - !ruby/object:Gem::Version
137
136
  version: '0'
138
137
  requirements: []
139
- rubygems_version: 3.1.4
138
+ rubygems_version: 3.2.15
140
139
  signing_key:
141
140
  specification_version: 4
142
- summary: Create Ascii charts on your terminal.
141
+ summary: A command line tool for Unicode Plotting
143
142
  test_files: []
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module YouPlot
4
- class Command
5
- CmdOptions = Struct.new(
6
- :delimiter,
7
- :transpose,
8
- :headers,
9
- :pass,
10
- :output,
11
- :fmt,
12
- :encoding,
13
- :color_names,
14
- :debug,
15
- keyword_init: true
16
- )
17
- end
18
- end
@@ -1,286 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'optparse'
4
- require_relative 'cmd_options'
5
- require_relative 'plot_params'
6
-
7
- module YouPlot
8
- class Command
9
- class Parser
10
- attr_reader :command, :options, :params
11
-
12
- def initialize
13
- @command = nil
14
-
15
- @options = CmdOptions.new(
16
- delimiter: "\t",
17
- transpose: false,
18
- headers: nil,
19
- pass: false,
20
- output: $stderr,
21
- fmt: 'xyy',
22
- encoding: nil,
23
- color_names: false,
24
- debug: false
25
- )
26
-
27
- @params = PlotParams.new
28
- end
29
-
30
- def create_default_parser
31
- OptionParser.new do |opt|
32
- opt.program_name = 'YouPlot'
33
- opt.version = YouPlot::VERSION
34
- opt.summary_width = 24
35
- opt.on_tail('') # Add a blank line at the end
36
- opt.separator('')
37
- opt.on('Common options:')
38
- opt.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
39
- 'for inserting YouPlot in the middle of Unix pipes') do |v|
40
- @options[:pass] = v || $stdout
41
- end
42
- opt.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
43
- 'If no option is specified, plot will print to stderr') do |v|
44
- @options[:output] = v || $stdout
45
- end
46
- opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v|
47
- @options[:delimiter] = v
48
- end
49
- opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
50
- @options[:headers] = v
51
- end
52
- opt.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
53
- @options[:transpose] = v
54
- end
55
- opt.on('-t', '--title VAL', String, 'print string on the top of plot') do |v|
56
- params.title = v
57
- end
58
- opt.on('-x', '--xlabel VAL', String, 'print string on the bottom of the plot') do |v|
59
- params.xlabel = v
60
- end
61
- opt.on('-y', '--ylabel VAL', String, 'print string on the far left of the plot') do |v|
62
- params.ylabel = v
63
- end
64
- opt.on('-w', '--width VAL', Integer, 'number of characters per row') do |v|
65
- params.width = v
66
- end
67
- opt.on('-h', '--height VAL', Numeric, 'number of rows') do |v|
68
- params.height = v
69
- end
70
- opt.on('-b', '--border VAL', String, 'specify the style of the bounding box') do |v|
71
- params.border = v.to_sym
72
- end
73
- opt.on('-m', '--margin VAL', Numeric, 'number of spaces to the left of the plot') do |v|
74
- params.margin = v
75
- end
76
- opt.on('-p', '--padding VAL', Numeric, 'space of the left and right of the plot') do |v|
77
- params.padding = v
78
- end
79
- opt.on('-c', '--color VAL', String, 'color of the drawing') do |v|
80
- params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
81
- end
82
- opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
83
- params.labels = v
84
- end
85
- opt.on('--progress', TrueClass, 'progressive') do |v|
86
- @options[:progressive] = v
87
- end
88
- opt.on('--encoding VAL', String, 'Specify the input encoding') do |v|
89
- @options[:encoding] = v
90
- end
91
- # Optparse adds the help option, but it doesn't show up in usage.
92
- # This is why you need the code below.
93
- opt.on('--help', 'print sub-command help menu') do
94
- puts opt.help
95
- exit
96
- end
97
- opt.on('--debug', TrueClass, 'print preprocessed data') do |v|
98
- @options[:debug] = v
99
- end
100
- yield opt if block_given?
101
- end
102
- end
103
-
104
- def main_parser
105
- @main_parser ||= create_default_parser do |main_parser|
106
- # Here, help message is stored in the banner.
107
- # Because help of main_parser may be referred by `sub_parser`.
108
-
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
- # Actually, main_parser can take common optional arguments.
137
- # However, these options dose not be shown in the help menu.
138
- # I think the main help should be simple.
139
- main_parser.on('--help', 'print sub-command help menu') do
140
- puts main_parser.banner
141
- puts
142
- exit
143
- end
144
- end
145
- end
146
-
147
- def sub_parser
148
- @sub_parser ||= create_default_parser do |parser|
149
- parser.banner = <<~MSG
150
-
151
- Usage: YouPlot #{command} [options] <in.tsv>
152
-
153
- Options for #{command}:
154
- MSG
155
-
156
- case command
157
-
158
- # If you type only `uplot` in the terminal.
159
- when nil
160
- warn main_parser.banner
161
- warn "\n"
162
- exit 1
163
-
164
- when :barplot, :bar
165
- parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
166
- params.symbol = v
167
- end
168
- parser.on_head('--xscale VAL', String, 'axis scaling') do |v|
169
- params.xscale = v
170
- end
171
- parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
172
- @options[:fmt] = v
173
- end
174
-
175
- when :count, :c
176
- parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
177
- params.symbol = v
178
- end
179
-
180
- when :histogram, :hist
181
- parser.on_head('-n', '--nbins VAL', Numeric, 'approximate number of bins') do |v|
182
- params.nbins = v
183
- end
184
- parser.on_head('--closed VAL', String) do |v|
185
- params.closed = v
186
- end
187
- parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
188
- params.symbol = v
189
- end
190
-
191
- when :lineplot, :line
192
- parser.on_head('--canvas VAL', String, 'type of canvas') do |v|
193
- params.canvas = v
194
- end
195
- parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
196
- params.xlim = v.take(2)
197
- end
198
- parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
199
- params.ylim = v.take(2)
200
- end
201
- parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
202
- @options[:fmt] = v
203
- end
204
-
205
- when :lineplots, :lines
206
- parser.on_head('--canvas VAL', String) do |v|
207
- params.canvas = v
208
- end
209
- parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
210
- params.xlim = v.take(2)
211
- end
212
- parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
213
- params.ylim = v.take(2)
214
- end
215
- parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
216
- 'xyy : header is like x, y1, y2, y2, y3...') do |v|
217
- @options[:fmt] = v
218
- end
219
-
220
- when :scatter, :s
221
- parser.on_head('--canvas VAL', String) do |v|
222
- params.canvas = v
223
- end
224
- parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
225
- params.xlim = v.take(2)
226
- end
227
- parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
228
- params.ylim = v.take(2)
229
- end
230
- parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
231
- 'xyy : header is like x, y1, y2, y2, y3...') do |v|
232
- @options[:fmt] = v
233
- end
234
-
235
- when :density, :d
236
- parser.on_head('--grid', TrueClass) do |v|
237
- params.grid = v
238
- end
239
- parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
240
- params.xlim = v.take(2)
241
- end
242
- parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
243
- params.ylim = v.take(2)
244
- end
245
- parser.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
246
- 'xyy : header is like x, y1, y2, y2, y3...') do |v|
247
- @options[:fmt] = v
248
- end
249
-
250
- when :boxplot, :box
251
- parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
252
- params.xlim = v.take(2)
253
- end
254
-
255
- when :colors, :color, :colours, :colour
256
- parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
257
- @options[:color_names] = v
258
- end
259
-
260
- else
261
- warn "uplot: unrecognized command '#{command}'"
262
- exit 1
263
- end
264
- end
265
- end
266
-
267
- def parse_options(argv = ARGV)
268
- begin
269
- main_parser.order!(argv)
270
- rescue OptionParser::ParseError => e
271
- warn "uplot: #{e.message}"
272
- exit 1
273
- end
274
-
275
- @command = argv.shift&.to_sym
276
-
277
- begin
278
- sub_parser.parse!(argv)
279
- rescue OptionParser::ParseError => e
280
- warn "uplot: #{e.message}"
281
- exit 1
282
- end
283
- end
284
- end
285
- end
286
- 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
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'csv'
4
-
5
- module YouPlot
6
- # Read and interpret Delimiter-separated values format file or stream.
7
- module DSVReader
8
- module_function
9
-
10
- def input(input, delimiter, headers, transpose)
11
- arr = parse_as_csv(input, delimiter)
12
- headers = get_headers(arr, headers, transpose)
13
- series = get_series(arr, headers, transpose)
14
- if headers.nil?
15
- Data.new(headers, series)
16
- else
17
- if headers.include?(nil)
18
- warn "\e[35mHeaders contains nil in it.\e[0m"
19
- elsif headers.include? ''
20
- warn "\e[35mHeaders contains \"\" in it.\e[0m"
21
- end
22
- h_size = headers.size
23
- s_size = series.size
24
- if h_size == s_size
25
- Data.new(headers, series)
26
- elsif h_size > s_size
27
- warn "\e[35mThe number of headers is greater than the number of series.\e[0m"
28
- exit 1
29
- elsif h_size < s_size
30
- warn "\e[35mThe number of headers is less than the number of series.\e[0m"
31
- exit 1
32
- end
33
- end
34
- end
35
-
36
- def parse_as_csv(input, delimiter)
37
- CSV.parse(input, col_sep: delimiter)
38
- .delete_if do |i|
39
- i == [] or i.all? nil
40
- end
41
- end
42
-
43
- # Transpose different sized ruby arrays
44
- # https://stackoverflow.com/q/26016632
45
- def transpose2(arr)
46
- Array.new(arr.map(&:length).max) { |i| arr.map { |e| e[i] } }
47
- end
48
-
49
- def get_headers(arr, headers, transpose)
50
- if headers
51
- if transpose
52
- arr.map(&:first)
53
- else
54
- arr[0]
55
- end
56
- end
57
- end
58
-
59
- def get_series(arr, headers, transpose)
60
- if transpose
61
- if headers
62
- arr.map { |row| row[1..-1] }
63
- else
64
- arr
65
- end
66
- elsif headers
67
- transpose2(arr[1..-1])
68
- else
69
- transpose2(arr)
70
- end
71
- end
72
- end
73
- end