sai 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sai/decorator.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'sai'
4
4
  require 'sai/ansi'
5
+ require 'sai/ansi/sequenced_string'
5
6
  require 'sai/conversion/color_sequence'
6
7
 
7
8
  module Sai
@@ -39,7 +40,7 @@ module Sai
39
40
  # @api public
40
41
  #
41
42
  # @example
42
- # decorator.black.decorate('Hello, world!') #=> "\e[30mHello, world!\e[0m"
43
+ # decorator.black.decorate('Hello, world!').to_s #=> "\e[30mHello, world!\e[0m"
43
44
  #
44
45
  # @return [Decorator] a new instance of Decorator with the color applied
45
46
  #
@@ -52,7 +53,7 @@ module Sai
52
53
  # @api public
53
54
  #
54
55
  # @example
55
- # decorator.blue.decorate('Hello, world!') #=> "\e[34mHello, world!\e[0m"
56
+ # decorator.blue.decorate('Hello, world!').to_s #=> "\e[34mHello, world!\e[0m"
56
57
  #
57
58
  # @return [Decorator] a new instance of Decorator with the color applied
58
59
  #
@@ -65,7 +66,7 @@ module Sai
65
66
  # @api public
66
67
  #
67
68
  # @example
68
- # decorator.bright_black.decorate('Hello, world!') #=> "\e[90mHello, world!\e[0m"
69
+ # decorator.bright_black.decorate('Hello, world!').to_s #=> "\e[90mHello, world!\e[0m"
69
70
  #
70
71
  # @return [Decorator] a new instance of Decorator with the color applied
71
72
  #
@@ -75,7 +76,7 @@ module Sai
75
76
  # @api public
76
77
  #
77
78
  # @example
78
- # decorator.bright_blue.decorate('Hello, world!') #=> "\e[94mHello, world!\e[0m"
79
+ # decorator.bright_blue.decorate('Hello, world!').to_s #=> "\e[94mHello, world!\e[0m"
79
80
  #
80
81
  # @return [Decorator] a new instance of Decorator with the color applied
81
82
  #
@@ -85,7 +86,7 @@ module Sai
85
86
  # @api public
86
87
  #
87
88
  # @example
88
- # decorator.bright_cyan.decorate('Hello, world!') #=> "\e[96mHello, world!\e[0m"
89
+ # decorator.bright_cyan.decorate('Hello, world!').to_s #=> "\e[96mHello, world!\e[0m"
89
90
  #
90
91
  # @return [Decorator] a new instance of Decorator with the color applied
91
92
  #
@@ -95,7 +96,7 @@ module Sai
95
96
  # @api public
96
97
  #
97
98
  # @example
98
- # decorator.bright_green.decorate('Hello, world!') #=> "\e[92mHello, world!\e[0m"
99
+ # decorator.bright_green.decorate('Hello, world!').to_s #=> "\e[92mHello, world!\e[0m"
99
100
  #
100
101
  # @return [Decorator] a new instance of Decorator with the color applied
101
102
  #
@@ -105,7 +106,7 @@ module Sai
105
106
  # @api public
106
107
  #
107
108
  # @example
108
- # decorator.bright_magenta.decorate('Hello, world!') #=> "\e[95mHello, world!\e[0m"
109
+ # decorator.bright_magenta.decorate('Hello, world!').to_s #=> "\e[95mHello, world!\e[0m"
109
110
  #
110
111
  # @return [Decorator] a new instance of Decorator with the color applied
111
112
  #
@@ -115,7 +116,7 @@ module Sai
115
116
  # @api public
116
117
  #
117
118
  # @example
118
- # decorator.bright_red.decorate('Hello, world!') #=> "\e[91mHello, world!\e[0m"
119
+ # decorator.bright_red.decorate('Hello, world!').to_s #=> "\e[91mHello, world!\e[0m"
119
120
  #
120
121
  # @return [Decorator] a new instance of Decorator with the color applied
121
122
  #
@@ -125,7 +126,7 @@ module Sai
125
126
  # @api public
126
127
  #
127
128
  # @example
128
- # decorator.bright_white.decorate('Hello, world!') #=> "\e[97mHello, world!\e[0m"
129
+ # decorator.bright_white.decorate('Hello, world!').to_s #=> "\e[97mHello, world!\e[0m"
129
130
  #
