vll 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/vl +66 -59
  3. data/bin/vll +3 -1
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b15751061629612b9ce0631e5ffce50cd1bc49d
4
- data.tar.gz: 82be2cbd7eebb2dd8850954c5b0a94c4c1a517a8
3
+ metadata.gz: 8e835b8515174f40da85daa6a5fd92b94f0fc525
4
+ data.tar.gz: 2510a88faf49a449fc58fd70b5e6b970d5c7adbd
5
5
  SHA512:
6
- metadata.gz: 07ae95011d219f39caf6dfbfbb4e141bfd37a42c1cf934830dff0d05713c09d0ea723b80ed163f349d2b6caf1ebdb867cb3f4fbdddb272d61b228109042b0414
7
- data.tar.gz: 59fa7b009f937a2c70e6cdc080998a00dead8398a04f05dabaa9afe7808f4b19c2b9fe20f61306f0fa036db380325b2bd1310e54ba80c8fb703b49e513be8e6f
6
+ metadata.gz: c4ec9e6c335414131aad90f76edcc1d72d36a8fa99a90d5cf301e0f2a2c8cfcd9c14862a60f073b292e6e6366cf311d4b7eede81b3850c39019ce2ee955a1c4b
7
+ data.tar.gz: c15455c89ccf657803aae83979ece54d176910e2741e277b99fd3bec6773e96f85613d538c71f5f9d0d79e424114d68acb9e4fe6782989594558868934db3f38
data/bin/vl CHANGED
@@ -2,10 +2,10 @@
2
2
  begin
3
3
 
4
4
  require 'optparse'
5
- require 'pp'
5
+ require 'csv'
6
6
 
7
- options = {
8
- :separator => /[,\t]/,
7
+ $options = {
8
+ :separator => "",
9
9
  :padding => 1,
10
10
  :skip => 0,
11
11
  :comment => /^#/,
@@ -13,42 +13,46 @@ options = {
13
13
  :justify => 'l'
14
14
  }
15
15
 
16
+
16
17
  opts = OptionParser.new do |opts|
17
18
  opts.program_name = "(V)iew (L)arge table"
18
- opts.version = "0.0.1"
19
- opts.banner = "Usage: vl FILENAME [options]"
19
+
20
+ opts.banner = "(V)iew (L)arge table: lightning fast table formatter"
21
+ opts.separator ''
22
+ opts.separator ' output to stdout:'
23
+ opts.separator ' vl FILENAME [options]'
24
+ opts.separator ' pipe to less:'
20
25
  opts.separator ' vll FILENAME [options]'
21
26
  opts.separator ''
22
27
  opts.separator 'Options:'
23
- opts.separator '-' * 80
24
28
 
25
29
  opts.on('-s', '--separator [REGEX]',
26
- 'regex to match the separator',
27
- ' default: [,\t]') do |s|
30
+ 'char or string to match the separator',
31
+ ' default: "," (if csv, "\t")') do |s|
28
32
  begin
29
33
  options[:separator] = Regexp.new(s)
30
34
  rescue
31
35
  puts 'Warning: Problems encountered when processing the regex.'
32
- puts "Warning: Default value \"#{options[:separator].source}\" is used."
36
+ puts "Warning: Default value \"#{$options[:separator].source}\" is used."
33
37
  end
34
38
  end
35
39
 
36
40
  opts.on('-p', '--padding [NUM_OF_SPACES]',
37
41
  'number of spaces that separate the columns',
38
42
  ' default: 1') do |p|
39
- options[:padding] = p.to_i
43
+ $options[:padding] = p.to_i
40
44
  end
41
45
 
42
46
  opts.on('-k', '--skip [NUM_OF_LINES]',
43
47
  'number of top lines to skip',
44
48
  ' default: 0') do |k|
45
- options[:skip] = k.to_i
49
+ $options[:skip] = k.to_i
46
50
  end
47
51
 
48
52
  opts.on('-p', '--probe-lines [NUM_OF_LINES]',
49
53
  'number of top lines used for fast estimation',
50
54
  ' default: 100') do |p|
51
- options[:probe_lines] = p.to_i
55
+ $options[:probe_lines] = p.to_i
52
56
  end
53
57
 
54
58
  opts.on('-j', '--justify [CODE]',
@@ -57,10 +61,10 @@ opts = OptionParser.new do |opts|
57
61
  ' options: l = left, r = right, a = auto',
58
62
  ' default: l') do |j|
59
63
  if ['l', 'r', 'a'].include?(j[0])
60
- options[:justify] = j[0]
64
+ $options[:justify] = j[0]
61
65
  else
62
66
  puts 'Warning: Unrecognized justify option.'
63
- puts "Warning: Default value \"#{options[:justify]}\" is used"
67
+ puts "Warning: Default value \"#{$options[:justify]}\" is used"
64
68
  end
65
69
  end
66
70
 
@@ -68,80 +72,83 @@ opts = OptionParser.new do |opts|
68
72
  'regex to match lines to ignore',
69
73
  ' default: ^#') do |c|
70
74
  begin
71
- options[:comment] = Regexp.new(c)
75
+ $options[:comment] = Regexp.new(c)
72
76
  rescue
73
77
  puts 'Warning: Problems encountered when processing the regex.'
74
- puts "Warning: Default value \"#{options[:comment].source}\" is used."
78
+ puts "Warning: Default value \"#{$options[:comment].source}\" is used."
75
79
  end
76
80
  end
77
81
  end
78
82
 
79
83
  opts.parse!
80
84
 
81
- if ARGV.empty?
82
- puts 'Error: No filename found.'
83
- puts opts
84
- abort
85
- else
86
- file_name = ARGV[0]
85
+ #default separator
86
+ if $options[:separator] == ''
87
+ if ARGF.filename =~ /.*\.csv/
88
+ $options[:separator] = ','
89
+ else
90
+ $options[:separator] = "\t"
91
+ end
87
92
  end
88
93
 
89
- if !File.exists? file_name
90
- abort "Error: file \"#{file_name}\" not found."
91
- end
92
94
 
93
- max_widths = []
94
95
 
95
- file = open file_name
96
+ #------#
97
+ # Main #
98
+ #------#
99
+
100
+
101
+ $max_widths = []
102
+
96
103
 
97
- file.each_line.with_index.reduce 0 do |pnum, (line, lnum)|
98
- next pnum if lnum < options[:skip] or line =~ options[:comment]
99
- line.chomp.split(options[:separator]).each_with_index do |c, i|
104
+ #==== line caching ====#
105
+ cached_lines = []
106
+ ARGF.each_line.with_index.reduce 0 do |pnum, (line, lnum)|
107
+ cached_lines.push(line)
108
+ next pnum if lnum < $options[:skip] or line =~ $options[:comment]
109
+ CSV.parse_line(line, {:col_sep=>$options[:separator]}).each_with_index do |c, i|
100
110
  # -1 because it's possible that c is empty and i is out of range
101
- if max_widths.fetch(i, -1) < c.length then max_widths[i] = c.length end
111
+ if $max_widths.fetch(i, -1) < c.length then $max_widths[i] = c.length end
102
112
  end
103
- (pnum += 1) < options[:probe_lines] ? pnum : break
113
+ (pnum += 1) < $options[:probe_lines] ? pnum : break
104
114
  end
115
+ #======================#
105
116
 
106
- #lnum = 0
107
- #options[:probe_lines].times do
108
- # begin
109
- # line = file.next rescue break
110
- # lnum += 1
111
- # end while lnum < options[:skip] or line =~ options[:comment]
112
- # line.chomp.split(options[:separator]).each_with_index do |c, i|
113
- # # -1 because it's possible that c is empty and i is out of range
114
- # if max_widths.fetch(i, -1) < c.length then max_widths[i] = c.length end
115
- # end
116
- #end
117
-
118
- file.rewind
119
-
120
- file.each_line.with_index do |line, lnum|
121
- if lnum < options[:skip] or line =~ options[:comment] then
117
+
118
+ def print_line(line, lnum)
119
+ if lnum < $options[:skip] or line =~ $options[:comment] then
122
120
  print line
123
- next
121
+ return
124
122
  end
125
- line.chomp.split(options[:separator]).each_with_index do |c, i|
126
- if max_widths.fetch(i, -1) < c.length then max_widths[i] = c.length end
127
- case options[:justify]
123
+ CSV.parse_line(line, {:col_sep=>$options[:separator]}).each_with_index do |c, i|
124
+ if $max_widths.fetch(i, -1) < c.length then $max_widths[i] = c.length end
125
+ case $options[:justify]
128
126
  when 'l'
129
- print c.ljust(max_widths[i] + options[:padding]) rescue break
127
+ print c.ljust($max_widths[i] + $options[:padding]) rescue break
130
128
  when 'r'
131
- print c.rjust(max_widths[i] + options[:padding]) rescue break
129
+ print c.rjust($max_widths[i] + $options[:padding]) rescue break
132
130
  when 'a'
133
131
  begin
134
- if Float(c) then print c.rjust(max_widths[i] + options[:padding]) end
132
+ if Float(c) then print c.rjust($max_widths[i] + $options[:padding]) end
135
133
  rescue
136
- print c.ljust(max_widths[i] + options[:padding])
134
+ print c.ljust($max_widths[i] + $options[:padding])
137
135
  end
138
136
  end
139
137
  end
140
- print "\n" rescue break
138
+ print "\n" rescue return
139
+ end
140
+
141
+ cached_lines.each.with_index do |line, lnum|
142
+ print_line(line, lnum)
143
+ end
144
+
145
+ ARGF.each_line.with_index do |line, lnum|
146
+ print_line(line, lnum)
141
147
  end
142
148
 
149
+
150
+
151
+
143
152
  rescue Interrupt
144
- # avoid awkward system prompt
145
- print "\n"
146
153
 
147
154
  end
data/bin/vll CHANGED
@@ -1,3 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- exec "vl '#{ARGV.join("' '")}' | less -S"
3
+ require 'shellwords'
4
+
5
+ exec "vl #{ARGV.shelljoin} | less -S"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vll
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - xzhu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Nicely format columns of a large CSV/TSV file according to their content
14
14
  width, using first few lines as estimation thus extremely fast. Customizable.
@@ -42,9 +42,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  version: '0'
43
43
  requirements: []
44
44
  rubyforge_project:
45
- rubygems_version: 2.4.2
45
+ rubygems_version: 2.4.5
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: "(V)iew (L)arge table. Format large table in a split of a second"
49
49
  test_files: []
50
- has_rdoc: