clean_text_table 0.0.1
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.
- data/README +15 -0
- data/lib/clean_text_table.rb +3 -0
- data/lib/clean_text_table/table.rb +138 -0
- data/test/test-table.rb +24 -0
- metadata +50 -0
data/README
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
= CleanTextTable
|
2
|
+
|
3
|
+
This package contains clean-txt-table, a simple ruby tool that can transform rows of delimited text into an ASCII table.
|
4
|
+
|
5
|
+
clean-txt-table has the following features:
|
6
|
+
|
7
|
+
* Users can choose from two output options. The standard output option will display the ASCII table as plain as it gets with a row header and then the contents of the table. The alternate method of display will add a "border line" between each row of data. In this mode, users can choose to print the row header for each row of data or only one row header.
|
8
|
+
|
9
|
+
== Download
|
10
|
+
|
11
|
+
The latest version of clean-txt-table can be found at
|
12
|
+
|
13
|
+
* http://rubyforge.org/frs/?group_id=5273
|
14
|
+
|
15
|
+
== Instalation
|
@@ -0,0 +1,138 @@
|
|
1
|
+
class Table
|
2
|
+
|
3
|
+
attr_accessor :table_hsh, :field_width_hsh, :num_fields, :header, :separate_each_line, :header_per_line
|
4
|
+
|
5
|
+
def initialize( separate_each_line, header_per_line )
|
6
|
+
@table_hsh = Hash.new
|
7
|
+
@field_width_hsh = Hash.new
|
8
|
+
@num_fields = 0
|
9
|
+
@separate_each_line = separate_each_line
|
10
|
+
@header_per_line = header_per_line
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_row( row, sep )
|
14
|
+
fields = row.split( sep )
|
15
|
+
if fields.length > @num_fields
|
16
|
+
@num_fields = fields.length
|
17
|
+
end
|
18
|
+
i = @table_hsh.length
|
19
|
+
@table_hsh[ i ] = fields
|
20
|
+
for col in 0..fields.length - 1
|
21
|
+
curr_width = 0
|
22
|
+
if !fields[ col ].nil?
|
23
|
+
curr_width = fields[ col ].length
|
24
|
+
end
|
25
|
+
max_width = @field_width_hsh[ col ]
|
26
|
+
if max_width.nil? || curr_width > max_width
|
27
|
+
@field_width_hsh[ col ] = curr_width
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def add_header( header, sep )
|
33
|
+
@header = header.split( sep )
|
34
|
+
if @header.length > @num_fields
|
35
|
+
@num_fields = @header.length
|
36
|
+
end
|
37
|
+
for col in 0..@header.length - 1
|
38
|
+
curr_width = 0
|
39
|
+
if !@header[ col ].nil?
|
40
|
+
curr_width = @header[ col ].length
|
41
|
+
end
|
42
|
+
max_width = @field_width_hsh[ col ]
|
43
|
+
if max_width.nil? || curr_width > max_width
|
44
|
+
@field_width_hsh[ col ] = curr_width
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def dump
|
50
|
+
#add header
|
51
|
+
table_str = add_header_to_table_str( "" )
|
52
|
+
#add table contents
|
53
|
+
table_str = add_table_contents( table_str )
|
54
|
+
return table_str.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def add_table_contents( table_str )
|
60
|
+
|
61
|
+
width = 0
|
62
|
+
@table_hsh.keys.sort{|x,y| x<=>y}.each do |row|
|
63
|
+
fields = @table_hsh[ row ]
|
64
|
+
row_str = "| "
|
65
|
+
for col in 0..@num_fields - 1
|
66
|
+
field_width = @field_width_hsh[ col ]
|
67
|
+
field_str = ""
|
68
|
+
if !fields[ col ].nil?
|
69
|
+
field_str = fields[ col ].to_s
|
70
|
+
end
|
71
|
+
pad = field_width - field_str.length
|
72
|
+
for i in 0..pad
|
73
|
+
field_str << " "
|
74
|
+
end
|
75
|
+
row_str << field_str.to_s
|
76
|
+
row_str << "| "
|
77
|
+
end
|
78
|
+
width = row_str.length
|
79
|
+
|
80
|
+
#add top separator, in case there is no header
|
81
|
+
if row == 0
|
82
|
+
table_str << get_sep_line( (row_str.length - 1) )
|
83
|
+
end
|
84
|
+
|
85
|
+
#add row separator
|
86
|
+
if @separate_each_line && row > 0 && row < @table_hsh.keys.length
|
87
|
+
table_str << get_sep_line( (row_str.length - 1) )
|
88
|
+
end
|
89
|
+
|
90
|
+
#add row
|
91
|
+
table_str << row_str.to_s
|
92
|
+
table_str << "\n"
|
93
|
+
|
94
|
+
#add header if applicable
|
95
|
+
if @header_per_line && row < @table_hsh.keys.length
|
96
|
+
table_str = add_header_to_table_str( table_str )
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
table_str << get_sep_line( width - 1 )
|
101
|
+
return table_str
|
102
|
+
end
|
103
|
+
|
104
|
+
def add_header_to_table_str( table_str )
|
105
|
+
width = 0
|
106
|
+
if !@header.nil?
|
107
|
+
row_str = "| "
|
108
|
+
for col in 0..@num_fields - 1
|
109
|
+
field_width = @field_width_hsh[ col ]
|
110
|
+
field_str = ""
|
111
|
+
if !@header[ col ].nil?
|
112
|
+
field_str = @header[ col ].to_s
|
113
|
+
end
|
114
|
+
pad = field_width - field_str.length
|
115
|
+
for i in 0..pad
|
116
|
+
field_str << " "
|
117
|
+
end
|
118
|
+
row_str << field_str.to_s
|
119
|
+
row_str << "| "
|
120
|
+
end
|
121
|
+
table_str << get_sep_line( (row_str.length - 1) )
|
122
|
+
table_str << row_str.to_s
|
123
|
+
table_str << get_sep_line( width - 1 )
|
124
|
+
width = row_str.length
|
125
|
+
end
|
126
|
+
return table_str
|
127
|
+
end
|
128
|
+
|
129
|
+
def get_sep_line( length )
|
130
|
+
s = ""
|
131
|
+
for i in 0..length - 1
|
132
|
+
s << "-"
|
133
|
+
end
|
134
|
+
s << "\n"
|
135
|
+
return s
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
data/test/test-table.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'Table'
|
3
|
+
|
4
|
+
s = ["ABY,2005-01-03,6.92",
|
5
|
+
"ABY,2005-01-04,6.75",
|
6
|
+
"ABY,2005-01-05,6.58",
|
7
|
+
"ABY,2005-01-06,6.59",
|
8
|
+
"ABY,2005-01-07,6.59",
|
9
|
+
"ABY,2005-01-09,6.59",
|
10
|
+
"ABY,2005-01-11,6.67",
|
11
|
+
"ABY,2005-01-12,6.52",
|
12
|
+
"ABY,2005-01-13,6.16",
|
13
|
+
"ABY,2005-01-14,6.19",
|
14
|
+
"ABY,2005-01-18,6.02",
|
15
|
+
"ABY,2005-01-19,6.17",
|
16
|
+
"ABY,1,6.09"]
|
17
|
+
|
18
|
+
#initialize( separate_each_line, header_per_line )
|
19
|
+
table = Table.new( true, false )
|
20
|
+
table.add_header( "symbol,date,open", "," )
|
21
|
+
s.each do |s|
|
22
|
+
table.add_row( s , "," )
|
23
|
+
end
|
24
|
+
puts table.dump
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: clean_text_table
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2008-01-10 00:00:00 -05:00
|
8
|
+
summary: Clean Text Table can transform rows of delimited text into an ASCII-table for easier viewing.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email:
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire: clean_text_table
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- John D. Gant, H. A. Goss
|
31
|
+
files:
|
32
|
+
- lib/clean_text_table
|
33
|
+
- lib/clean_text_table/table.rb
|
34
|
+
- lib/clean_text_table.rb
|
35
|
+
- test/test-table.rb
|
36
|
+
- README
|
37
|
+
test_files:
|
38
|
+
- test/test-table.rb
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions: []
|
46
|
+
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
dependencies: []
|
50
|
+
|