vll 0.1.11 → 0.1.12

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.
Files changed (2) hide show
  1. data/bin/vl +120 -50
  2. metadata +5 -5
data/bin/vl CHANGED
@@ -1,8 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- #TODO: add column colors
4
- #TODO: chop long columns (option)
5
-
6
3
  begin
7
4
 
8
5
  require 'optparse'
@@ -108,51 +105,108 @@ end
108
105
  #------#
109
106
 
110
107
 
108
+ #$max_widths = []
109
+ #
110
+ ##==== line caching ====#
111
+ #cached_lines = []
112
+ #ARGF.each_line.with_index.reduce 0 do |pnum, (line, lnum)|
113
+ # cached_lines.push(line)
114
+ # next pnum if lnum < $options[:skip] or line =~ $options[:comment]
115
+ # parsed_line = CSV.parse_line(line, {:col_sep=>$options[:separator], :quote_char=>$options[:quote]})
116
+ # # damn you, 'csv' package!
117
+ # parsed_line = [] if parsed_line == nil
118
+ # parsed_line.each_with_index do |c, i|
119
+ # # damn you, 'csv' package!
120
+ # c = '' if c == nil
121
+ # c = c.inspect[1..-2]
122
+ # # -1 because it's possible that c is empty and i is out of range
123
+ # if $max_widths.fetch(i, -1) < c.length then $max_widths[i] = c.length end
124
+ # end
125
+ # (pnum += 1) < $options[:probe_lines] ? pnum : break
126
+ #end
127
+ ##======================#
128
+ #
129
+ #
130
+ #def print_line(line, lnum)
131
+ # if lnum < $options[:skip] or line =~ $options[:comment] then
132
+ # print line
133
+ # return
134
+ # end
135
+ # parsed_line = CSV.parse_line(line, {:col_sep=>$options[:separator], :quote_char=>$options[:quote]})
136
+ # # damn you, 'csv' package!
137
+ # parsed_line = [] if parsed_line == nil
138
+ # parsed_line.each_with_index do |c, i|
139
+ # break if i > $options[:max_n_cols]
140
+ # if i == $options[:max_n_cols] then
141
+ # print "... #{parsed_line.size - i} more columns"
142
+ # break
143
+ # end
144
+ #
145
+ # # damn you, 'csv' package!
146
+ # c = '' if c == nil
147
+ # c = c.inspect[1..-2]
148
+ #
149
+ # if $max_widths.fetch(i, -1) < c.length then
150
+ # $max_widths[i] = c.length
151
+ # end
152
+ #
153
+ # c = c[0..$options[:chop_length]]
154
+ # $max_widths[i] = [$options[:chop_length], $max_widths[i]].min
155
+ #
156
+ # case $options[:justify]
157
+ # when 'l'
158
+ # c = c.ljust($max_widths[i] + $options[:padding]) rescue break
159
+ # when 'r'
160
+ # c = c.rjust($max_widths[i] + $options[:padding]) rescue break
161
+ # when 'a'
162
+ # begin
163
+ # if Float(c) then c = c.rjust($max_widths[i] + $options[:padding]) end
164
+ # rescue
165
+ # c = c.ljust($max_widths[i] + $options[:padding])
166
+ # end
167
+ # end
168
+ #
169
+ # if $options[:alternate_color] then
170
+ # c = c.colorize([:yellow, :blue][i % 2])
171
+ # end
172
+ #
173
+ # print c
174
+ # end
175
+ # puts
176
+ #rescue
177
+ # STDERR.puts 'vl: Pipe ended while writing.'
178
+ # abort
179
+ #end
180
+ #
181
+ #cached_lines.each.with_index do |line, lnum|
182
+ # print_line(line, lnum)
183
+ #end
184
+ #
185
+ #ARGF.each_line.with_index do |line, lnum|
186
+ # print_line(line, lnum)
187
+ #end
188
+
189
+
190
+
191
+
192
+ #==== new version ===#
193
+
194
+ $cached_lines = []
111
195
  $max_widths = []
196
+ $csv_options = {
197
+ :col_sep => $options[:separator],
198
+ :quote_char => $options[:quote],
199
+ #:skip_lines => $options[:comment]
200
+ }
112
201
 
