formatador 0.2.1 → 0.3.0
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.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +18 -0
- data/CONTRIBUTORS.md +16 -0
- data/LICENSE.md +20 -0
- data/{README.rdoc → README.md} +97 -16
- data/Rakefile +1 -7
- data/changelog.txt +52 -0
- data/formatador.gemspec +11 -6
- data/lib/formatador.rb +21 -20
- data/lib/formatador/progressbar.rb +33 -2
- data/lib/formatador/table.rb +40 -13
- data/tests/basic_tests.rb +2 -1
- data/tests/table_tests.rb +73 -4
- data/tests/tests_helper.rb +13 -0
- metadata +68 -69
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 371ea2974db0d03751e1a559433313ef4f7e7c2c46cabba41f004dba91632065
|
4
|
+
data.tar.gz: 05ee97c7103cb73f5a6ee26e135a828f2c8d738937409a933c379f66b0f2cd76
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbec7dda7093e08b69ff2372fbbff210cc7d6bde6f2d03e6ca5bf4ed03ff0e75ed7791c4a53812f7f1804c11ed801fbad3acadc71dd48a769977f42cde522ddc
|
7
|
+
data.tar.gz: 54c7902a6d39b65d929c02f9f66c490f27753c3aec04d27c3921b33f8d3a49bc9524965d52eaac0fdd14a7892f0797d38f0e5c758d6c55dd026d465517160e4b
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
## Getting Involved
|
2
|
+
|
3
|
+
New contributors are always welcome, when it doubt please ask questions. We strive to be an open and welcoming community. Please be nice to one another.
|
4
|
+
|
5
|
+
### Coding
|
6
|
+
|
7
|
+
* Pick a task:
|
8
|
+
* Offer feedback on open [pull requests](https://github.com/geemus/formatador/pulls).
|
9
|
+
* Review open [issues](https://github.com/geemus/formatador/issues) for things to help on.
|
10
|
+
* [Create an issue](https://github.com/geemus/formatador/issues/new) to start a discussion on additions or features.
|
11
|
+
* Fork the project, add your changes and tests to cover them in a topic branch.
|
12
|
+
* Commit your changes and rebase against `geemus/formatador` to ensure everything is up to date.
|
13
|
+
* [Submit a pull request](https://github.com/geemus/formatador/compare/).
|
14
|
+
|
15
|
+
### Non-Coding
|
16
|
+
|
17
|
+
* Offer feedback on open [issues](https://github.com/geemus/formatador/issues).
|
18
|
+
* Organize or volunteer at events.
|
data/CONTRIBUTORS.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
* Bohuslav Kabrda <bkabrda@redhat.com>
|
2
|
+
* Chris Howe <chris@howeville.com>
|
3
|
+
* Damien Pollet <damien.pollet@gmail.com>
|
4
|
+
* Daniel Lv <lgn21st@gmail.com>
|
5
|
+
* Gabe Martin-Dempesy <gabe@mudbugmedia.com>
|
6
|
+
* Kevin Menard <nirvdrum@gmail.com>
|
7
|
+
* Mal Curtis <mal@sitepoint.com>
|
8
|
+
* Matt Bridges <mbridges.91@gmail.com>
|
9
|
+
* Matt Petty <matt@kizmeta.com>
|
10
|
+
* Nathaniel Eliot <temujin9@t9productions.com>
|
11
|
+
* Wesley Beary <geemus@engineyard.com>
|
12
|
+
* Wesley Beary <geemus@gmail.com>
|
13
|
+
* Wesley Beary <wbeary@engineyard.com>
|
14
|
+
* Wesley Beary <wesley@heroku.com>
|
15
|
+
* geemus (Wesley Beary) <wbeary@engineyard.com>
|
16
|
+
* geemus <geemus@gmail.com>
|
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2009-2013 [CONTRIBUTORS.md](https://github.com/geemus/formatador/blob/master/CONTRIBUTORS.md)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,48 +1,129 @@
|
|
1
|
-
|
1
|
+
# formatador
|
2
2
|
|
3
3
|
STDOUT text formatting
|
4
4
|
|
5
|
-
|
5
|
+
[](http://travis-ci.org/geemus/formatador)
|
6
|
+
|
7
|
+
## Quick and dirty
|
6
8
|
|
7
9
|
You can call class methods to print out single lines like this:
|
8
10
|
|
9
|
-
|
11
|
+
```ruby
|
12
|
+
Formatador.display_line('Hello World')
|
13
|
+
```
|
10
14
|
|
11
15
|
You use tags, similar to html, to set formatting options:
|
12
16
|
|
13
|
-
|
17
|
+
```ruby
|
18
|
+
Formatador.display_line('[green]Hello World[/]')
|
19
|
+
```
|
14
20
|
|
15
|
-
|
21
|
+
`[/]` resets everything to normal, colors are supported and `[_color_]` sets the background color.
|
16
22
|
|
17
|
-
|
23
|
+
## Standard options
|
18
24
|
|
19
25
|
* format - and adds color codes if STDOUT.tty? is true
|
20
26
|
* display - calls format on the input and prints it
|
21
27
|
* display_line - calls display, but adds on a newline (\n)
|
22
28
|
* redisplay - Displays text, prepended with \r which will overwrite the last existing line
|
23
29
|
|
24
|
-
|
30
|
+
## Extensions
|
25
31
|
|
26
32
|
* display_table: takes an array of hashes. Each hash is a row, with the keys being the headers and values being the data. An optional second argument can specify which headers/columns to include and in what order they should appear.
|
27
33
|
* display_compact_table: Same as display_table, execpt that split lines are not drawn by default in the body of the table. If you need a split line, put a :split constant in the body array.
|
28
34
|
* redisplay_progressbar: takes the current and total values as its first two arguments and redisplays a progressbar (until current = total and then it display_lines). An optional third argument represents the start time and will add an elapsed time counter.
|
29
35
|
|
30
|
-
|
36
|
+
### Progress Bar examples
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
total = 1000
|
40
|
+
progress = Formatador::ProgressBar.new(total)
|
41
|
+
|
42
|
+
1000.times do
|
43
|
+
progress.increment
|
44
|
+
end
|
45
|
+
|
46
|
+
#=> 978/1000 |************************************************* |
|
47
|
+
|
48
|
+
# Change the color of the bar
|
49
|
+
total = 1000
|
50
|
+
progress = Formatador::ProgressBar.new(total, :color => "light_blue")
|
51
|
+
|
52
|
+
1000.times do
|
53
|
+
progress.increment
|
54
|
+
end
|
55
|
+
|
56
|
+
# Change the color of a completed progress bar
|
57
|
+
total = 1000
|
58
|
+
progress = Formatador::ProgressBar.new(total) { |b| b.opts[:color] = "green" }
|
59
|
+
|
60
|
+
1000.times do
|
61
|
+
progress.increment
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### Table examples
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
table_data = [
|
69
|
+
{ :name => "Joe", :food => "Burger" },
|
70
|
+
{ :name => "Bill", :food => "French fries" }
|
71
|
+
]
|
72
|
+
Formatador.display_table(table_data)
|
73
|
+
|
74
|
+
#=> +------+--------------+
|
75
|
+
# | name | food |
|
76
|
+
# +------+--------------+
|
77
|
+
# | Joe | Burger |
|
78
|
+
# +------+--------------+
|
79
|
+
# | Bill | French fries |
|
80
|
+
# +------+--------------+
|
81
|
+
|
82
|
+
table_data = [
|
83
|
+
{
|
84
|
+
:name => "Joe",
|
85
|
+
:meal => {
|
86
|
+
:main_dish => "Burger",
|
87
|
+
:drink => "water"
|
88
|
+
}
|
89
|
+
},
|
90
|
+
{
|
91
|
+
:name => "Bill",
|
92
|
+
:meal => {
|
93
|
+
:main_dish => "Chicken",
|
94
|
+
:drink => "soda"
|
95
|
+
}
|
96
|
+
}
|
97
|
+
]
|
98
|
+
Formatador.display_table(table_data, [:name, :"meal.drink"])
|
99
|
+
|
100
|
+
#=> +------+------------+
|
101
|
+
# | name | meal.drink |
|
102
|
+
# +------+------------+
|
103
|
+
# | Joe | water |
|
104
|
+
# +------+------------+
|
105
|
+
# | Bill | soda |
|
106
|
+
# +------+------------+
|
107
|
+
```
|
108
|
+
|
109
|
+
## Indentation
|
31
110
|
|
32
111
|
By initializing a formatador object you can keep track of indentation:
|
33
112
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
113
|
+
```ruby
|
114
|
+
formatador = Formatador.new
|
115
|
+
formatador.display_line('one level of indentation')
|
116
|
+
formatador.indent {
|
117
|
+
formatador.display_line('two levels of indentation')
|
118
|
+
}
|
119
|
+
formatador.display_line('one level of indentation')
|
120
|
+
```
|
40
121
|
|
41
|
-
|
122
|
+
## Copyright
|
42
123
|
|
43
124
|
(The MIT License)
|
44
125
|
|
45
|
-
Copyright (c)
|
126
|
+
Copyright (c) 2015 [geemus (Wesley Beary)](http://github.com/geemus)
|
46
127
|
|
47
128
|
Permission is hereby granted, free of charge, to any person obtaining
|
48
129
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -21,10 +21,6 @@ def date
|
|
21
21
|
Date.today.to_s
|
22
22
|
end
|
23
23
|
|
24
|
-
def rubyforge_project
|
25
|
-
name
|
26
|
-
end
|
27
|
-
|
28
24
|
def gemspec_file
|
29
25
|
"#{name}.gemspec"
|
30
26
|
end
|
@@ -56,7 +52,7 @@ task :coverage do
|
|
56
52
|
sh "open coverage/index.html"
|
57
53
|
end
|
58
54
|
|
59
|
-
require '
|
55
|
+
require 'rdoc/task'
|
60
56
|
Rake::RDocTask.new do |rdoc|
|
61
57
|
rdoc.rdoc_dir = 'rdoc'
|
62
58
|
rdoc.title = "#{name} #{version}"
|
@@ -111,8 +107,6 @@ task :gemspec => :validate do
|
|
111
107
|
replace_header(head, :name)
|
112
108
|
replace_header(head, :version)
|
113
109
|
replace_header(head, :date)
|
114
|
-
#comment this out if your rubyforge_project has a different name
|
115
|
-
replace_header(head, :rubyforge_project)
|
116
110
|
|
117
111
|
# determine file list from git ls-files
|
118
112
|
files = `git ls-files`.
|
data/changelog.txt
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
v0.3.0 06/17/21
|
2
|
+
===============
|
3
|
+
|
4
|
+
add travis badge to readme
|
5
|
+
change readme to md
|
6
|
+
fix markdown readme
|
7
|
+
fix travis badge
|
8
|
+
monkey-patch StringIO to try and fix jruby build
|
9
|
+
override tty? for jruby build
|
10
|
+
fix copyright statement
|
11
|
+
use unicode.width instead of string.length
|
12
|
+
remove unicode from requirements, use only if loaded
|
13
|
+
display datum who's value is a FalseClass
|
14
|
+
more colors with syntax-specific code/length limitations
|
15
|
+
better code visualization in readme
|
16
|
+
update readme
|
17
|
+
remove broken rubinius build from CI
|
18
|
+
fix length method to detect multibyte char width
|
19
|
+
fix test for table with multi byte chars
|
20
|
+
support multibyte characters
|
21
|
+
update CI to 2.2.7, 2.3.4, and 2.4.1
|
22
|
+
drop rubyforge_project from gemspec and rakefile
|
23
|
+
change github reference to https
|
24
|
+
change readme.rdoc to readme.md in gemspec
|
25
|
+
|
26
|
+
v0.2.5 05/23/14
|
27
|
+
===============
|
28
|
+
|
29
|
+
* fix typo in readme
|
30
|
+
* ensure indent is reset
|
31
|
+
* add progress bar object
|
32
|
+
* improve thread safety for progress bar
|
33
|
+
|
34
|
+
v0.2.4 10/26/12
|
35
|
+
===============
|
36
|
+
|
37
|
+
* sort background colors higher
|
38
|
+
* fix homepage link in gem metadata
|
39
|
+
|
40
|
+
v0.2.3 05/18/12
|
41
|
+
===============
|
42
|
+
|
43
|
+
* fix nested hashes to allow for keys which contain periods
|
44
|
+
|
45
|
+
v0.2.2 05/16/12
|
46
|
+
===============
|
47
|
+
|
48
|
+
* require stringio in tests
|
49
|
+
* allow tests to pass without tty
|
50
|
+
* label test groups
|
51
|
+
* fix for redisplay width
|
52
|
+
* allow tables to use nested hashes
|
data/formatador.gemspec
CHANGED
@@ -13,9 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'formatador'
|
16
|
-
s.version = '0.
|
17
|
-
s.date = '
|
18
|
-
s.rubyforge_project = 'formatador'
|
16
|
+
s.version = '0.3.0'
|
17
|
+
s.date = '2021-06-17'
|
19
18
|
|
20
19
|
## Make sure your summary is short. The description may be as long
|
21
20
|
## as you like.
|
@@ -27,7 +26,8 @@ Gem::Specification.new do |s|
|
|
27
26
|
## a custom homepage, consider using your GitHub URL or the like.
|
28
27
|
s.authors = ["geemus (Wesley Beary)"]
|
29
28
|
s.email = 'geemus@gmail.com'
|
30
|
-
s.homepage =
|
29
|
+
s.homepage = "https://github.com/geemus/#{s.name}"
|
30
|
+
s.license = 'MIT'
|
31
31
|
|
32
32
|
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
33
33
|
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
@@ -44,7 +44,7 @@ Gem::Specification.new do |s|
|
|
44
44
|
## Specify any RDoc options here. You'll want to add your README and
|
45
45
|
## LICENSE files to the extra_rdoc_files list.
|
46
46
|
s.rdoc_options = ["--charset=UTF-8"]
|
47
|
-
s.extra_rdoc_files = %w[README.
|
47
|
+
s.extra_rdoc_files = %w[README.md]
|
48
48
|
|
49
49
|
## List your runtime dependencies here. Runtime dependencies are those
|
50
50
|
## that are needed for an end user to actually USE your code.
|
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
|
|
53
53
|
## List your development dependencies here. Development dependencies are
|
54
54
|
## those that are only needed during development
|
55
55
|
s.add_development_dependency('rake')
|
56
|
+
s.add_development_dependency('rdoc')
|
56
57
|
s.add_development_dependency('shindo')
|
57
58
|
|
58
59
|
## Leave this section as-is. It will be automatically generated from the
|
@@ -60,9 +61,13 @@ Gem::Specification.new do |s|
|
|
60
61
|
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
61
62
|
# = MANIFEST =
|
62
63
|
s.files = %w[
|
64
|
+
CONTRIBUTING.md
|
65
|
+
CONTRIBUTORS.md
|
63
66
|
Gemfile
|
64
|
-
|
67
|
+
LICENSE.md
|
68
|
+
README.md
|
65
69
|
Rakefile
|
70
|
+
changelog.txt
|
66
71
|
formatador.gemspec
|
67
72
|
lib/formatador.rb
|
68
73
|
lib/formatador/progressbar.rb
|
data/lib/formatador.rb
CHANGED
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'formatador', 'progressbar')
|
|
3
3
|
|
4
4
|
class Formatador
|
5
5
|
|
6
|
-
VERSION = '0.
|
6
|
+
VERSION = '0.3.0'
|
7
7
|
|
8
8
|
STYLES = {
|
9
9
|
:"\/" => "0",
|
@@ -17,15 +17,6 @@ class Formatador
|
|
17
17
|
:underline_none => "24",
|
18
18
|
:blink_off => "25",
|
19
19
|
:positive => "27", # revert color/color
|
20
|
-
:black => "30",
|
21
|
-
:red => "31",
|
22
|
-
:green => "32",
|
23
|
-
:yellow => "33",
|
24
|
-
:blue => "34",
|
25
|
-
:magenta => "35",
|
26
|
-
:purple => "35",
|
27
|
-
:cyan => "36",
|
28
|
-
:white => "37",
|
29
20
|
:_black_ => "40",
|
30
21
|
:_red_ => "41",
|
31
22
|
:_green_ => "42",
|
@@ -35,14 +26,6 @@ class Formatador
|
|
35
26
|
:_purple_ => "45",
|
36
27
|
:_cyan_ => "46",
|
37
28
|
:_white_ => "47",
|
38
|
-
:light_black => "90",
|
39
|
-
:light_red => "91",
|
40
|
-
:light_green => "92",
|
41
|
-
:light_yellow => "93",
|
42
|
-
:light_blue => "94",
|
43
|
-
:light_magenta => "95",
|
44
|
-
:light_purple => "95",
|
45
|
-
:light_cyan => "96",
|
46
29
|
:_light_black_ => "100",
|
47
30
|
:_light_red_ => "101",
|
48
31
|
:_light_green_ => "102",
|
@@ -51,6 +34,23 @@ class Formatador
|
|
51
34
|
:_light_magenta_ => "105",
|
52
35
|
:_light_purple_ => "105",
|
53
36
|
:_light_cyan_ => "106",
|
37
|
+
:black => "30",
|
38
|
+
:red => "31",
|
39
|
+
:green => "32",
|
40
|
+
:yellow => "33",
|
41
|
+
:blue => "34",
|
42
|
+
:magenta => "35",
|
43
|
+
:purple => "35",
|
44
|
+
:cyan => "36",
|
45
|
+
:white => "37",
|
46
|
+
:light_black => "90",
|
47
|
+
:light_red => "91",
|
48
|
+
:light_green => "92",
|
49
|
+
:light_yellow => "93",
|
50
|
+
:light_blue => "94",
|
51
|
+
:light_magenta => "95",
|
52
|
+
:light_purple => "95",
|
53
|
+
:light_cyan => "96",
|
54
54
|
}
|
55
55
|
|
56
56
|
PARSE_REGEX = /\[(#{ STYLES.keys.join('|') })\]/ix
|
@@ -62,7 +62,7 @@ class Formatador
|
|
62
62
|
|
63
63
|
def display(string = '')
|
64
64
|
print(parse("[indent]#{string}"))
|
65
|
-
|
65
|
+
$stdout.flush
|
66
66
|
nil
|
67
67
|
end
|
68
68
|
|
@@ -80,7 +80,7 @@ class Formatador
|
|
80
80
|
end
|
81
81
|
|
82
82
|
def parse(string)
|
83
|
-
if
|
83
|
+
if $stdout.tty?
|
84
84
|
string.gsub(PARSE_REGEX) { "\e[#{STYLES[$1.to_sym]}m" }.gsub(INDENT_REGEX) { indentation }
|
85
85
|
else
|
86
86
|
strip(string)
|
@@ -90,6 +90,7 @@ class Formatador
|
|
90
90
|
def indent(&block)
|
91
91
|
@indent += 1
|
92
92
|
yield
|
93
|
+
ensure
|
93
94
|
@indent -= 1
|
94
95
|
end
|
95
96
|
|
@@ -1,12 +1,43 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
1
3
|
class Formatador
|
2
4
|
|
5
|
+
class ProgressBar
|
6
|
+
|
7
|
+
attr_accessor :current, :total, :opts
|
8
|
+
|
9
|
+
def initialize(total, opts = {}, &block)
|
10
|
+
@current = opts.delete(:start) || 0
|
11
|
+
@total = total.to_i
|
12
|
+
@opts = opts
|
13
|
+
@lock = Mutex.new
|
14
|
+
@complete_proc = block_given? ? block : Proc.new { }
|
15
|
+
end
|
16
|
+
|
17
|
+
def increment(increment = 1)
|
18
|
+
@lock.synchronize do
|
19
|
+
return if complete?
|
20
|
+
@current += increment.to_i
|
21
|
+
@complete_proc.call(self) if complete?
|
22
|
+
Formatador.redisplay_progressbar(current, total, opts)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def complete?
|
29
|
+
current == total
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
3
34
|
def redisplay_progressbar(current, total, options = {})
|
4
35
|
options = { :color => 'white', :width => 50, :new_line => true }.merge!(options)
|
5
36
|
data = progressbar(current, total, options)
|
6
37
|
if current < total
|
7
|
-
redisplay(data)
|
38
|
+
redisplay(data, options[:width])
|
8
39
|
else
|
9
|
-
redisplay("#{data}")
|
40
|
+
redisplay("#{data}", options[:width])
|
10
41
|
if options[:new_line]
|
11
42
|
new_line
|
12
43
|
end
|
data/lib/formatador/table.rb
CHANGED
@@ -10,54 +10,61 @@ class Formatador
|
|
10
10
|
def display_compact_table(hashes, keys = nil, &block)
|
11
11
|
headers = keys || []
|
12
12
|
widths = {}
|
13
|
+
|
14
|
+
# Calculate Widths
|
13
15
|
if hashes.empty? && keys
|
14
|
-
|
16
|
+
keys.each do |key|
|
15
17
|
widths[key] = key.to_s.length
|
16
18
|
end
|
17
19
|
else
|
18
|
-
|
20
|
+
hashes.each do |hash|
|
19
21
|
next unless hash.respond_to?(:keys)
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
(headers + hash.keys).each do |key|
|
24
|
+
if !keys
|
23
25
|
headers << key
|
24
26
|
end
|
25
|
-
widths[key] = [ length(key), widths[key] || 0,
|
27
|
+
widths[key] = [ length(key), widths[key] || 0, length(calculate_datum(key, hash)) || 0].max
|
26
28
|
end
|
27
29
|
headers = headers.uniq
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
33
|
+
# Determine order of headers
|
31
34
|
if block_given?
|
32
35
|
headers = headers.sort(&block)
|
33
36
|
elsif !keys
|
34
37
|
headers = headers.sort {|x,y| x.to_s <=> y.to_s}
|
35
38
|
end
|
36
39
|
|
40
|
+
# Display separator row
|
37
41
|
split = "+"
|
38
42
|
if headers.empty?
|
39
43
|
split << '--+'
|
40
44
|
else
|
41
|
-
|
45
|
+
headers.each do |header|
|
42
46
|
widths[header] ||= length(header)
|
43
47
|
split << ('-' * (widths[header] + 2)) << '+'
|
44
48
|
end
|
45
49
|
end
|
46
|
-
|
47
50
|
display_line(split)
|
51
|
+
|
52
|
+
# Display data row
|
48
53
|
columns = []
|
49
|
-
|
54
|
+
headers.each do |header|
|
50
55
|
columns << "[bold]#{header}[/]#{' ' * (widths[header] - header.to_s.length)}"
|
51
56
|
end
|
52
57
|
display_line("| #{columns.join(' | ')} |")
|
53
58
|
display_line(split)
|
54
59
|
|
55
|
-
|
60
|
+
hashes.each do |hash|
|
56
61
|
if hash.respond_to? :keys
|
57
62
|
columns = []
|
58
|
-
|
59
|
-
datum =
|
60
|
-
|
63
|
+
headers.each do |header|
|
64
|
+
datum = calculate_datum(header, hash)
|
65
|
+
width = widths[header] - length(datum)
|
66
|
+
width = width < 0 ? 0 : width
|
67
|
+
columns << "#{datum}#{' ' * width}"
|
61
68
|
end
|
62
69
|
display_line("| #{columns.join(' | ')} |")
|
63
70
|
else
|
@@ -73,6 +80,26 @@ class Formatador
|
|
73
80
|
private
|
74
81
|
|
75
82
|
def length(value)
|
76
|
-
|
83
|
+
if Module.const_defined?(:Unicode)
|
84
|
+
Unicode.width(value.to_s.gsub(PARSE_REGEX, ''))
|
85
|
+
else
|
86
|
+
value.to_s.gsub(PARSE_REGEX, '').chars.reduce(0) { |sum, char| sum += char.bytesize > 1 ? 2 : 1 }
|
87
|
+
end
|
88
|
+
|
89
|
+
rescue NotImplementedError
|
90
|
+
value.to_s.gsub(PARSE_REGEX, '').chars.reduce(0) { |sum, char| sum += char.bytesize > 1 ? 2 : 1 }
|
91
|
+
end
|
92
|
+
|
93
|
+
def calculate_datum(header, hash)
|
94
|
+
if !hash.keys.include?(header) && (splits = header.to_s.split('.')).length > 1
|
95
|
+
datum = nil
|
96
|
+
splits.each do |split|
|
97
|
+
d = (datum||hash)
|
98
|
+
datum = d[split] || d[split.to_sym] || ''
|
99
|
+
end
|
100
|
+
else
|
101
|
+
datum = hash.fetch(header, '')
|
102
|
+
end
|
103
|
+
datum
|
77
104
|
end
|
78
105
|
end
|
data/tests/basic_tests.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Shindo.tests("Formatador") do
|
1
|
+
Shindo.tests("Formatador: basics") do
|
2
2
|
|
3
3
|
tests("#display_line(Formatador)").returns(" Formatador\n") do
|
4
4
|
capture_stdout do
|
@@ -10,6 +10,7 @@ output = <<-OUTPUT
|
|
10
10
|
one
|
11
11
|
two
|
12
12
|
OUTPUT
|
13
|
+
output = Formatador.parse(output)
|
13
14
|
|
14
15
|
tests("#display_lines(['one', 'two']").returns(output) do
|
15
16
|
capture_stdout do
|
data/tests/table_tests.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
Shindo.tests("Formatador: tables") do
|
2
3
|
|
3
4
|
output = <<-OUTPUT
|
4
5
|
+---+
|
5
|
-
|
|
6
|
+
| [bold]a[/] |
|
6
7
|
+---+
|
7
8
|
| 1 |
|
8
9
|
+---+
|
9
10
|
| 2 |
|
10
11
|
+---+
|
11
12
|
OUTPUT
|
13
|
+
output = Formatador.parse(output)
|
12
14
|
|
13
15
|
tests("#display_table([{:a => 1}, {:a => 2}])").returns(output) do
|
14
16
|
capture_stdout do
|
@@ -18,10 +20,11 @@ OUTPUT
|
|
18
20
|
|
19
21
|
output = <<-OUTPUT
|
20
22
|
+--------+
|
21
|
-
|
|
23
|
+
| [bold]header[/] |
|
22
24
|
+--------+
|
23
25
|
+--------+
|
24
26
|
OUTPUT
|
27
|
+
output = Formatador.parse(output)
|
25
28
|
|
26
29
|
tests("#display_table([], [:header])").returns(output) do
|
27
30
|
capture_stdout do
|
@@ -31,11 +34,12 @@ OUTPUT
|
|
31
34
|
|
32
35
|
output = <<-OUTPUT
|
33
36
|
+--------+
|
34
|
-
|
|
37
|
+
| [bold]header[/] |
|
35
38
|
+--------+
|
36
39
|
| |
|
37
40
|
+--------+
|
38
41
|
OUTPUT
|
42
|
+
output = Formatador.parse(output)
|
39
43
|
|
40
44
|
tests("#display_table([{:a => 1}], [:header])").returns(output) do
|
41
45
|
capture_stdout do
|
@@ -43,4 +47,69 @@ OUTPUT
|
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
50
|
+
|
51
|
+
|
52
|
+
output = <<-OUTPUT
|
53
|
+
+---+------------+
|
54
|
+
| [bold]a[/] | [bold]nested.key[/] |
|
55
|
+
+---+------------+
|
56
|
+
| 1 | value |
|
57
|
+
+---+------------+
|
58
|
+
OUTPUT
|
59
|
+
output = Formatador.parse(output)
|
60
|
+
|
61
|
+
tests("#display_table([{:a => 1, :nested => {:key => 'value'}}], [:header, :'nested.key'])").returns(output) do
|
62
|
+
capture_stdout do
|
63
|
+
Formatador.display_table([{:a => 1, :nested => {:key => 'value'}}], [:a, :'nested.key'])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
output = <<-OUTPUT
|
68
|
+
+---+-----------------+
|
69
|
+
| [bold]a[/] | [bold]nested[/] |
|
70
|
+
+---+-----------------+
|
71
|
+
| 1 | {:key=>"value"} |
|
72
|
+
+---+-----------------+
|
73
|
+
OUTPUT
|
74
|
+
output = Formatador.parse(output)
|
75
|
+
|
76
|
+
tests("#display_table([{:a => 1, :nested => {:key => 'value'}}])").returns(output) do
|
77
|
+
capture_stdout do
|
78
|
+
Formatador.display_table([{:a => 1, :nested => {:key => 'value'}}])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
output = <<-OUTPUT
|
83
|
+
+---+--------------+
|
84
|
+
| [bold]a[/] | [bold]just.pointed[/] |
|
85
|
+
+---+--------------+
|
86
|
+
| 1 | value |
|
87
|
+
+---+--------------+
|
88
|
+
OUTPUT
|
89
|
+
output = Formatador.parse(output)
|
90
|
+
|
91
|
+
tests("#display_table([{:a => 1, 'just.pointed' => :value}])").returns(output) do
|
92
|
+
capture_stdout do
|
93
|
+
Formatador.display_table([{:a => 1, 'just.pointed' => :value}])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
output = <<-OUTPUT
|
99
|
+
+------+
|
100
|
+
| [bold]a[/] |
|
101
|
+
+------+
|
102
|
+
| 1 |
|
103
|
+
+------+
|
104
|
+
| 震度 |
|
105
|
+
+------+
|
106
|
+
OUTPUT
|
107
|
+
output = Formatador.parse(output)
|
108
|
+
|
109
|
+
tests("#display_table([{:a => 1}, {:a => 2}])").returns(output) do
|
110
|
+
capture_stdout do
|
111
|
+
Formatador.display_table([{:a => 1}, {:a => "震度"}])
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
46
115
|
end
|
data/tests/tests_helper.rb
CHANGED
@@ -3,6 +3,19 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
3
3
|
require 'formatador'
|
4
4
|
require 'rubygems'
|
5
5
|
require 'shindo'
|
6
|
+
require 'stringio'
|
7
|
+
|
8
|
+
class IO
|
9
|
+
def tty?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class StringIO
|
15
|
+
def tty?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
6
19
|
|
7
20
|
def capture_stdout
|
8
21
|
old_stdout = $stdout
|
metadata
CHANGED
@@ -1,62 +1,71 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: formatador
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 1
|
10
|
-
version: 0.2.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- geemus (Wesley Beary)
|
14
|
-
autorequire:
|
8
|
+
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2021-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
22
21
|
prerelease: false
|
23
|
-
|
24
|
-
|
25
|
-
requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
26
24
|
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
32
34
|
type: :development
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: shindo
|
36
35
|
prerelease: false
|
37
|
-
|
38
|
-
|
39
|
-
requirements:
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
40
38
|
- - ">="
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: shindo
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
46
48
|
type: :development
|
47
|
-
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
48
55
|
description: STDOUT text formatting
|
49
56
|
email: geemus@gmail.com
|
50
57
|
executables: []
|
51
|
-
|
52
58
|
extensions: []
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README.md
|
61
|
+
files:
|
62
|
+
- CONTRIBUTING.md
|
63
|
+
- CONTRIBUTORS.md
|
57
64
|
- Gemfile
|
58
|
-
-
|
65
|
+
- LICENSE.md
|
66
|
+
- README.md
|
59
67
|
- Rakefile
|
68
|
+
- changelog.txt
|
60
69
|
- formatador.gemspec
|
61
70
|
- lib/formatador.rb
|
62
71
|
- lib/formatador/progressbar.rb
|
@@ -64,38 +73,28 @@ files:
|
|
64
73
|
- tests/basic_tests.rb
|
65
74
|
- tests/table_tests.rb
|
66
75
|
- tests/tests_helper.rb
|
67
|
-
homepage:
|
68
|
-
licenses:
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
76
|
+
homepage: https://github.com/geemus/formatador
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options:
|
82
|
+
- "--charset=UTF-8"
|
83
|
+
require_paths:
|
74
84
|
- lib
|
75
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
-
|
77
|
-
requirements:
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
78
87
|
- - ">="
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
version: "0"
|
84
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
|
-
requirements:
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
87
92
|
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
segments:
|
91
|
-
- 0
|
92
|
-
version: "0"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
93
95
|
requirements: []
|
94
|
-
|
95
|
-
|
96
|
-
rubygems_version: 1.8.5
|
97
|
-
signing_key:
|
96
|
+
rubygems_version: 3.2.15
|
97
|
+
signing_key:
|
98
98
|
specification_version: 2
|
99
99
|
summary: Ruby STDOUT text formatting
|
100
100
|
test_files: []
|
101
|
-
|