just-ansi 0.1.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f93be0e88f2c592c08f145ee09ac614745333ecd14b9a9bb024c50adf3b2d549
4
- data.tar.gz: daa92f7cb65f9ba56c5991ad2876f7aa4cc2738532dce52cf69ef25432a81275
3
+ metadata.gz: 4bc8e95b8442246c0bb54c0fc9fd4bbb0196023a254f0127cd77f1699c7af47d
4
+ data.tar.gz: de0c011547c12fdc2568233a17d40b0228ea4604f5114db02c860d67c0737ad9
5
5
  SHA512:
6
- metadata.gz: 11c986e236eff5ef0ce7d815c39341dddb6050f8deec3e50df7a48fb45595d7ab722daa44c5b03d907e31332611fbe3099b05fd3c338c485bd54c00eb9713b31
7
- data.tar.gz: d910ca7ac0374a2ab2237aa139e1e3cd5e1f29bfbf429ea6e65c7684d5d8932405f86ad5d8096961d59d7aa2bfccff8ac3215912681229b29507dfc502127e54
6
+ metadata.gz: 918be47dc5e4562af228c7a82e32713424fcd771cf78b174ae72fd8bfea37011af96a40b07564be935e4d28f25bb855d07a4db6cce058781b2a7ccc3998ad4f1
7
+ data.tar.gz: a908ba695fc54a3503e7faccab0fabeaded67385b3ff0a2a42ad7c12615ad6e2dc1aab81c8ca331d77bf040f417cd5f101181cf9d00cf08eb7577294b975a897
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
- # shareable_constant_value: literal
3
2
 
4
3
  module JustAnsi
5
4
  ATTRIBUTES = {
5
+ '/' => '',
6
6
  '/b' => '22',
7
7
  '/blink' => '25',
8
8
  '/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.1'
6
6
  end
data/lib/just-ansi.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # shareable_constant_value: literal
3
2
 
4
3
  #
5
4
  # Simple and fast ANSI control code processing.
@@ -187,6 +186,50 @@ module JustAnsi
187
186
  }m"
188
187
  end
189
188
 
189
+ # Replace embedded BBCode-like attributes with ANSI codes.
190
+ #
191
+ # @example
192
+ # JustAnsi.bbcode "[b]Bold[/b] Text"
193
+ # # "\e[1mBold\e[22m Text"
194
+ #
195
+ # @param str [#to_s] string to be modified
196
+ # @return [String] string with ANSI attributes
197
+ def bbcode(str)
198
+ str
199
+ .to_s
200
+ .gsub(BBCODE) do |match_str|
201
+ match = Regexp.last_match[1]
202
+ next "[#{match[1..]}]" if match[0] == '\\'
203
+ try_convert(match) || match_str
204
+ end
205
+ end
206
+
207
+ # Remove embedded BBCode-like attributes.
208
+ #
209
+ # @example
210
+ # JustAnsi.unbbcode "[b]Bold[/b] Text"
211
+ # # "Bold Text"
212
+ #
213
+ # @param str [#to_s] string to be modified
214
+ # @return [String] string without BBCode
215
+ def unbbcode(str)
216
+ str
217
+ .to_s
218
+ .gsub(BBCODE) do |match_str|
219
+ next match_str if (match = Regexp.last_match[1]).empty?
220
+ next "[#{match[1..]}]" if match[0] == '\\'
221
+ next match_str if (match = match.split).empty?
222
+ next if match.all? { ATTRIBUTES[_1] || COLORS[_1] || _color(_1) }
223
+ match_str
224
+ end
225
+ end
226
+
227
+ # Remove any BBCode-like and/or ANSI attributes.
228
+ #
229
+ # @param str [#to_s] string to be modified
230
+ # @return [String] string without BBCode and ANSI control codes.
231
+ def plain(str) = unbbcode(str).gsub(TEST, '')
232
+
190
233
  # @!group Control functions
191
234
 
192
235
  # Move cursor given lines up.
@@ -334,7 +377,7 @@ module JustAnsi
334
377
  #
335
378
  # @param [String] title text
336
379
  # @return (see cursor_up)
337
- def window_title(title) = "\e]2;#{title}\e\\"
380
+ def window_title(title) = "\e]2;#{title}\a"
338
381
 
339
382
  # Change tab title.
340
383
  # This is not widely supported.
@@ -345,7 +388,9 @@ module JustAnsi
345
388
 
346
389
  # Create a hyperlink.
347
390
  # This is not widely supported.
348
- def link(url, text) = "\e]8;;#{url}\e\\#{text}\e]8;;\e\\"
391
+ def link(url, text) = "\e]8;;#{url}\a#{text}\e]8;;\a"
392
+
393
+ # @comment simple def notify(title) = "\e]9;#{title}\a"
349
394
 
350
395
  # @!endgroup
351
396
 
@@ -403,15 +448,17 @@ module JustAnsi
403
448
  end
404
449
 
405
450
  TEST =
406
- /\e
407
- (?:\[[\d;:\?]*[ABCDEFGHJKSTfminsuhl])
451
+ /
452
+ (?:\e\[[\d;:\?]*[ABCDEFGHJKSTfminsuhl])
408
453
  |
409
- (?:\]\d+(?:;[^;\a\e]+)*(?:\a|\e\\))
454
+ (?:\e\]\d+(?:;[^\a\e]+)*(?:\a|\e\\))
410
455
  /x
411
456
 
457
+ BBCODE = /(?:\[((?~[\[\]]))\])/
458
+
412
459
  require_relative 'just-ansi/attributes'
413
460
  autoload :NAMED_COLORS, File.join(__dir__, 'just-ansi', 'named_colors')
414
- private_constant :TEST, :NAMED_COLORS
461
+ private_constant :TEST, :BBCODE, :NAMED_COLORS
415
462
 
416
463
  # @!visibility private
417
464
  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.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-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