113
- #==== line caching ====#
114
- cached_lines = []
115
- ARGF.each_line.with_index.reduce 0 do |pnum, (line, lnum)|
116
- cached_lines.push(line)
117
- next pnum if lnum < $options[:skip] or line =~ $options[:comment]
118
- parsed_line = CSV.parse_line(line, {:col_sep=>$options[:separator], :quote_char=>$options[:quote]})
119
- # damn you, 'csv' package!
120
- parsed_line = [] if parsed_line == nil
121
- parsed_line.each_with_index do |c, i|
122
- # damn you, 'csv' package!
123
- c = '' if c == nil
124
- c = c.inspect[1..-2]
125
- # -1 because it's possible that c is empty and i is out of range
126
- if $max_widths.fetch(i, -1) < c.length then $max_widths[i] = c.length end
127
- end
128
- (pnum += 1) < $options[:probe_lines] ? pnum : break
129
- end
130
- #======================#
131
-
132
-
133
- def print_line(line, lnum)
134
- if lnum < $options[:skip] or line =~ $options[:comment] then
135
- print line
136
- return
137
- end
138
- parsed_line = CSV.parse_line(line, {:col_sep=>$options[:separator], :quote_char=>$options[:quote]})
139
- # damn you, 'csv' package!
140
- parsed_line = [] if parsed_line == nil
141
- parsed_line.each_with_index do |c, i|
202
+ def print_row(row)
203
+ row.each_with_index do |c, i|
142
204
  break if i > $options[:max_n_cols]
143
205
  if i == $options[:max_n_cols] then
144
- print "... #{parsed_line.size - i} more columns"
206
+ print "... #{row.size - i} more columns"
145
207
  break
146
208
  end
147
209
 
148
- # damn you, 'csv' package!
149
- c = '' if c == nil
150
- c = c.inspect[1..-2]
151
-
152
- if $max_widths.fetch(i, -1) < c.length then
153
- $max_widths[i] = c.length
154
- end
155
-
156
210
  c = c[0..$options[:chop_length]]
157
211
  $max_widths[i] = [$options[:chop_length], $max_widths[i]].min
158
212
 
@@ -176,22 +230,38 @@ def print_line(line, lnum)
176
230
  print c
177
231
  end
178
232
  puts
179
- rescue
180
- STDERR.puts 'vl: Pipe ended while writing.'
181
- abort
233
+ #rescue
234
+ # STDERR.puts 'vl: Pipe ended while writing.'
235
+ # abort
182
236
  end
183
237
 
184
- cached_lines.each.with_index do |line, lnum|
185
- print_line(line, lnum)
186
- end
238
+ CSV.new(ARGF, $csv_options).each.with_index do |row, i|
239
+ row = row.map.with_index do |c, i|
240
+ # damn you, 'csv' package!
241
+ c = '' if c == nil
242
+ c = c.inspect[1..-2]
243
+ if $max_widths.fetch(i, -1) < c.length then $max_widths[i] = c.length end
244
+ c
245
+ end
187
246
 
188
- ARGF.each_line.with_index do |line, lnum|
189
- print_line(line, lnum)
247
+ if i < $options[:probe_lines] then
248
+ $cached_lines.push(row)
249
+ else
250
+ if $cached_lines.length > 0 then
251
+ $cached_lines.each{|row|print_row(row)}
252
+ $cached_lines = []
253
+ end
254
+ print_row(row)
255
+ end
190
256
  end
191
257
 
192
-
258
+ if $cached_lines.length > 0 then
259
+ $cached_lines.each{|row|print_row(row)}
260
+ $cached_lines = []
261
+ end
193
262
 
194
263
 
195
264
  rescue Interrupt
196
265
 
197
266
  end
267
+
metadata CHANGED
@@ -2,31 +2,31 @@
2
2
  name: vll
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.11
5
+ version: 0.1.12
6
6
  platform: ruby
7
7
  authors:
8
8
  - xzhu
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-08-30 00:00:00.000000000 Z
12
+ date: 2016-09-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  type: :runtime
16
- prerelease: false
17
16
  name: colorize
18
- requirement: !ruby/object:Gem::Requirement
17
+ version_requirements: !ruby/object:Gem::Requirement
19
18
  none: false
20
19
  requirements:
21
20
  - - ! '>='
22
21
  - !ruby/object:Gem::Version
23
22
  version: 0.7.7
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: !ruby/object:Gem::Requirement
25
24
  none: false
26
25
  requirements:
27
26
  - - ! '>='
28
27
  - !ruby/object:Gem::Version
29
28
  version: 0.7.7
29
+ prerelease: false
30
30
  description: Nicely format columns of a large CSV/TSV file according to their content
31
31
  width, using first few lines as estimation thus extremely fast. Customizable.
32
32
  email: