formatador 1.1.1 → 1.2.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.
- checksums.yaml +4 -4
- data/changelog.txt +11 -0
- data/formatador.gemspec +3 -2
- data/lib/formatador/table.rb +1 -5
- data/lib/formatador.rb +66 -58
- data/tests/table_tests.rb +18 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 907613ddcc1485d890419b8198c117fb441e02e487e61371f50bd455d8aca4a1
|
4
|
+
data.tar.gz: 9f8ad9ea1d10ad47831a6132328aaf7d4cb80e87eb21bcc1f42fda5f0dd81a2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1aaf1339006a7791ccdd9941e21d7b28ab993e528c463aaf089014cbcc88e9861f6fc5aef9b8654d8cce3fbefd81be949b4dc3f20dcd0a5731030345ea3d7ecf
|
7
|
+
data.tar.gz: 8f4026b8a0d35a4ba1eae4eff98d45cff61edc498d6378cf5dc10d86b1b04677669541096bf67a29883eb6306a53af6ba2c23f9b4134b4ad8cfa5526df90adfb
|
data/changelog.txt
CHANGED
data/formatador.gemspec
CHANGED
@@ -13,8 +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 = '1.
|
17
|
-
s.date = '2025-
|
16
|
+
s.version = '1.2.1'
|
17
|
+
s.date = '2025-09-17'
|
18
18
|
|
19
19
|
## Make sure your summary is short. The description may be as long
|
20
20
|
## as you like.
|
@@ -49,6 +49,7 @@ Gem::Specification.new do |s|
|
|
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.
|
51
51
|
# s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"])
|
52
|
+
s.add_dependency('reline')
|
52
53
|
|
53
54
|
## List your development dependencies here. Development dependencies are
|
54
55
|
## those that are only needed during development
|
data/lib/formatador/table.rb
CHANGED
@@ -80,11 +80,7 @@ class Formatador
|
|
80
80
|
private
|
81
81
|
|
82
82
|
def length(value)
|
83
|
-
|
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
|
83
|
+
Reline::Unicode.calculate_width(value.to_s.gsub(PARSE_REGEX, ''))
|
88
84
|
|
89
85
|
rescue NotImplementedError
|
90
86
|
value.to_s.gsub(PARSE_REGEX, '').chars.reduce(0) { |sum, char| sum += char.bytesize > 1 ? 2 : 1 }
|
data/lib/formatador.rb
CHANGED
@@ -1,60 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require File.join(File.dirname(__FILE__), 'formatador', 'table')
|
2
4
|
require File.join(File.dirname(__FILE__), 'formatador', 'progressbar')
|
3
5
|
|
4
|
-
|
6
|
+
require 'reline' # for table char width calculations
|
5
7
|
|
6
|
-
|
8
|
+
class Formatador
|
9
|
+
VERSION = '1.2.1'
|
7
10
|
|
8
11
|
STYLES = {
|
9
|
-
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
13
|
-
:
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
20
|
-
:
|
21
|
-
:
|
22
|
-
:
|
23
|
-
:
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
33
|
-
:
|
34
|
-
:
|
35
|
-
:
|
36
|
-
:
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:
|
40
|
-
:
|
41
|
-
:
|
42
|
-
:
|
43
|
-
:
|
44
|
-
:
|
45
|
-
:
|
46
|
-
:
|
47
|
-
:
|
48
|
-
:
|
49
|
-
:
|
50
|
-
:
|
51
|
-
:
|
52
|
-
:
|
53
|
-
:
|
54
|
-
}
|
55
|
-
|
56
|
-
PARSE_REGEX = /\[(#{
|
57
|
-
INDENT_REGEX = /\[indent\]/ix
|
12
|
+
"\/": '0',
|
13
|
+
reset: '0',
|
14
|
+
bold: '1',
|
15
|
+
underline: '4',
|
16
|
+
blink_slow: '5',
|
17
|
+
blink_fast: '6',
|
18
|
+
negative: '7', # invert color/color
|
19
|
+
normal: '22',
|
20
|
+
underline_none: '24',
|
21
|
+
blink_off: '25',
|
22
|
+
positive: '27', # revert color/color
|
23
|
+
_black_: '40',
|
24
|
+
_red_: '41',
|
25
|
+
_green_: '42',
|
26
|
+
_yellow_: '43',
|
27
|
+
_blue_: '44',
|
28
|
+
_magenta_: '45',
|
29
|
+
_purple_: '45',
|
30
|
+
_cyan_: '46',
|
31
|
+
_white_: '47',
|
32
|
+
_light_black_: '100',
|
33
|
+
_light_red_: '101',
|
34
|
+
_light_green_: '102',
|
35
|
+
_light_yellow_: '103',
|
36
|
+
_light_blue_: '104',
|
37
|
+
_light_magenta_: '105',
|
38
|
+
_light_purple_: '105',
|
39
|
+
_light_cyan_: '106',
|
40
|
+
black: '30',
|
41
|
+
red: '31',
|
42
|
+
green: '32',
|
43
|
+
yellow: '33',
|
44
|
+
blue: '34',
|
45
|
+
magenta: '35',
|
46
|
+
purple: '35',
|
47
|
+
cyan: '36',
|
48
|
+
white: '37',
|
49
|
+
light_black: '90',
|
50
|
+
light_red: '91',
|
51
|
+
light_green: '92',
|
52
|
+
light_yellow: '93',
|
53
|
+
light_blue: '94',
|
54
|
+
light_magenta: '95',
|
55
|
+
light_purple: '95',
|
56
|
+
light_cyan: '96'
|
57
|
+
}.freeze
|
58
|
+
|
59
|
+
PARSE_REGEX = /\[(#{STYLES.keys.join('|')})\]/ix.freeze
|
60
|
+
INDENT_REGEX = /\[indent\]/ix.freeze
|
58
61
|
|
59
62
|
def initialize
|
60
63
|
@indent = 1
|
@@ -73,21 +76,21 @@ class Formatador
|
|
73
76
|
end
|
74
77
|
|
75
78
|
def display_lines(lines = [])
|
76
|
-
|
79
|
+
[*lines].each do |line|
|
77
80
|
display_line(line)
|
78
81
|
end
|
79
82
|
nil
|
80
83
|
end
|
81
84
|
|
82
85
|
def parse(string)
|
83
|
-
if
|
84
|
-
string.gsub(PARSE_REGEX) { "\e[#{STYLES[
|
86
|
+
if color_support
|
87
|
+
string.gsub(PARSE_REGEX) { "\e[#{STYLES[::Regexp.last_match(1).to_sym]}m" }.gsub(INDENT_REGEX) { indentation }
|
85
88
|
else
|
86
89
|
strip(string)
|
87
90
|
end
|
88
91
|
end
|
89
92
|
|
90
|
-
def indent
|
93
|
+
def indent
|
91
94
|
@indent += 1
|
92
95
|
yield
|
93
96
|
ensure
|
@@ -100,7 +103,7 @@ class Formatador
|
|
100
103
|
|
101
104
|
def redisplay(string = '', width = 120)
|
102
105
|
print("\r#{' ' * width}\r")
|
103
|
-
display(
|
106
|
+
display(string.to_s)
|
104
107
|
nil
|
105
108
|
end
|
106
109
|
|
@@ -119,7 +122,7 @@ class Formatador
|
|
119
122
|
string.gsub(PARSE_REGEX, '').gsub(INDENT_REGEX) { indentation }
|
120
123
|
end
|
121
124
|
|
122
|
-
%w
|
125
|
+
%w[display display_line display_lines indent parse redisplay redisplay_line new_line redisplay_progressbar].each do |method|
|
123
126
|
eval <<-DEF
|
124
127
|
def self.#{method}(*args, &block)
|
125
128
|
Thread.current[:formatador] ||= new
|
@@ -128,7 +131,7 @@ class Formatador
|
|
128
131
|
DEF
|
129
132
|
end
|
130
133
|
|
131
|
-
%w
|
134
|
+
%w[display_table display_compact_table].each do |method|
|
132
135
|
eval <<-DEF
|
133
136
|
def self.#{method}(*args, **kwargs, &block)
|
134
137
|
Thread.current[:formatador] ||= new
|
@@ -137,4 +140,9 @@ class Formatador
|
|
137
140
|
DEF
|
138
141
|
end
|
139
142
|
|
143
|
+
private
|
144
|
+
|
145
|
+
def color_support
|
146
|
+
@color_support ||= $stdout.tty? || ENV['GITHUB_ACTIONS'] == 'true'
|
147
|
+
end
|
140
148
|
end
|
data/tests/table_tests.rb
CHANGED
@@ -153,4 +153,22 @@ OUTPUT
|
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
156
|
+
output = <<-OUTPUT
|
157
|
+
+----+
|
158
|
+
| [bold]a[/] |
|
159
|
+
+----+
|
160
|
+
| 1 |
|
161
|
+
+----+
|
162
|
+
| 🤷 |
|
163
|
+
+----+
|
164
|
+
OUTPUT
|
165
|
+
output = Formatador.parse(output)
|
166
|
+
|
167
|
+
tests("#display_table([{:a => 1}, {:a => '🤷'}])").returns(output) do
|
168
|
+
capture_stdout do
|
169
|
+
Formatador.display_table([{:a => 1}, {:a => '🤷'}])
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
|
156
174
|
end
|
metadata
CHANGED
@@ -1,14 +1,28 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formatador
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- geemus (Wesley Beary)
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-09-17 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: reline
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
12
26
|
- !ruby/object:Gem::Dependency
|
13
27
|
name: rake
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|