130
131
  # @return [Decorator] a new instance of Decorator with the color applied
131
132
  #
@@ -135,7 +136,7 @@ module Sai
135
136
  # @api public
136
137
  #
137
138
  # @example
138
- # decorator.bright_yellow.decorate('Hello, world!') #=> "\e[93mHello, world!\e[0m"
139
+ # decorator.bright_yellow.decorate('Hello, world!').to_s #=> "\e[93mHello, world!\e[0m"
139
140
  #
140
141
  # @return [Decorator] a new instance of Decorator with the color applied
141
142
  #
@@ -145,7 +146,7 @@ module Sai
145
146
  # @api public
146
147
  #
147
148
  # @example
148
- # decorator.cyan.decorate('Hello, world!') #=> "\e[36mHello, world!\e[0m"
149
+ # decorator.cyan.decorate('Hello, world!').to_s #=> "\e[36mHello, world!\e[0m"
149
150
  #
150
151
  # @return [Decorator] a new instance of Decorator with the color applied
151
152
  #
@@ -155,7 +156,7 @@ module Sai
155
156
  # @api public
156
157
  #
157
158
  # @example
158
- # decorator.green.decorate('Hello, world!') #=> "\e[32mHello, world!\e[0m"
159
+ # decorator.green.decorate('Hello, world!').to_s #=> "\e[32mHello, world!\e[0m"
159
160
  #
160
161
  # @return [Decorator] a new instance of Decorator with the color applied
161
162
  #
@@ -165,7 +166,7 @@ module Sai
165
166
  # @api public
166
167
  #
167
168
  # @example
168
- # decorator.magenta.decorate('Hello, world!') #=> "\e[35mHello, world!\e[0m"
169
+ # decorator.magenta.decorate('Hello, world!').to_s #=> "\e[35mHello, world!\e[0m"
169
170
  #
170
171
  # @return [Decorator] a new instance of Decorator with the color applied
171
172
  #
@@ -178,7 +179,7 @@ module Sai
178
179
  # @api public
179
180
  #
180
181
  # @example
181
- # decorator.on_black.decorate('Hello, world!') #=> "\e[40mHello, world!\e[0m"
182
+ # decorator.on_black.decorate('Hello, world!').to_s #=> "\e[40mHello, world!\e[0m"
182
183
  #
183
184
  # @return [Decorator] a new instance of Decorator with the color applied
184
185
  #
@@ -191,7 +192,7 @@ module Sai
191
192
  # @api public
192
193
  #
193
194
  # @example
194
- # decorator.on_blue.decorate('Hello, world!') #=> "\e[44mHello, world!\e[0m"
195
+ # decorator.on_blue.decorate('Hello, world!').to_s #=> "\e[44mHello, world!\e[0m"
195
196
  #
196
197
  # @return [Decorator] a new instance of Decorator with the color applied
197
198
  #
@@ -204,7 +205,7 @@ module Sai
204
205
  # @api public
205
206
  #
206
207
  # @example
207
- # decorator.on_bright_black.decorate('Hello, world!') #=> "\e[100mHello, world!\e[0m"
208
+ # decorator.on_bright_black.decorate('Hello, world!').to_s #=> "\e[100mHello, world!\e[0m"
208
209
  #
209
210
  # @return [Decorator] a new instance of Decorator with the color applied
210
211
  #
@@ -217,7 +218,7 @@ module Sai
217
218
  # @api public
218
219
  #
219
220
  # @example
220
- # decorator.on_bright_blue.decorate('Hello, world!') #=> "\e[104mHello, world!\e[0m"
221
+ # decorator.on_bright_blue.decorate('Hello, world!').to_s #=> "\e[104mHello, world!\e[0m"
221
222
  #
222
223
  # @return [Decorator] a new instance of Decorator with the color applied
223
224
  #
@@ -230,7 +231,7 @@ module Sai
230
231
  # @api public
231
232
  #
232
233
  # @example
233
- # decorator.on_bright_cyan.decorate('Hello, world!') #=> "\e[106mHello, world!\e[0m"
234
+ # decorator.on_bright_cyan.decorate('Hello, world!').to_s #=> "\e[106mHello, world!\e[0m"
234
235
  #
235
236
  # @return [Decorator] a new instance of Decorator with the color applied
236
237
  #
@@ -243,7 +244,7 @@ module Sai
243
244
  # @api public
244
245
  #
245
246
  # @example
246
- # decorator.on_bright_green.decorate('Hello, world!') #=> "\e[102mHello, world!\e[0m"
247
+ # decorator.on_bright_green.decorate('Hello, world!').to_s #=> "\e[102mHello, world!\e[0m"
247
248
  #
248
249
  # @return [Decorator] a new instance of Decorator with the color applied
249
250
  #
@@ -256,7 +257,7 @@ module Sai
256
257
  # @api public
257
258
  #
258
259
  # @example
259
- # decorator.on_bright_magenta.decorate('Hello, world!') #=> "\e[105mHello, world!\e[0m"
260
+ # decorator.on_bright_magenta.decorate('Hello, world!').to_s #=> "\e[105mHello, world!\e[0m"
260
261
  #
261
262
  # @return [Decorator] a new instance of Decorator with the color applied
262
263
  #
@@ -269,7 +270,7 @@ module Sai
269
270
  # @api public
270
271
  #
271
272
  # @example
272
- # decorator.on_bright_red.decorate('Hello, world!') #=> "\e[101mHello, world!\e[0m"
273
+ # decorator.on_bright_red.decorate('Hello, world!').to_s #=> "\e[101mHello, world!\e[0m"
273
274
  #
274
275
  # @return [Decorator] a new instance of Decorator with the color applied
275
276
  #
@@ -282,7 +283,7 @@ module Sai
282
283
  # @api public
283
284
  #
284
285
  # @example
285
- # decorator.on_bright_white.decorate('Hello, world!') #=> "\e[107mHello, world!\e[0m"
286
+ # decorator.on_bright_white.decorate('Hello, world!').to_s #=> "\e[107mHello, world!\e[0m"
286
287
  #
287
288
  # @return [Decorator] a new instance of Decorator with the color applied
288
289
  #
@@ -295,7 +296,7 @@ module Sai
295
296
  # @api public
296
297
  #
297
298
  # @example
298
- # decorator.on_bright_yellow.decorate('Hello, world!') #=> "\e[103mHello, world!\e[0m"
299
+ # decorator.on_bright_yellow.decorate('Hello, world!').to_s #=> "\e[103mHello, world!\e[0m"
299
300
  #
300
301
  # @return [Decorator] a new instance of Decorator with the color applied
301
302
  #
@@ -308,7 +309,7 @@ module Sai
308
309
  # @api public
309
310
  #
310
311
  # @example
311
- # decorator.on_cyan.decorate('Hello, world!') #=> "\e[46mHello, world!\e[0m"
312
+ # decorator.on_cyan.decorate('Hello, world!').to_s #=> "\e[46mHello, world!\e[0m"
312
313
  #
313
314
  # @return [Decorator] a new instance of Decorator with the color applied
314
315
  #
@@ -321,7 +322,7 @@ module Sai
321
322
  # @api public
322
323
  #
323
324
  # @example
324
- # decorator.on_green.decorate('Hello, world!') #=> "\e[42mHello, world!\e[0m"
325
+ # decorator.on_green.decorate('Hello, world!').to_s #=> "\e[42mHello, world!\e[0m"
325
326
  #
326
327
  # @return [Decorator] a new instance of Decorator with the color applied
327
328
  #
@@ -334,7 +335,7 @@ module Sai
334
335
  # @api public
335
336
  #
336
337
  # @example
337
- # decorator.on_magenta.decorate('Hello, world!') #=> "\e[45mHello, world!\e[0m"
338
+ # decorator.on_magenta.decorate('Hello, world!').to_s #=> "\e[45mHello, world!\e[0m"
338
339
  #
339
340
  # @return [Decorator] a new instance of Decorator with the color applied
340
341
  #
@@ -347,7 +348,7 @@ module Sai
347
348
  # @api public
348
349
  #
349
350
  # @example
350
- # decorator.on_red.decorate('Hello, world!') #=> "\e[41mHello, world!\e[0m"
351
+ # decorator.on_red.decorate('Hello, world!').to_s #=> "\e[41mHello, world!\e[0m"
351
352
  #
352
353
  # @return [Decorator] a new instance of Decorator with the color applied
353
354
  #
@@ -360,7 +361,7 @@ module Sai
360
361
  # @api public
361
362
  #
362
363
  # @example
363
- # decorator.on_white.decorate('Hello, world!') #=> "\e[47mHello, world!\e[0m"
364
+ # decorator.on_white.decorate('Hello, world!').to_s #=> "\e[47mHello, world!\e[0m"
364
365
  #
365
366
  # @return [Decorator] a new instance of Decorator with the color applied
366
367
  #
@@ -373,7 +374,7 @@ module Sai
373
374
  # @api public
374
375
  #
375
376
  # @example
376
- # decorator.on_yellow.decorate('Hello, world!') #=> "\e[43mHello, world!\e[0m"
377
+ # decorator.on_yellow.decorate('Hello, world!').to_s #=> "\e[43mHello, world!\e[0m"
377
378
  #
378
379
  # @return [Decorator] a new instance of Decorator with the color applied
379
380
  #
@@ -383,7 +384,7 @@ module Sai
383
384
  # @api public
384
385
  #
385
386
  # @example
386
- # decorator.red.decorate('Hello, world!') #=> "\e[31mHello, world!\e[0m"
387
+ # decorator.red.decorate('Hello, world!').to_s #=> "\e[31mHello, world!\e[0m"
387
388
  #
388
389
  # @return [Decorator] a new instance of Decorator with the color applied
389
390
  #
@@ -393,7 +394,7 @@ module Sai
393
394
  # @api public
394
395
  #
395
396
  # @example
396
- # decorator.white.decorate('Hello, world!') #=> "\e[37mHello, world!\e[0m"
397
+ # decorator.white.decorate('Hello, world!').to_s #=> "\e[37mHello, world!\e[0m"
397
398
  #
398
399
  # @return [Decorator] a new instance of Decorator with the color applied
399
400
  #
@@ -403,7 +404,7 @@ module Sai
403
404
  # @api public
404
405
  #
405
406
  # @example
406
- # decorator.yellow.decorate('Hello, world!') #=> "\e[33mHello, world!\e[0m"
407
+ # decorator.yellow.decorate('Hello, world!').to_s #=> "\e[33mHello, world!\e[0m"
407
408
  #
408
409
  # @return [Decorator] a new instance of Decorator with the color applied
409
410
  ANSI::COLOR_NAMES.each_key do |color|
@@ -457,7 +458,7 @@ module Sai
457
458
  # @api public
458
459
  #
459
460
  # @example
460
- # decorator.blink.decorate('Hello, world!') #=> "\e[5mHello, world!\e[0m"
461
+ # decorator.blink.decorate('Hello, world!').to_s #=> "\e[5mHello, world!\e[0m"
461
462
  #
462
463
  # @return [Decorator] a new instance of Decorator with the style applied
463
464
  #
@@ -470,7 +471,7 @@ module Sai
470
471
  # @api public
471
472
  #
472
473
  # @example
473
- # decorator.bold.decorate('Hello, world!') #=> "\e[1mHello, world!\e[0m"
474
+ # decorator.bold.decorate('Hello, world!').to_s #=> "\e[1mHello, world!\e[0m"
474
475
  #
475
476
  # @return [Decorator] a new instance of Decorator with the style applied
476
477
  #
@@ -483,7 +484,7 @@ module Sai
483
484
  # @api public
484
485
  #
485
486
  # @example
486
- # decorator.conceal.decorate('Hello, world!') #=> "\e[8mHello, world!\e[0m"
487
+ # decorator.conceal.decorate('Hello, world!').to_s #=> "\e[8mHello, world!\e[0m"
487
488
  #
488
489
  # @return [Decorator] a new instance of Decorator with the style applied
489
490
  #
@@ -496,7 +497,7 @@ module Sai
496
497
  # @api public
497
498
  #
498
499
  # @example
499
- # decorator.dim.decorate('Hello, world!') #=> "\e[2mHello, world!\e[0m"
500
+ # decorator.dim.decorate('Hello, world!').to_s #=> "\e[2mHello, world!\e[0m"
500
501
  #
501
502
  # @return [Decorator] a new instance of Decorator with the style applied
502
503
  #
@@ -509,7 +510,7 @@ module Sai
509
510
  # @api public
510
511
  #
511
512
  # @example
512
- # decorator.italic.decorate('Hello, world!') #=> "\e[3mHello, world!\e[0m"
513
+ # decorator.italic.decorate('Hello, world!').to_s #=> "\e[3mHello, world!\e[0m"
513
514
  #
514
515
  # @return [Decorator] a new instance of Decorator with the style applied
515
516
  #
@@ -522,7 +523,7 @@ module Sai
522
523
  # @api public
523
524
  #
524
525
  # @example
525
- # decorator.no_blink.decorate('Hello, world!') #=> "\e[25mHello, world!\e[0m"
526
+ # decorator.no_blink.decorate('Hello, world!').to_s #=> "\e[25mHello, world!\e[0m"
526
527
  #
527
528
  # @return [Decorator] a new instance of Decorator with the style applied
528
529
  #
@@ -535,7 +536,7 @@ module Sai
535
536
  # @api public
536
537
  #
537
538
  # @example
538
- # decorator.no_conceal.decorate('Hello, world!') #=> "\e[28mHello, world!\e[0m"
539
+ # decorator.no_conceal.decorate('Hello, world!').to_s #=> "\e[28mHello, world!\e[0m"
539
540
  #
540
541
  # @return [Decorator] a new instance of Decorator with the style applied
541
542
  #
@@ -548,7 +549,7 @@ module Sai
548
549
  # @api public
549
550
  #
550
551
  # @example
551
- # decorator.no_italic.decorate('Hello, world!') #=> "\e[23mHello, world!\e[0m"
552
+ # decorator.no_italic.decorate('Hello, world!').to_s #=> "\e[23mHello, world!\e[0m"
552
553
  #
553
554
  # @return [Decorator] a new instance of Decorator with the style applied
554
555
  #
@@ -561,7 +562,7 @@ module Sai
561
562
  # @api public
562
563
  #
563
564
  # @example
564
- # decorator.no_reverse.decorate('Hello, world!') #=> "\e[27mHello, world!\e[0m"
565
+ # decorator.no_reverse.decorate('Hello, world!').to_s #=> "\e[27mHello, world!\e[0m"
565
566
  #
566
567
  # @return [Decorator] a new instance of Decorator with the style applied
567
568
  #
@@ -574,7 +575,7 @@ module Sai
574
575
  # @api public
575
576
  #
576
577
  # @example
577
- # decorator.no_strike.decorate('Hello, world!') #=> "\e[29mHello, world!\e[0m"
578
+ # decorator.no_strike.decorate('Hello, world!').to_s #=> "\e[29mHello, world!\e[0m"
578
579
  #
579
580
  # @return [Decorator] a new instance of Decorator with the style applied
580
581
  #
@@ -587,7 +588,7 @@ module Sai
587
588
  # @api public
588
589
  #
589
590
  # @example
590
- # decorator.no_underline.decorate('Hello, world!') #=> "\e[24mHello, world!\e[0m"
591
+ # decorator.no_underline.decorate('Hello, world!').to_s #=> "\e[24mHello, world!\e[0m"
591
592
  #
592
593
  # @return [Decorator] a new instance of Decorator with the style applied
593
594
  #
@@ -600,7 +601,7 @@ module Sai
600
601
  # @api public
601
602
  #
602
603
  # @example
603
- # decorator.normal_intensity.decorate('Hello, world!') #=> "\e[22mHello, world!\e[0m"
604
+ # decorator.normal_intensity.decorate('Hello, world!').to_s #=> "\e[22mHello, world!\e[0m"
604
605
  #
605
606
  # @return [Decorator] a new instance of Decorator with the style applied
606
607
  #
@@ -613,7 +614,7 @@ module Sai
613
614
  # @api public
614
615
  #
615
616
  # @example
616
- # decorator.rapid_blink.decorate('Hello, world!') #=> "\e[6mHello, world!\e[0m"
617
+ # decorator.rapid_blink.decorate('Hello, world!').to_s #=> "\e[6mHello, world!\e[0m"
617
618
  #
618
619
  # @return [Decorator] a new instance of Decorator with the style applied
619
620
  #
@@ -626,7 +627,7 @@ module Sai
626
627
  # @api public
627
628
  #
628
629
  # @example
629
- # decorator.reverse.decorate('Hello, world!') #=> "\e[7mHello, world!\e[0m"
630
+ # decorator.reverse.decorate('Hello, world!').to_s #=> "\e[7mHello, world!\e[0m"
630
631
  #
631
632
  # @return [Decorator] a new instance of Decorator with the style applied
632
633
  #
@@ -639,7 +640,7 @@ module Sai
639
640
  # @api public
640
641
  #
641
642
  # @example
642
- # decorator.strike.decorate('Hello, world!') #=> "\e[9mHello, world!\e[0m"
643
+ # decorator.strike.decorate('Hello, world!').to_s #=> "\e[9mHello, world!\e[0m"
643
644
  #
644
645
  # @return [Decorator] a new instance of Decorator with the style applied
645
646
  #
@@ -652,7 +653,7 @@ module Sai
652
653
  # @api public
653
654
  #
654
655
  # @example
655
- # decorator.underline.decorate('Hello, world!') #=> "\e[4mHello, world!\e[0m"
656
+ # decorator.underline.decorate('Hello, world!').to_s #=> "\e[4mHello, world!\e[0m"
656
657
  #
657
658
  # @return [Decorator] a new instance of Decorator with the style applied
658
659
  ANSI::STYLES.each_key do |style|
@@ -687,15 +688,14 @@ module Sai
687
688
  # @api public
688
689
  #
689
690
  # @example
690
- # decorator.red.on_blue.bold.decorate('Hello, world!')
691
- # #=> "\e[38;2;205;0;0m\e[48;2;0;0;238m\e[1mHello, world!\e[0m"
691
+ # decorator.red.on_blue.bold.decorate('Hello, world!').to_s #=> "\e[38;5;160;48;5;21;1mHello, world!\e[0m"
692
692
  #
693
693
  # @param text [String] the text to decorate
694
694
  #
695
- # @return [String] the decorated text
696
- # @rbs (String text) -> String
695
+ # @return [ANSI::SequencedString] the decorated text
696
+ # @rbs (String text) -> ANSI::SequencedString
697
697
  def decorate(text)
698
- return text unless should_decorate?
698
+ return ANSI::SequencedString.new(text) unless should_decorate?
699
699
 
700
700
  sequences = [
701
701
  @foreground && Conversion::ColorSequence.resolve(@foreground, @mode),
@@ -703,7 +703,7 @@ module Sai
703
703
  @styles.map { |style| "\e[#{ANSI::STYLES[style]}m" }.join
704
704
  ].compact.join
705
705
 
706
- "#{sequences}#{text}#{ANSI::RESET}"
706
+ ANSI::SequencedString.new("#{sequences}#{text}#{ANSI::RESET}")
707
707
  end
708
708
  alias apply decorate
709
709
  alias call decorate
@@ -717,7 +717,7 @@ module Sai
717
717
  # @api public
718
718
  #
719
719
  # @example
720
- # decorator.hex("#EB4133").decorate('Hello, world!') #=> "\e[38;2;235;65;51mHello, world!\e[0m"
720
+ # decorator.hex("#EB4133").decorate('Hello, world!').to_s #=> "\e[38;2;235;65;51mHello, world!\e[0m"
721
721
  #
722
722
  # @param code [String] the hex color code
723
723
  #
@@ -738,7 +738,7 @@ module Sai
738
738
  # @api public
739
739
  #
740
740
  # @example
741
- # decorator.on_hex("#EB4133").decorate('Hello, world!') #=> "\e[48;2;235;65;51mHello, world!\e[0m"
741
+ # decorator.on_hex("#EB4133").decorate('Hello, world!').to_s #=> "\e[48;2;235;65;51mHello, world!\e[0m"
742
742
  #
743
743
  # @param code [String] the hex color code
744
744
  #
@@ -759,7 +759,7 @@ module Sai
759
759
  # @api public
760
760
  #
761
761
  # @example
