tabelr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ Tabler
2
+ ======
3
+
4
+ Creates neat aligned ascii tables from a json input
5
+
6
+ This is mainly useful on SQU query output from Sequel Pro app, which can save results as json.
7
+
8
+ I'm sure there are a 100+ versions of this around, but I wrote this mainly for the challenge.
9
+
10
+
11
+ Usage
12
+ -----
13
+
14
+ ruby bin/tabelr test/example.json test/output.txt
15
+
16
+
17
+ input
18
+ -----
19
+
20
+ - The input must be a json formatted file
21
+
22
+ - Which contains a hash
23
+ - with one key named `data`
24
+ - and one value containing an array of hashes
25
+
26
+ - Each hash must contain the same number of same named keys, which this will use as the column headers
27
+
28
+ E.g. (extract from `test/example.json`)
29
+
30
+ {
31
+ "data":
32
+ [
33
+ {
34
+ "column1": "This",
35
+ "column2": "is",
36
+ "column3": "quite",
37
+ "column4": "cool!"
38
+ },
39
+ {
40
+ "column1": "This",
41
+ "column2": "is the longest field",
42
+ "column3": "by far",
43
+ "column4": 1241
44
+ },
45
+
46
+ output
47
+ ------
48
+
49
+ The output filename can be omitted, it will then default to the STDOUT terminal
50
+
51
+ Given the file input above, the output will look like :-
52
+
53
+ +---------+----------------------+---------+------------------+
54
+ | column1 | column2 | column3 | column4 |
55
+ +---------+----------------------+---------+------------------+
56
+ | This | is | quite | cool! |
57
+ | This | is the longest field | by far | 1241 |
58
+ | Short | fields | fit | in |
59
+ | other | eg | | 3322119999999999 |
60
+ +---------+----------------------+---------+------------------+
61
+
62
+
data/bin/tabelr ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require './lib/tabelr'
4
+
5
+ Tabelr::run
data/lib/tabelr.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require './lib/tabelr/table_formater'
3
+
4
+ module Tabelr
5
+ class << self
6
+
7
+ def run
8
+ args = parse_args
9
+ @tf = TableFormator.new
10
+
11
+ json = JSON.parse(File.read(args[0]))
12
+ output = STDOUT
13
+ output = File.open(args[1], 'w') unless args[1].nil?
14
+
15
+ @tf.go json, output
16
+ end
17
+
18
+ def parse_args
19
+ commands = []
20
+ ARGV.each do |arg|
21
+ commands << arg unless arg.index('-') === 0
22
+ end
23
+ commands
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,75 @@
1
+ module Tabelr
2
+ class TableFormator
3
+
4
+ def initialize
5
+ @lines = []
6
+ @line = []
7
+ end
8
+
9
+ def go json, output
10
+ parse json
11
+ analyse
12
+ dump output
13
+ end
14
+
15
+ def stash data
16
+ @line << data
17
+ end
18
+
19
+ def bank
20
+ @lines << @line.dup
21
+ @line.clear
22
+ end
23
+
24
+ def parse json
25
+ # header
26
+ json['data'].first.each_key { |key| stash key }
27
+ bank
28
+
29
+ # content/row
30
+ json['data'].each do |line|
31
+ line.each_value { |value| stash value }
32
+ bank
33
+ end
34
+ end
35
+
36
+ def analyse
37
+ @max_len = []
38
+ @lines.each do |line|
39
+ line.each_with_index do |item, i|
40
+ @max_len[i] = max @max_len[i], item.to_s.length
41
+ end
42
+ end
43
+ end
44
+
45
+ def dump output
46
+ output.printf divider
47
+ @lines.each_with_index do |line, index|
48
+ output.printf divider if index == 1
49
+ line.each_with_index do |item, i|
50
+ output.printf format item.to_s, @max_len[i]
51
+ end
52
+ output.printf "|\n"
53
+ end
54
+ output.printf divider
55
+ end
56
+
57
+ def max a, b
58
+ return a if b.nil?
59
+ return b if a.nil?
60
+ return a if a > b
61
+ b
62
+ end
63
+
64
+ def format text, count
65
+ "| #{text} " + " " * (count-text.length)
66
+ end
67
+
68
+ def divider
69
+ s = "+"
70
+ @max_len.each { |n| s += "-" * (n+2) + "+" }
71
+ s += "\n"
72
+ end
73
+
74
+ end
75
+ end
data/output.txt ADDED
@@ -0,0 +1,8 @@
1
+ +---------+----------------------+---------+------------------+
2
+ | column1 | column2 | column3 | column4 |
3
+ +---------+----------------------+---------+------------------+
4
+ | This | is | quite | cool! |
5
+ | This | is the longest field | by far | 1241 |
6
+ | Short | fields | fit | in |
7
+ | other | eg | | 3322119999999999 |
8
+ +---------+----------------------+---------+------------------+
data/tabelr.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "tabelr"
3
+ s.summary = "Creates neat aligned ascii tables from a json input"
4
+ s.description = File.read(File.join(File.dirname(__FILE__), 'README.md'))
5
+ s.requirements = [ 'Dont know of any!' ]
6
+ s.version = "0.0.1"
7
+ s.author = "Ian Vaughan"
8
+ s.email = "tabelr@ianvaughan.co.uk"
9
+ s.homepage = ''
10
+ s.platform = Gem::Platform::RUBY
11
+ s.required_ruby_version = '>=1.9'
12
+ s.files = Dir['**/**']
13
+ s.executables = [ 'tabelr' ]
14
+ s.test_files = Dir["test/test*.rb"]
15
+ s.has_rdoc = false
16
+ end
data/test/example.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "data":
3
+ [
4
+ {
5
+ "column1": "This",
6
+ "column2": "is",
7
+ "column3": "quite",
8
+ "column4": "cool!"
9
+ },
10
+ {
11
+ "column1": "This",
12
+ "column2": "is the longest field",
13
+ "column3": "by far",
14
+ "column4": 1241
15
+ },
16
+ {
17
+ "column1": "Short",
18
+ "column2": "fields",
19
+ "column3": "fit",
20
+ "column4": "in"
21
+ },
22
+ {
23
+ "column1": "other",
24
+ "column2": "eg",
25
+ "column3": null,
26
+ "column4": 3322119999999999
27
+ }
28
+ ]
29
+ }
data/test/output.txt ADDED
@@ -0,0 +1,8 @@
1
+ +---------+----------------------+---------+------------------+
2
+ | column1 | column2 | column3 | column4 |
3
+ +---------+----------------------+---------+------------------+
4
+ | This | is | quite | cool! |
5
+ | This | is the longest field | by far | 1241 |
6
+ | Short | fields | fit | in |
7
+ | other | eg | | 3322119999999999 |
8
+ +---------+----------------------+---------+------------------+
@@ -0,0 +1,10 @@
1
+ require "test/unit"
2
+
3
+ class TestString < Test::Unit::TestCase
4
+ def test_camel_case
5
+ input = "THIS IS A STRING"
6
+ test = input.camelcase
7
+ assert_equal "This Is A String", test
8
+ assert_equal "THIS IS A STRING", input
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tabelr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ian Vaughan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "Tabler\n======\n\nCreates neat aligned ascii tables from a json input\n\nThis
15
+ is mainly useful on SQU query output from Sequel Pro app, which can save results
16
+ as json.\n\nI'm sure there are a 100+ versions of this around, but I wrote this
17
+ mainly for the challenge.\n\n\nUsage\n-----\n\n ruby bin/tabelr test/example.json
18
+ test/output.txt\n\n\ninput\n-----\n\n- The input must be a json formatted file\n\n-
19
+ Which contains a hash\n - with one key named `data`\n - and one value containing
20
+ an array of hashes\n\n- Each hash must contain the same number of same named keys,
21
+ which this will use as the column headers\n\nE.g. (extract from `test/example.json`)\n\n
22
+ \ {\n \"data\":\n [\n {\n \"column1\": \"This\",\n \"column2\":
23
+ \"is\",\n \"column3\": \"quite\",\n \"column4\": \"cool!\"\n },\n
24
+ \ {\n \"column1\": \"This\",\n \"column2\": \"is the longest
25
+ field\",\n \"column3\": \"by far\",\n \"column4\": 1241\n },\n\noutput\n------\n\nThe
26
+ output filename can be omitted, it will then default to the STDOUT terminal\n\nGiven
27
+ the file input above, the output will look like :-\n\n +---------+----------------------+---------+------------------+\n
28
+ \ | column1 | column2 | column3 | column4 |\n +---------+----------------------+---------+------------------+\n
29
+ \ | This | is | quite | cool! |\n | This
30
+ \ | is the longest field | by far | 1241 |\n | Short | fields
31
+ \ | fit | in |\n | other | eg |
32
+ \ | 3322119999999999 |\n +---------+----------------------+---------+------------------+\n\n\n"
33
+ email: tabelr@ianvaughan.co.uk
34
+ executables:
35
+ - tabelr
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - bin/tabelr
40
+ - lib/tabelr/table_formater.rb
41
+ - lib/tabelr.rb
42
+ - output.txt
43
+ - README.md
44
+ - tabelr.gemspec
45
+ - test/example.json
46
+ - test/output.txt
47
+ - test/tabelr_test.rb
48
+ homepage: ''
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '1.9'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements:
67
+ - Dont know of any!
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.17
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Creates neat aligned ascii tables from a json input
73
+ test_files: []