vll 0.0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/vl +138 -0
  3. data/bin/vll +3 -0
  4. metadata +50 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 50e04bec2ad58ee8640575e09e59bd27aa689f82
4
+ data.tar.gz: f778373d230f3eef2e92b7e6d0e93234c0c5ad27
5
+ SHA512:
6
+ metadata.gz: 73b29ce4ce096657f89e1067349c68c0c5e4ad4e5e0d141b553ff969712c81a2671cb47122fdda5b7051897b880dfbe3acf2b6afbf1b5e554de94faae34fb63b
7
+ data.tar.gz: c0a940aef059fd08f11bdc26520090a7db252495cbe2a874736c5fb2341b9b080153dead684a84b2bc7b57e3db004a454be0b96185fe6570e839d50d48c412a9
data/bin/vl ADDED
@@ -0,0 +1,138 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+
4
+ require 'optparse'
5
+ require 'pp'
6
+
7
+ options = {
8
+ :separator => /[,\t]/,
9
+ :padding => 1,
10
+ :skip => 0,
11
+ :comment => /^#/,
12
+ :probe_lines => 100,
13
+ :justify => 'l'
14
+ }
15
+
16
+ opts = OptionParser.new do |opts|
17
+ opts.program_name = "(V)iew (L)arge table"
18
+ opts.version = "0.0.1"
19
+ opts.banner = "Usage: vl FILENAME [options]"
20
+ opts.separator ' vll FILENAME [options]'
21
+ opts.separator ''
22
+ opts.separator 'Options:'
23
+ opts.separator '-' * 80
24
+
25
+ opts.on('-s', '--separator [REGEX]',
26
+ 'regex to match the separator',
27
+ ' default: [,\t]') do |s|
28
+ begin
29
+ options[:separator] = Regexp.new(s)
30
+ rescue
31
+ puts 'Warning: Problems encountered when processing the regex.'
32
+ puts "Warning: Default value \"#{options[:separator].source}\" is used."
33
+ end
34
+ end
35
+
36
+ opts.on('-p', '--padding [NUM_OF_SPACES]',
37
+ 'number of spaces that separate the columns',
38
+ ' default: 1') do |p|
39
+ options[:padding] = p.to_i
40
+ end
41
+
42
+ opts.on('-k', '--skip [NUM_OF_LINES]',
43
+ 'number of top lines to skip',
44
+ ' default: 0') do |k|
45
+ options[:skip] = k.to_i
46
+ end
47
+
48
+ opts.on('-p', '--probe-lines [NUM_OF_LINES]',
49
+ 'number of top lines used for fast estimation',
50
+ ' default: 100') do |p|
51
+ options[:probe_lines] = p.to_i
52
+ end
53
+
54
+ opts.on('-j', '--justify [CODE]',
55
+ 'justification mode for cells',
56
+ ' auto will only right-justify numbers',
57
+ ' options: l = left, r = right, a = auto',
58
+ ' default: l') do |j|
59
+ if ['l', 'r', 'a'].include?(j[0])
60
+ options[:justify] = j[0]
61
+ else
62
+ puts 'Warning: Unrecognized justify option.'
63
+ puts "Warning: Default value \"#{options[:justify]}\" is used"
64
+ end
65
+ end
66
+
67
+ opts.on('-c', '--comment [REGEX]',
68
+ 'regex to match lines to ignore',
69
+ ' default: ^#') do |c|
70
+ begin
71
+ options[:comment] = Regexp.new(c)
72
+ rescue
73
+ puts 'Warning: Problems encountered when processing the regex.'
74
+ puts "Warning: Default value \"#{options[:comment].source}\" is used."
75
+ end
76
+ end
77
+ end
78
+
79
+ opts.parse!
80
+
81
+ if ARGV.empty?
82
+ puts 'Error: No filename found.'
83
+ puts opts
84
+ abort
85
+ else
86
+ file_name = ARGV[0]
87
+ end
88
+
89
+ if !File.exists? file_name
90
+ abort "Error: file \"#{file_name}\" not found."
91
+ end
92
+
93
+ max_widths = []
94
+
95
+ f_iter = File.foreach(file_name)
96
+
97
+ lnum = 0
98
+ options[:probe_lines].times do
99
+ begin
100
+ line = f_iter.next rescue break
101
+ lnum += 1
102
+ end while lnum < options[:skip] or line =~ options[:comment]
103
+ line.chomp.split(options[:separator]).each_with_index do |c, i|
104
+ # -1 because it's possible that c is empty and i is out of range
105
+ if max_widths.fetch(i, -1) < c.length then max_widths[i] = c.length end
106
+ end
107
+ end
108
+
109
+ f_iter.rewind
110
+
111
+ f_iter.with_index do |line, lnum|
112
+ if lnum < options[:skip] or line =~ options[:comment] then
113
+ print line
114
+ next
115
+ end
116
+ line.chomp.split(options[:separator]).each_with_index do |c, i|
117
+ if max_widths.fetch(i, -1) < c.length then max_widths[i] = c.length end
118
+ case options[:justify]
119
+ when 'l'
120
+ print c.ljust(max_widths[i] + options[:padding]) rescue break
121
+ when 'r'
122
+ print c.rjust(max_widths[i] + options[:padding]) rescue break
123
+ when 'a'
124
+ begin
125
+ if Float(c) then print c.rjust(max_widths[i] + options[:padding]) end
126
+ rescue
127
+ print c.ljust(max_widths[i] + options[:padding])
128
+ end
129
+ end
130
+ end
131
+ print "\n" rescue break
132
+ end
133
+
134
+ rescue Interrupt
135
+ # avoid awkward system prompt
136
+ print "\n"
137
+
138
+ end
data/bin/vll ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ exec "vl #{ARGV.join(" ")} | less -S"
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vll
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - xzhu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Nicely format columns of a large CSV/TSV file according to their content
14
+ width, using first few lines as estimation thus extremely fast. Customizable.
15
+ email:
16
+ - zhuxun2@gmail.com
17
+ executables:
18
+ - vl
19
+ - vll
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - bin/vl
24
+ - bin/vll
25
+ homepage:
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.4.2
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: "(V)iew (L)arge table. Format large table in a split of a second"
49
+ test_files: []
50
+ has_rdoc: