just-ansi 0.1.1 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f93be0e88f2c592c08f145ee09ac614745333ecd14b9a9bb024c50adf3b2d549
4
- data.tar.gz: daa92f7cb65f9ba56c5991ad2876f7aa4cc2738532dce52cf69ef25432a81275
3
+ metadata.gz: de63b65fe1ca722779a0847310877197661780173df41c2f95178c36a5b85cb4
4
+ data.tar.gz: 2893de40b471bb6c461a9dc16e82fcfc990ee12172440cfac17f49b2192c49b2
5
5
  SHA512:
6
- metadata.gz: 11c986e236eff5ef0ce7d815c39341dddb6050f8deec3e50df7a48fb45595d7ab722daa44c5b03d907e31332611fbe3099b05fd3c338c485bd54c00eb9713b31
7
- data.tar.gz: d910ca7ac0374a2ab2237aa139e1e3cd5e1f29bfbf429ea6e65c7684d5d8932405f86ad5d8096961d59d7aa2bfccff8ac3215912681229b29507dfc502127e54
6
+ metadata.gz: b7a6bbaab57d7c56d8579cc5120e48ea2f5be68831f848102883b48c32a81a175c2cb0823d147b9c7ec8ea385355d612c17b1da79dbf04b8928d40a92052f326
7
+ data.tar.gz: 169589c1a0bf8712c233bb64a87cca32dc4248d2d9564542b6c9edfbc166587609e72c5adf87551c800c97559f87d51c74a06d0425f66c7a6d8c611601fff2e7
@@ -3,6 +3,7 @@
3
3
 
4
4
  module JustAnsi
5
5
  ATTRIBUTES = {
6
+ '/' => '',
6
7
  '/b' => '22',
7
8
  '/blink' => '25',
8
9
  '/bold' => '22',
@@ -2,7 +2,14 @@
2
2
 
3
3
  require_relative '../just-ansi'
4
4
 
5
+ #
5
6
  # Ruby String class ANSI extension.
7
+ #
8
+ # This extension is not load by default but you need to require it.
9
+ #
10
+ # @example
11
+ # require 'just-ansi/string'
12
+ #
6
13
  class String
7
14
  # Test if String contains ANSI codes.
8
15
  #
@@ -28,4 +35,37 @@ class String
28
35
  #
29
36
  # @return [String] string without ANSI attributes
30
37
  def unansi = JustAnsi.undecorate(self)
38
+
39
+ if defined?(:bbcode)
40
+ def bbcode = JustAnsi.bbcode(super)
41
+ else
42
+ # Replace embedded BBCode-like attributes with ANSI codes.
43
+ #
44
+ # @see JustAnsi.bbcode
45
+ #
46
+ # @return [String] string with ANSI attributes
47
+ def bbcode = JustAnsi.bbcode(self)
48
+ end
49
+
50
+ if defined?(:unbbcode)
51
+ def unbbcode = JustAnsi.unbbcode(super)
52
+ else
53
+ # Remove embedded BBCode-like attributes.
54
+ #
55
+ # @see JustAnsi.unbbcode
56
+ #
57
+ # @return [String] string without BBCode
58
+ def unbbcode = JustAnsi.unbbcode(self)
59
+ end
60
+
61
+ if defined?(:plain)
62
+ def plain = JustAnsi.plain(super)
63
+ else
64
+ # Remove any BBCode-like and/or ANSI attributes.
65
+ #
66
+ # @see JustAnsi.plain
67
+ #
68
+ # @return [String] string without BBCode and ANSI control codes.
69
+ def plain = JustAnsi.plain(self)
70
+ end
31
71
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module JustAnsi
4
4
  # The version number of the gem.
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.0'
6
6
  end
data/lib/just-ansi.rb CHANGED
@@ -187,6 +187,50 @@ module JustAnsi
187
187
  }m"
188
188
  end
189
189
 
190
+ # Replace embedded BBCode-like attributes with ANSI codes.
191
+ #
192
+ # @example
193
+ # JustAnsi.bbcode "[b]Bold[/b] Text"
194
+ # # "\e[1mBold\e[22m Text"
195
+ #
196
+ # @param str [#to_s] string to be modified
197
+ # @return [String] string with ANSI attributes
198
+ def bbcode(str)
199
+ str
200
+ .to_s
201
+ .gsub(BBCODE) do |match_str|
202
+ match = Regexp.last_match[1]
203
+ next "[#{match[1..]}]" if match[0] == '\\'
204
+ try_convert(match) || match_str
205
+ end
206
+ end
207
+
208
+ # Remove embedded BBCode-like attributes.
209
+ #
210
+ # @example
211
+ # JustAnsi.unbbcode "[b]Bold[/b] Text"
212
+ # # "Bold Text"
213
+ #
214
+ # @param str [#to_s] string to be modified
215
+ # @return [String] string without BBCode
216
+ def unbbcode(str)
217
+ str
218
+ .to_s
219
+ .gsub(BBCODE) do |match_str|
220
+ next match_str if (match = Regexp.last_match[1]).empty?
221
+ next "[#{match[1..]}]" if match[0] == '\\'
222
+ next match_str if (match = match.split).empty?
223
+ next if match.all? { ATTRIBUTES[_1] || COLORS[_1] || _color(_1) }
224
+ match_str
225
+ end
226
+ end
227
+
228
+ # Remove any BBCode-like and/or ANSI attributes.
229
+ #
230
+ # @param str [#to_s] string to be modified
231
+ # @return [String] string without BBCode and ANSI control codes.
232
+ def plain(str) = unbbcode(str).gsub(TEST, '')
233
+
190
234
  # @!group Control functions
191
235
 
192
236
  # Move cursor given lines up.
@@ -334,7 +378,7 @@ module JustAnsi
334
378
  #
335
379
  # @param [String] title text
336
380
  # @return (see cursor_up)
337
- def window_title(title) = "\e]2;#{title}\e\\"
381
+ def window_title(title) = "\e]2;#{title}\a"
338
382
 
339
383
  # Change tab title.
340
384
  # This is not widely supported.
@@ -345,7 +389,9 @@ module JustAnsi
345
389
 
346
390
  # Create a hyperlink.
347
391
  # This is not widely supported.
348
- def link(url, text) = "\e]8;;#{url}\e\\#{text}\e]8;;\e\\"
392
+ def link(url, text) = "\e]8;;#{url}\a#{text}\e]8;;\a"
393
+
394
+ # @comment simple def notify(title) = "\e]9;#{title}\a"
349
395
 
350
396
  # @!endgroup
351
397
 
@@ -403,15 +449,17 @@ module JustAnsi
403
449
  end
404
450
 
405
451
  TEST =
406
- /\e
407
- (?:\[[\d;:\?]*[ABCDEFGHJKSTfminsuhl])
452
+ /
453
+ (?:\e\[[\d;:\?]*[ABCDEFGHJKSTfminsuhl])
408
454
  |
409
- (?:\]\d+(?:;[^;\a\e]+)*(?:\a|\e\\))
455
+ (?:\e\]\d+(?:;[^\a\e]+)*(?:\a|\e\\))
410
456
  /x
411
457
 
458
+ BBCODE = /(?:\[((?~[\[\]]))\])/
459
+
412
460
  require_relative 'just-ansi/attributes'
413
461
  autoload :NAMED_COLORS, File.join(__dir__, 'just-ansi', 'named_colors')
414
- private_constant :TEST, :NAMED_COLORS
462
+ private_constant :TEST, :BBCODE, :NAMED_COLORS
415
463
 
416
464
  # @!visibility private
417
465
  RESET = self[:reset].freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just-ansi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
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-09-14 00:00:00.000000000 Z
11
+ date: 2024-09-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Simple and fast ANSI control code processing.
14
14