762
- # decorator.on_rgb(235, 65, 51).decorate('Hello, world!') #=> "\e[48;2;235;65;51mHello, world!\e[0m"
762
+ # decorator.on_rgb(235, 65, 51).decorate('Hello, world!').to_s #=> "\e[48;2;235;65;51mHello, world!\e[0m"
763
763
  #
764
764
  # @param red [Integer] the red component
765
765
  # @param green [Integer] the green component
@@ -784,7 +784,7 @@ module Sai
784
784
  # @api public
785
785
  #
786
786
  # @example
787
- # decorator.rgb(235, 65, 51).decorate('Hello, world!') #=> "\e[38;2;235;65;51mHello, world!\e[0m"
787
+ # decorator.rgb(235, 65, 51).decorate('Hello, world!').to_s #=> "\e[38;2;235;65;51mHello, world!\e[0m"
788
788
  #
789
789
  # @param red [Integer] the red component
790
790
  # @param green [Integer] the green component
@@ -831,16 +831,9 @@ module Sai
831
831
  # @param style_type [Symbol] the style type to apply the color to
832
832
  # @param color [Symbol] the color to apply
833
833
  #
834
- # @raise [ArgumentError] if the color is invalid
835
834
  # @return [Decorator] a new instance of Decorator with the color applied
836
835
  # @rbs (Conversion::ColorSequence::style_type style_type, Symbol color) -> Decorator
837
836
  def apply_named_color(style_type, color)
838
- unless ANSI::COLOR_NAMES.key?(color.to_s.downcase.to_sym)
839
- e = ArgumentError.new("Invalid color: #{color}")
840
- e.set_backtrace(caller_locations(1, 1)&.map(&:to_s)) # steep:ignore UnresolvedOverloading
841
- raise e
842
- end
843
-
844
837
  dup.tap { |duped| duped.instance_variable_set(:"@#{style_type}", color) }
845
838
  end
846
839
 
@@ -853,17 +846,10 @@ module Sai
853
846
  #
854
847
  # @param style [String, Symbol] the style to apply
855
848
  #
856
- # @raise [ArgumentError] if the style is invalid
857
849
  # @return [Decorator] a new instance of Decorator with the style applied
858
850
  # @rbs (String | Symbol style) -> self
859
851
  def apply_style(style)
860
852
  style = style.to_s.downcase.to_sym
861
- unless ANSI::STYLES.key?(style)
862
- e = ArgumentError.new("Invalid style: #{style}")
863
- e.set_backtrace(caller_locations(1, 1)&.map(&:to_s)) # steep:ignore UnresolvedOverloading
864
- raise e
865
- end
866
-
867
853
  dup.tap { |duped| duped.instance_variable_set(:@styles, (@styles + [style]).uniq) }
868
854
  end
869
855
 
@@ -17,7 +17,7 @@ module Sai
17
17
  # @author {https://aaronmallen.me Aaron Allen}
18
18
  # @since 0.2.0
19
19
  #
20
- # @api private
20
+ # @api public
21
21
  #
22
22
  # @example
23
23
  # Sai.mode.advanced #=> 3
@@ -47,31 +47,31 @@ module Sai
47
47
  # @api public
48
48
  #
49
49
  # @example With color support enabled
50
- # ENV['COLORTERM'] #=> nil
51
- # ENV['TERM'] #=> 'xterm-256color'
52
- # Sai.mode.ansi_auto #=> 3
53
- # Sai.mode.four_bit_auto #=> 3
54
- # Sai.mode.color_16_auto #=> 3
50
+ # ENV['COLORTERM'] #=> nil
51
+ # ENV['TERM'] #=> 'xterm-256color'
52
+ # Sai.mode.advanced_auto #=> 3
53
+ # Sai.mode.eight_bit_auto #=> 3
54
+ # Sai.mode.color256_auto #=> 3
55
55
  #
56
56
  # @example With only 4-bit color support
