formatador 0.2.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +18 -0
- data/CONTRIBUTORS.md +16 -0
- data/Gemfile +1 -1
- data/LICENSE.md +20 -0
- data/{README.rdoc → README.md} +95 -41
- data/Rakefile +2 -8
- data/changelog.txt +44 -0
- data/formatador.gemspec +10 -10
- data/lib/formatador/progressbar.rb +31 -0
- data/lib/formatador/table.rb +15 -8
- data/lib/formatador.rb +14 -4
- data/tests/table_tests.rb +50 -1
- data/tests/tests_helper.rb +12 -0
- metadata +36 -28
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c7691580892da9215dfc4bbe6523203fef95cd0c57a25652af464d208e1a7a64
|
4
|
+
data.tar.gz: 8e99624363cc7a8b76d2cc699664618d184ffdffe16ca4d4b3ce73927aa3e5d0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 240f470bb912d51efd07d5916ba31ca06694db82058146ad1d5403959966bc8c4c7c409b5d53958ac6232a87d46ff299e9640a7b5b4333bbff527fdf0055f604
|
7
|
+
data.tar.gz: 10800e56b00b910904da64dc8a8b64d01e717ffcc91ec19d4ac4e3c4e94ce903921722707ca16cc675c082bff888e170b1324da5426f7f8e7c1668de64448b37
|
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/Gemfile
CHANGED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2009-2022 [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,75 +1,129 @@
|
|
1
|
-
|
1
|
+
# formatador
|
2
2
|
|
3
3
|
STDOUT text formatting
|
4
4
|
|
5
|
-
|
5
|
+
[![Build Status](https://github.com/geemus/formatador/actions/workflows/ruby.yml/badge.svg)](https://github.com/geemus/formatador/actions/workflows/ruby.yml)
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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"])
|
48
99
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
100
|
+
#=> +------+------------+
|
101
|
+
# | name | meal.drink |
|
102
|
+
# +------+------------+
|
103
|
+
# | Joe | water |
|
104
|
+
# +------+------------+
|
105
|
+
# | Bill | soda |
|
106
|
+
# +------+------------+
|
107
|
+
```
|
56
108
|
|
57
|
-
|
109
|
+
## Indentation
|
58
110
|
|
59
111
|
By initializing a formatador object you can keep track of indentation:
|
60
112
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
+
```
|
67
121
|
|
68
|
-
|
122
|
+
## Copyright
|
69
123
|
|
70
124
|
(The MIT License)
|
71
125
|
|
72
|
-
Copyright (c)
|
126
|
+
Copyright (c) 2022 [geemus (Wesley Beary)](http://github.com/geemus)
|
73
127
|
|
74
128
|
Permission is hereby granted, free of charge, to any person obtaining
|
75
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}"
|
@@ -88,7 +84,7 @@ task :release => :build do
|
|
88
84
|
puts "You must be on the master branch to release!"
|
89
85
|
exit!
|
90
86
|
end
|
91
|
-
sh "
|
87
|
+
sh "gem install pkg/#{name}-#{version}.gem"
|
92
88
|
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
93
89
|
sh "git tag v#{version}"
|
94
90
|
sh "git push origin master"
|
@@ -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
CHANGED
@@ -1,3 +1,47 @@
|
|
1
|
+
v1.0.0 01/20/21
|
2
|
+
===============
|
3
|
+
|
4
|
+
release v1, it's been stable for ages
|
5
|
+
|
6
|
+
v0.3.0 06/17/21
|
7
|
+
===============
|
8
|
+
|
9
|
+
add travis badge to readme
|
10
|
+
change readme to md
|
11
|
+
fix markdown readme
|
12
|
+
fix travis badge
|
13
|
+
monkey-patch StringIO to try and fix jruby build
|
14
|
+
override tty? for jruby build
|
15
|
+
fix copyright statement
|
16
|
+
use unicode.width instead of string.length
|
17
|
+
remove unicode from requirements, use only if loaded
|
18
|
+
display datum who's value is a FalseClass
|
19
|
+
more colors with syntax-specific code/length limitations
|
20
|
+
better code visualization in readme
|
21
|
+
update readme
|
22
|
+
remove broken rubinius build from CI
|
23
|
+
fix length method to detect multibyte char width
|
24
|
+
fix test for table with multi byte chars
|
25
|
+
support multibyte characters
|
26
|
+
update CI to 2.2.7, 2.3.4, and 2.4.1
|
27
|
+
drop rubyforge_project from gemspec and rakefile
|
28
|
+
change github reference to https
|
29
|
+
change readme.rdoc to readme.md in gemspec
|
30
|
+
|
31
|
+
v0.2.5 05/23/14
|
32
|
+
===============
|
33
|
+
|
34
|
+
* fix typo in readme
|
35
|
+
* ensure indent is reset
|
36
|
+
* add progress bar object
|
37
|
+
* improve thread safety for progress bar
|
38
|
+
|
39
|
+
v0.2.4 10/26/12
|
40
|
+
===============
|
41
|
+
|
42
|
+
* sort background colors higher
|
43
|
+
* fix homepage link in gem metadata
|
44
|
+
|
1
45
|
v0.2.3 05/18/12
|
2
46
|
===============
|
3
47
|
|
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 = '
|
17
|
-
s.date = '
|
18
|
-
s.rubyforge_project = 'formatador'
|
16
|
+
s.version = '1.1.0'
|
17
|
+
s.date = '2022-01-24'
|
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,8 +61,11 @@ 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
|
66
70
|
changelog.txt
|
67
71
|
formatador.gemspec
|
@@ -73,8 +77,4 @@ Gem::Specification.new do |s|
|
|
73
77
|
tests/tests_helper.rb
|
74
78
|
]
|
75
79
|
# = MANIFEST =
|
76
|
-
|
77
|
-
## Test files will be grabbed from the file list. Make sure the path glob
|
78
|
-
## matches what you actually use.
|
79
|
-
s.test_files = s.files.select { |path| path =~ /^[spec|tests]\/.*_[spec|tests]\.rb/ }
|
80
80
|
end
|
@@ -1,5 +1,36 @@
|
|
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)
|
data/lib/formatador/table.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
class Formatador
|
2
|
-
def display_table(hashes, keys = nil, &block)
|
2
|
+
def display_table(hashes, keys = nil, **options, &block)
|
3
3
|
new_hashes = hashes.inject([]) do |accum,item|
|
4
4
|
accum << :split unless accum.empty?
|
5
5
|
accum << item
|
6
6
|
end
|
7
|
-
display_compact_table(new_hashes, keys, &block)
|
7
|
+
display_compact_table(new_hashes, keys, **options, &block)
|
8
8
|
end
|
9
9
|
|
10
|
-
def display_compact_table(hashes, keys = nil, &block)
|
10
|
+
def display_compact_table(hashes, keys = nil, **options, &block)
|
11
11
|
headers = keys || []
|
12
12
|
widths = {}
|
13
13
|
|
@@ -59,12 +59,12 @@ class Formatador
|
|
59
59
|
|
60
60
|
hashes.each do |hash|
|
61
61
|
if hash.respond_to? :keys
|
62
|
-
columns =
|
63
|
-
headers.each do |header|
|
62
|
+
columns = headers.map do |header|
|
64
63
|
datum = calculate_datum(header, hash)
|
65
64
|
width = widths[header] - length(datum)
|
66
65
|
width = width < 0 ? 0 : width
|
67
|
-
|
66
|
+
|
67
|
+
datum.is_a?(Numeric) && options[:numeric_rjust] ? "#{' ' * width}#{datum}" : "#{datum}#{' ' * width}"
|
68
68
|
end
|
69
69
|
display_line("| #{columns.join(' | ')} |")
|
70
70
|
else
|
@@ -80,7 +80,14 @@ class Formatador
|
|
80
80
|
private
|
81
81
|
|
82
82
|
def length(value)
|
83
|
-
|
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 }
|
84
91
|
end
|
85
92
|
|
86
93
|
def calculate_datum(header, hash)
|
@@ -91,7 +98,7 @@ class Formatador
|
|
91
98
|
datum = d[split] || d[split.to_sym] || ''
|
92
99
|
end
|
93
100
|
else
|
94
|
-
datum = hash
|
101
|
+
datum = hash.fetch(header, '')
|
95
102
|
end
|
96
103
|
datum
|
97
104
|
end
|
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 = '
|
6
|
+
VERSION = '1.1.0'
|
7
7
|
|
8
8
|
STYLES = {
|
9
9
|
:"\/" => "0",
|
@@ -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
|
|
@@ -118,7 +119,7 @@ class Formatador
|
|
118
119
|
string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation }
|
119
120
|
end
|
120
121
|
|
121
|
-
%w{display display_line display_lines
|
122
|
+
%w{display display_line display_lines indent parse redisplay redisplay_line new_line redisplay_progressbar}.each do |method|
|
122
123
|
eval <<-DEF
|
123
124
|
def self.#{method}(*args, &block)
|
124
125
|
Thread.current[:formatador] ||= new
|
@@ -127,4 +128,13 @@ class Formatador
|
|
127
128
|
DEF
|
128
129
|
end
|
129
130
|
|
131
|
+
%w{display_table display_compact_table}.each do |method|
|
132
|
+
eval <<-DEF
|
133
|
+
def self.#{method}(*args, **kwargs, &block)
|
134
|
+
Thread.current[:formatador] ||= new
|
135
|
+
Thread.current[:formatador].#{method}(*args, **kwargs, &block)
|
136
|
+
end
|
137
|
+
DEF
|
138
|
+
end
|
139
|
+
|
130
140
|
end
|
data/tests/table_tests.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
Shindo.tests("Formatador: tables") do
|
2
3
|
|
3
4
|
output = <<-OUTPUT
|
@@ -93,4 +94,52 @@ output = Formatador.parse(output)
|
|
93
94
|
end
|
94
95
|
end
|
95
96
|
|
96
|
-
|
97
|
+
output = <<-OUTPUT
|
98
|
+
+-------------------------+----------------+
|
99
|
+
| [bold]right-justify a numeric[/] | [bold]standard value[/] |
|
100
|
+
+-------------------------+----------------+
|
101
|
+
| 12345 | value |
|
102
|
+
+-------------------------+----------------+
|
103
|
+
OUTPUT
|
104
|
+
output = Formatador.parse(output)
|
105
|
+
|
106
|
+
tests("#display_table([{'right-justify a numeric' => 12345, 'standard value' => 'standard value'}], numeric_rjust: true)").returns(output) do
|
107
|
+
capture_stdout do
|
108
|
+
Formatador.display_table([{'right-justify a numeric' => 12345, 'standard value' => 'value'}], numeric_rjust: true)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
output = <<-OUTPUT
|
113
|
+
+--------+------------+
|
114
|
+
| [bold]header[/] | [bold]nested.key[/] |
|
115
|
+
+--------+------------+
|
116
|
+
| 12345 | value |
|
117
|
+
+--------+------------+
|
118
|
+
OUTPUT
|
119
|
+
output = Formatador.parse(output)
|
120
|
+
|
121
|
+
tests("#display_table([{:header => 12345, :nested => {:key => value}}], [:header, :'nested.key'], numeric_rjust: true)").returns(output) do
|
122
|
+
capture_stdout do
|
123
|
+
Formatador.display_table([{:header => 12345, :nested => {:key => 'value'}}], [:header, :'nested.key'], numeric_rjust: true)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
output = <<-OUTPUT
|
129
|
+
+------+
|
130
|
+
| [bold]a[/] |
|
131
|
+
+------+
|
132
|
+
| 1 |
|
133
|
+
+------+
|
134
|
+
| 震度 |
|
135
|
+
+------+
|
136
|
+
OUTPUT
|
137
|
+
output = Formatador.parse(output)
|
138
|
+
|
139
|
+
tests("#display_table([{:a => 1}, {:a => '震度'}])").returns(output) do
|
140
|
+
capture_stdout do
|
141
|
+
Formatador.display_table([{:a => 1}, {:a => "震度"}])
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
data/tests/tests_helper.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formatador
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- geemus (Wesley Beary)
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2022-01-24 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
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'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
28
39
|
- !ruby/object:Gem::Version
|
29
40
|
version: '0'
|
30
41
|
- !ruby/object:Gem::Dependency
|
31
42
|
name: shindo
|
32
43
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
44
|
requirements:
|
35
|
-
- -
|
45
|
+
- - ">="
|
36
46
|
- !ruby/object:Gem::Version
|
37
47
|
version: '0'
|
38
48
|
type: :development
|
39
49
|
prerelease: false
|
40
50
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
51
|
requirements:
|
43
|
-
- -
|
52
|
+
- - ">="
|
44
53
|
- !ruby/object:Gem::Version
|
45
54
|
version: '0'
|
46
55
|
description: STDOUT text formatting
|
@@ -48,10 +57,13 @@ email: geemus@gmail.com
|
|
48
57
|
executables: []
|
49
58
|
extensions: []
|
50
59
|
extra_rdoc_files:
|
51
|
-
- README.
|
60
|
+
- README.md
|
52
61
|
files:
|
62
|
+
- CONTRIBUTING.md
|
63
|
+
- CONTRIBUTORS.md
|
53
64
|
- Gemfile
|
54
|
-
-
|
65
|
+
- LICENSE.md
|
66
|
+
- README.md
|
55
67
|
- Rakefile
|
56
68
|
- changelog.txt
|
57
69
|
- formatador.gemspec
|
@@ -61,32 +73,28 @@ files:
|
|
61
73
|
- tests/basic_tests.rb
|
62
74
|
- tests/table_tests.rb
|
63
75
|
- tests/tests_helper.rb
|
64
|
-
homepage:
|
65
|
-
licenses:
|
66
|
-
|
76
|
+
homepage: https://github.com/geemus/formatador
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
67
81
|
rdoc_options:
|
68
|
-
- --charset=UTF-8
|
82
|
+
- "--charset=UTF-8"
|
69
83
|
require_paths:
|
70
84
|
- lib
|
71
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
86
|
requirements:
|
74
|
-
- -
|
87
|
+
- - ">="
|
75
88
|
- !ruby/object:Gem::Version
|
76
89
|
version: '0'
|
77
|
-
segments:
|
78
|
-
- 0
|
79
|
-
hash: 26397976056427726
|
80
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
91
|
requirements:
|
83
|
-
- -
|
92
|
+
- - ">="
|
84
93
|
- !ruby/object:Gem::Version
|
85
94
|
version: '0'
|
86
95
|
requirements: []
|
87
|
-
|
88
|
-
|
89
|
-
signing_key:
|
96
|
+
rubygems_version: 3.3.5
|
97
|
+
signing_key:
|
90
98
|
specification_version: 2
|
91
99
|
summary: Ruby STDOUT text formatting
|
92
100
|
test_files: []
|