natty-ui 0.9.0 → 0.9.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/examples/illustration.rb +4 -3
- data/lib/natty-ui/ansi.rb +13 -3
- data/lib/natty-ui/version.rb +1 -1
- data/lib/natty-ui.rb +33 -22
- metadata +2 -3
- data/examples/illustration.png +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88f26cebef7a281c4b11df8e8accf096706d4308aa22d716bb516f9c85b51be6
|
4
|
+
data.tar.gz: 62fc6e920615b9f4536da8a9db33cc9e4261352d36a3423cdd11c64ab7b2e93c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 231f29c1bca8face152dbb63ed1c6137866a97941e092ed3db3f30ee2c372d71bbb10b1885100fd28691fb6093d2d4deab14fcf8d95fe05106cfc0c2d44d3b66
|
7
|
+
data.tar.gz: e39a9f14e9adfb4bfc1360cc9ee3dda19f96b3cce23afbf97da2315b184bf5b0b62f62c0f4e6fb283f22029d2902bc14623beec8cb42c45d528ea5168a5bb274
|
data/examples/illustration.rb
CHANGED
@@ -7,9 +7,10 @@ ui.framed do
|
|
7
7
|
This is the [[c4]]beautiful[[/]], [[c5]]nice[[/]], [[c6]]nifty[[/]],
|
8
8
|
[[c7]]fancy[[/]], [[c8]]neat[[/]], [[c9]]pretty[[/]], [[ca]]cool[[/]],
|
9
9
|
[[cb]]lovely[[/]], [[cc]]natty[[/]] [[bold]]user interface[[/]] you like to
|
10
|
-
have for your command line applications. It contains
|
11
|
-
[[
|
12
|
-
|
10
|
+
have for your command line applications. It contains
|
11
|
+
[[curly_underline ul_c4]]elegant[[/]], [[italic]]simple[[/]] and
|
12
|
+
[[double_underline ul_cc]]beautiful[[/]] tools that enhance your command
|
13
|
+
line interfaces functionally and aesthetically.
|
13
14
|
TEXT
|
14
15
|
|
15
16
|
ui.msg('Features', glyph: '⭐️') { ui.ls(<<~FEATURES.lines(chomp: true)) }
|
data/lib/natty-ui/ansi.rb
CHANGED
@@ -239,7 +239,7 @@ module NattyUI
|
|
239
239
|
#
|
240
240
|
# @param str [#to_s] string to be modified
|
241
241
|
# @return [String] string without ANSI attributes
|
242
|
-
def blemish(str) = str.to_s.gsub(
|
242
|
+
def blemish(str) = str.to_s.gsub(ESC, '')
|
243
243
|
|
244
244
|
# Try to combine given ANSI attributes and colors.
|
245
245
|
# The attributes and colors have to be seperated by space char (" ").
|
@@ -331,6 +331,8 @@ module NattyUI
|
|
331
331
|
end
|
332
332
|
end
|
333
333
|
|
334
|
+
ESC = /(#{Reline::Unicode::CSI_REGEXP})|(#{Reline::Unicode::OSC_REGEXP})/
|
335
|
+
|
334
336
|
CLR_PREFIX = {
|
335
337
|
'fg' => '38',
|
336
338
|
'bg' => '48',
|
@@ -477,8 +479,16 @@ module NattyUI
|
|
477
479
|
ATTR = SATTR.transform_keys(&:to_s).freeze
|
478
480
|
SCLR = CLR.transform_keys(&:to_sym).compare_by_identity.freeze
|
479
481
|
|
480
|
-
private_constant
|
481
|
-
|
482
|
+
private_constant(
|
483
|
+
:ESC,
|
484
|
+
:CLR_PREFIX,
|
485
|
+
:PI2_THIRD,
|
486
|
+
:PI4_THIRD,
|
487
|
+
:SATTR,
|
488
|
+
:CLR,
|
489
|
+
:ATTR,
|
490
|
+
:SCLR
|
491
|
+
)
|
482
492
|
end
|
483
493
|
end
|
484
494
|
|
data/lib/natty-ui/version.rb
CHANGED
data/lib/natty-ui.rb
CHANGED
@@ -83,18 +83,19 @@ module NattyUI
|
|
83
83
|
def embellish(str)
|
84
84
|
return +'' if (str = str.to_s).empty?
|
85
85
|
reset = false
|
86
|
-
|
86
|
+
str =
|
87
87
|
str.gsub(/(\[\[((?~\]\]))\]\])/) do
|
88
88
|
match = Regexp.last_match[2]
|
89
|
-
|
89
|
+
if match[0] == '/'
|
90
|
+
next "[[#{match[1..]}]]" if match.size > 1
|
91
|
+
reset = false
|
92
|
+
Ansi::RESET
|
93
|
+
else
|
90
94
|
ansi = Ansi.try_convert(match)
|
91
|
-
|
95
|
+
ansi ? reset = ansi : "[[#{match}]]"
|
92
96
|
end
|
93
|
-
match.empty? or next "[[#{match}]]"
|
94
|
-
reset = false
|
95
|
-
Ansi::RESET
|
96
97
|
end
|
97
|
-
reset ? "#{
|
98
|
+
reset ? "#{str}#{Ansi::RESET}" : str
|
98
99
|
end
|
99
100
|
|
100
101
|
# Remove embedded attribute descriptions from given string.
|
@@ -103,16 +104,13 @@ module NattyUI
|
|
103
104
|
# @param [:keep,:remove] ansi keep or remove ANSI codes too
|
104
105
|
# @return [String] edited string
|
105
106
|
def plain(str, ansi: :keep)
|
107
|
+
return +'' if (str = str.to_s).empty?
|
106
108
|
str =
|
107
|
-
str
|
108
|
-
.
|
109
|
-
.
|
110
|
-
|
111
|
-
|
112
|
-
next match.empty? ? nil : "[[#{match}]]"
|
113
|
-
end
|
114
|
-
Ansi.try_convert(match) ? nil : "[[#{match}]]"
|
115
|
-
end
|
109
|
+
str.gsub(/(\[\[((?~\]\]))\]\])/) do
|
110
|
+
match = Regexp.last_match[2]
|
111
|
+
next match.size == 1 ? nil : "[[#{match[1..]}]]" if match[0] == '/'
|
112
|
+
Ansi.try_convert(match) ? nil : "[[#{match}]]"
|
113
|
+
end
|
116
114
|
ansi == :keep ? str : Ansi.blemish(str)
|
117
115
|
end
|
118
116
|
|
@@ -122,8 +120,21 @@ module NattyUI
|
|
122
120
|
# @param [#to_s] str string to calculate
|
123
121
|
# @return [Integer] the display size
|
124
122
|
def display_width(str)
|
125
|
-
|
126
|
-
|
123
|
+
str = plain(str).encode(Encoding::UTF_8)
|
124
|
+
return 0 if str.empty?
|
125
|
+
width = 0
|
126
|
+
in_zero_width = false
|
127
|
+
str.scan(
|
128
|
+
Reline::Unicode::WIDTH_SCANNER
|
129
|
+
) do |non_printing_start, non_printing_end, _csi, _osc, gc|
|
130
|
+
if in_zero_width
|
131
|
+
in_zero_width = false if non_printing_end
|
132
|
+
next
|
133
|
+
end
|
134
|
+
next in_zero_width = true if non_printing_start
|
135
|
+
width += Reline::Unicode.get_mbchar_width(gc) if gc
|
136
|
+
end
|
137
|
+
width
|
127
138
|
end
|
128
139
|
|
129
140
|
# Convert given arguments into strings and yield each line.
|
@@ -190,10 +201,10 @@ module NattyUI
|
|
190
201
|
private
|
191
202
|
|
192
203
|
def wrapper_class(stream, ansi)
|
193
|
-
return AnsiWrapper if ansi == true
|
194
|
-
if ansi == false || ENV.key?('NO_COLOR')
|
195
|
-
|
196
|
-
|
204
|
+
return AnsiWrapper if ansi == true
|
205
|
+
return Wrapper if ansi == false || ENV.key?('NO_COLOR')
|
206
|
+
return AnsiWrapper if ENV['ANSI'] == '1'
|
207
|
+
return Wrapper if ENV['TERM'] == 'dumb'
|
197
208
|
stream.tty? ? AnsiWrapper : Wrapper
|
198
209
|
end
|
199
210
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: natty-ui
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Blumtritt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
This is the beautiful, nice, nifty, fancy, neat, pretty, cool, lovely,
|
@@ -31,7 +31,6 @@ files:
|
|
31
31
|
- examples/animate.rb
|
32
32
|
- examples/attributes.rb
|
33
33
|
- examples/demo.rb
|
34
|
-
- examples/illustration.png
|
35
34
|
- examples/illustration.rb
|
36
35
|
- examples/ls.rb
|
37
36
|
- examples/message.rb
|
data/examples/illustration.png
DELETED
Binary file
|