57
- # ENV['NO_COLOR'] #=> nil
58
- # ENV['TERM'] #=> 'ansi'
59
- # Sai.mode.ansi_auto #=> 2
60
- # Sai.mode.four_bit_auto #=> 2
61
- # Sai.mode.color_16_auto #=> 2
57
+ # ENV['NO_COLOR'] #=> nil
58
+ # ENV['TERM'] #=> 'ansi'
59
+ # Sai.mode.advanced_auto #=> 2
60
+ # Sai.mode.eight_bit_auto #=> 2
61
+ # Sai.mode.color256_auto #=> 2
62
62
  #
63
63
  # @example With only 3-bit color support
64
64
  # ENV['TERM'] #=> nil
65
65
  # ENV['NO_COLOR'] #=> nil
66
- # Sai.mode.ansi_auto #=> 1
67
- # Sai.mode.four_bit_auto #=> 1
68
- # Sai.mode.color16_auto #=> 1
66
+ # Sai.mode.advanced_auto #=> 1
67
+ # Sai.mode.eight_bit_auto #=> 1
68
+ # Sai.mode.color256_auto #=> 1
69
69
  #
70
70
  # @example With color support disabled
71
71
  # ENV['NO_COLOR'] #=> 'true'
72
- # Sai.mode.ansi_auto #=> 0
73
- # Sai.mode.four_bit_auto #=> 0
74
- # Sai.mode.color16_auto #=> 0
72
+ # Sai.mode.advanced_auto #=> 0
73
+ # Sai.mode.eight_bit_auto #=> 0
74
+ # Sai.mode.color256_auto #=> 0
75
75
  #
76
76
  # @return [Integer] the color mode
77
77
  # @rbs () -> Integer
@@ -123,7 +123,7 @@ module Sai
123
123
  # ENV['TERM'] #=> 'ansi'
124
124
  # Sai.mode.ansi_auto #=> 2
125
125
  # Sai.mode.four_bit_auto #=> 2
126
- # Sai.mode.color_16_auto #=> 2
126
+ # Sai.mode.color16_auto #=> 2
127
127
  #
128
128
  # @example With only 3-bit color support
129
129
  # ENV['TERM'] #=> nil
@@ -276,7 +276,7 @@ module Sai
276
276
  # @author {https://aaronmallen.me Aaron Allen}
277
277
  # @since 0.2.0
278
278
  #
279
- # @api private
279
+ # @api public
280
280
  #
281
281
  # @example
282
282
  # Sai.mode.true_color #=> 4
data/lib/sai.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'sai/ansi'
4
+ require 'sai/ansi/sequence_processor'
5
+ require 'sai/ansi/sequenced_string'
3
6
  require 'sai/conversion/color_sequence'
4
7
  require 'sai/conversion/rgb'
5
8
  require 'sai/decorator'
@@ -19,6 +22,7 @@ require 'sai/terminal/color_mode'
19
22
  # adaptive color to your terminal interfaces
20
23
  #
21
24
  # When included in a class or module, Sai provides the following instance methods:
25
+ # * {#color_mode} - Returns an interface to select Sai color modes
22
26
  # * {#decorator} - Returns a new instance of {Decorator} for method chaining
23
27
  # * {#terminal_color_support} - Returns the color support capabilities of the current terminal
24
28
  #
@@ -121,6 +125,24 @@ module Sai
121
125
  # def white: () -> Decorator
122
126
  # def yellow: () -> Decorator
123
127
 
128
+ # Sequence a string with ANSI escape codes
129
+ #
130
+ # @author {https://aaronmallen.me Aaron Allen}
131
+ # @since 0.3.0
132
+ #
133
+ # @api public
134
+ #
135
+ # @example Sequence a string with ANSI escape codes
136
+ # Sai.sequence("\e[38;2;205;0;0mHello, World!\e[0m") #=> #<Sai::ANSI::SequencedString:0x123>
137
+ #
138
+ # @param text [String] the text to sequence
139
+ #
140
+ # @return [ANSI::SequencedString] the sequenced string
141
+ # @rbs (String text) -> ANSI::SequencedString
142
+ def sequence(text)
143
+ ANSI::SequencedString.new(text)
144
+ end
145
+
124
146
  # The supported color modes for the terminal
125
147
  #
126
148
  # @author {https://aaronmallen.me Aaron Allen}
data/sig/manifest.yaml ADDED
@@ -0,0 +1,3 @@
1
+ dependencies:
2
+ - name: forwardable
3
+ - name: strscan