sai 0.2.0 → 0.3.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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +38 -1
  4. data/README.md +39 -241
  5. data/docs/USAGE.md +351 -0
  6. data/lib/sai/ansi/color_parser.rb +109 -0
  7. data/lib/sai/ansi/sequence_processor.rb +269 -0
  8. data/lib/sai/ansi/sequenced_string.rb +475 -0
  9. data/lib/sai/ansi/style_parser.rb +66 -0
  10. data/lib/sai/ansi.rb +0 -27
  11. data/lib/sai/conversion/color_sequence.rb +4 -4
  12. data/lib/sai/conversion/rgb/color_classifier.rb +209 -0
  13. data/lib/sai/conversion/rgb/color_indexer.rb +48 -0
  14. data/lib/sai/conversion/rgb/color_space.rb +192 -0
  15. data/lib/sai/conversion/rgb/color_transformer.rb +140 -0
  16. data/lib/sai/conversion/rgb.rb +23 -269
  17. data/lib/sai/decorator/color_manipulations.rb +157 -0
  18. data/lib/sai/decorator/delegation.rb +84 -0
  19. data/lib/sai/decorator/gradients.rb +363 -0
  20. data/lib/sai/decorator/hex_colors.rb +56 -0
  21. data/lib/sai/decorator/named_colors.rb +780 -0
  22. data/lib/sai/decorator/named_styles.rb +276 -0
  23. data/lib/sai/decorator/rgb_colors.rb +64 -0
  24. data/lib/sai/decorator.rb +35 -795
  25. data/lib/sai/mode_selector.rb +19 -19
  26. data/lib/sai/named_colors.rb +437 -0
  27. data/lib/sai.rb +753 -23
  28. data/sig/manifest.yaml +3 -0
  29. data/sig/sai/ansi/color_parser.rbs +77 -0
  30. data/sig/sai/ansi/sequence_processor.rbs +178 -0
  31. data/sig/sai/ansi/sequenced_string.rbs +380 -0
  32. data/sig/sai/ansi/style_parser.rbs +59 -0
  33. data/sig/sai/ansi.rbs +0 -10
  34. data/sig/sai/conversion/rgb/color_classifier.rbs +165 -0
  35. data/sig/sai/conversion/rgb/color_indexer.rbs +41 -0
  36. data/sig/sai/conversion/rgb/color_space.rbs +129 -0
  37. data/sig/sai/conversion/rgb/color_transformer.rbs +99 -0
  38. data/sig/sai/conversion/rgb.rbs +15 -198
  39. data/sig/sai/decorator/color_manipulations.rbs +125 -0
  40. data/sig/sai/decorator/delegation.rbs +47 -0
  41. data/sig/sai/decorator/gradients.rbs +267 -0
  42. data/sig/sai/decorator/hex_colors.rbs +48 -0
  43. data/sig/sai/decorator/named_colors.rbs +1491 -0
  44. data/sig/sai/decorator/named_styles.rbs +72 -0
  45. data/sig/sai/decorator/rgb_colors.rbs +52 -0
  46. data/sig/sai/decorator.rbs +25 -202
  47. data/sig/sai/mode_selector.rbs +19 -19
  48. data/sig/sai/named_colors.rbs +65 -0
  49. data/sig/sai.rbs +1485 -44
  50. metadata +38 -4
@@ -0,0 +1,72 @@
1
+ # Generated from lib/sai/decorator/named_styles.rb with RBS::Inline
2
+
3
+ module Sai
4
+ class Decorator
5
+ # Named style methods for the {Decorator} class
6
+ #
7
+ # @author {https://aaronmallen.me Aaron Allen}
8
+ # @since 0.3.1
9
+ #
10
+ # @abstract This module is meant to be included in the {Decorator} class to provide named style methods
11
+ # @api private
12
+ module NamedStyles
13
+ def blink: () -> Decorator
14
+
15
+ def bold: () -> Decorator
16
+
17
+ def conceal: () -> Decorator
18
+
19
+ def dim: () -> Decorator
20
+
21
+ def italic: () -> Decorator
22
+
23
+ def no_blink: () -> Decorator
24
+
25
+ def no_conceal: () -> Decorator
26
+
27
+ def no_italic: () -> Decorator
28
+
29
+ def no_reverse: () -> Decorator
30
+
31
+ def no_strike: () -> Decorator
32
+
33
+ def no_underline: () -> Decorator
34
+
35
+ def normal_intensity: () -> Decorator
36
+
37
+ def rapid_blink: () -> Decorator
38
+
39
+ def reverse: () -> Decorator
40
+
41
+ def strike: () -> Decorator
42
+
43
+ def underline: () -> Decorator
44
+
45
+ private
46
+
47
+ # Apply a style to the text
48
+ #
49
+ # @author {https://aaronmallen.me Aaron Allen}
50
+ # @since 0.1.0
51
+ #
52
+ # @api private
53
+ #
54
+ # @param style [String, Symbol] the style to apply
55
+ #
56
+ # @return [Decorator] a new instance of Decorator with the style applied
57
+ # @rbs (String | Symbol style) -> Decorator
58
+ def apply_style: (String | Symbol style) -> Decorator
59
+
60
+ # Get style sequences
61
+ #
62
+ # @author {https://aaronmallen.me Aaron Allen}
63
+ # @since 0.3.1
64
+ #
65
+ # @api private
66
+ #
67
+ # @return [Array<String>] ANSI sequences for styles
68
+ # @rbs () -> Array[String]
69
+ def style_sequences: () -> Array[String]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,52 @@
1
+ # Generated from lib/sai/decorator/rgb_colors.rb with RBS::Inline
2
+
3
+ module Sai
4
+ class Decorator
5
+ # RGB color methods for the {Decorator} class
6
+ #
7
+ # @author {https://aaronmallen.me Aaron Allen}
8
+ # @since 0.3.1
9
+ #
10
+ # @abstract This module is meant to be included in the {Decorator} class to provide RGB color methods
11
+ # @api private
12
+ module RGBColors
13
+ # Apply an RGB color to the background
14
+ #
15
+ # @author {https://aaronmallen.me Aaron Allen}
16
+ # @since 0.1.0
17
+ #
18
+ # @api public
19
+ #
20
+ # @example
21
+ # decorator.on_rgb(235, 65, 51).decorate('Hello, world!').to_s #=> "\e[48;2;235;65;51mHello, world!\e[0m"
22
+ #
23
+ # @param red [Integer] the red component
24
+ # @param green [Integer] the green component
25
+ # @param blue [Integer] the blue component
26
+ #
27
+ # @raise [ArgumentError] if the RGB values are out of range
28
+ # @return [Decorator] a new instance of Decorator with the RGB color applied
29
+ # @rbs (Integer red, Integer green, Integer blue) -> Decorator
30
+ def on_rgb: (Integer red, Integer green, Integer blue) -> Decorator
31
+
32
+ # Apply an RGB color to the foreground
33
+ #
34
+ # @author {https://aaronmallen.me Aaron Allen}
35
+ # @since 0.1.0
36
+ #
37
+ # @api public
38
+ #
39
+ # @example
40
+ # decorator.rgb(235, 65, 51).decorate('Hello, world!').to_s #=> "\e[38;2;235;65;51mHello, world!\e[0m"
41
+ #
42
+ # @param red [Integer] the red component
43
+ # @param green [Integer] the green component
44
+ # @param blue [Integer] the blue component
45
+ #
46
+ # @raise [ArgumentError] if the RGB values are out of range
47
+ # @return [Decorator] a new instance of Decorator with the RGB color applied
48
+ # @rbs (Integer red, Integer green, Integer blue) -> Decorator
49
+ def rgb: (Integer red, Integer green, Integer blue) -> Decorator
50
+ end
51
+ end
52
+ end
@@ -7,7 +7,28 @@ module Sai
7
7
  # @since 0.1.0
8
8
  #
9
9
  # @api public
10
+ #
11
+ # @note For each named color, two methods are dynamically generated:
12
+ # * color_name - Applies the color to the foreground
13
+ # * on_color_name - Applies the color to the backgroundAll color methods return {Decorator}
14
+ # @see Sai::NamedColors Sai::NamedColors for available color names
15
+ #
16
+ # @example Using a named color
17
+ # decorator.azure.decorate('Hello') #=> "\e[38;2;0;127;255mHello\e[0m"
18
+ # decorator.on_azure.decorate('Hello') #=> "\e[48;2;0;127;255mHello\e[0m"
10
19
  class Decorator
20
+ include ColorManipulations
21
+
22
+ include Gradients
23
+
24
+ include HexColors
25
+
26
+ include NamedColors
27
+
28
+ include NamedStyles
29
+
30
+ include RGBColors
31
+
11
32
  # Initialize a new instance of Decorator
12
33
  #
13
34
  # @author {https://aaronmallen.me Aaron Allen}
@@ -21,102 +42,6 @@ module Sai
21
42
  # @rbs (?mode: Integer) -> void
22
43
  def initialize: (?mode: Integer) -> void
23
44
 
24
- def black: () -> Decorator
25
-
26
- def blue: () -> Decorator
27
-
28
- def bright_black: () -> Decorator
29
-
30
- def bright_blue: () -> Decorator
31
-
32
- def bright_cyan: () -> Decorator
33
-
34
- def bright_green: () -> Decorator
35
-
36
- def bright_magenta: () -> Decorator
37
-
38
- def bright_red: () -> Decorator
39
-
40
- def bright_white: () -> Decorator
41
-
42
- def bright_yellow: () -> Decorator
43
-
44
- def cyan: () -> Decorator
45
-
46
- def green: () -> Decorator
47
-
48
- def magenta: () -> Decorator
49
-
50
- def on_black: () -> Decorator
51
-
52
- def on_blue: () -> Decorator
53
-
54
- def on_bright_black: () -> Decorator
55
-
56
- def on_bright_blue: () -> Decorator
57
-
58
- def on_bright_cyan: () -> Decorator
59
-
60
- def on_bright_green: () -> Decorator
61
-
62
- def on_bright_magenta: () -> Decorator
63
-
64
- def on_bright_red: () -> Decorator
65
-
66
- def on_bright_white: () -> Decorator
67
-
68
- def on_bright_yellow: () -> Decorator
69
-
70
- def on_cyan: () -> Decorator
71
-
72
- def on_green: () -> Decorator
73
-
74
- def on_magenta: () -> Decorator
75
-
76
- def on_red: () -> Decorator
77
-
78
- def on_white: () -> Decorator
79
-
80
- def on_yellow: () -> Decorator
81
-
82
- def red: () -> Decorator
83
-
84
- def white: () -> Decorator
85
-
86
- def yellow: () -> Decorator
87
-
88
- def blink: () -> Decorator
89
-
90
- def bold: () -> Decorator
91
-
92
- def conceal: () -> Decorator
93
-
94
- def dim: () -> Decorator
95
-
96
- def italic: () -> Decorator
97
-
98
- def no_blink: () -> Decorator
99
-
100
- def no_conceal: () -> Decorator
101
-
102
- def no_italic: () -> Decorator
103
-
104
- def no_reverse: () -> Decorator
105
-
106
- def no_strike: () -> Decorator
107
-
108
- def no_underline: () -> Decorator
109
-
110
- def normal_intensity: () -> Decorator
111
-
112
- def rapid_blink: () -> Decorator
113
-
114
- def reverse: () -> Decorator
115
-
116
- def strike: () -> Decorator
117
-
118
- def underline: () -> Decorator
119
-
120
45
  # Apply the styles and colors to the text
121
46
  #
122
47
  # @author {https://aaronmallen.me Aaron Allen}
@@ -125,14 +50,13 @@ module Sai
125
50
  # @api public
126
51
  #
127
52
  # @example
128
- # decorator.red.on_blue.bold.decorate('Hello, world!')
129
- # #=> "\e[38;2;205;0;0m\e[48;2;0;0;238m\e[1mHello, world!\e[0m"
53
+ # decorator.red.on_blue.bold.decorate('Hello, world!').to_s #=> "\e[38;5;160;48;5;21;1mHello, world!\e[0m"
130
54
  #
131
55
  # @param text [String] the text to decorate
132
56
  #
133
- # @return [String] the decorated text
134
- # @rbs (String text) -> String
135
- def decorate: (String text) -> String
57
+ # @return [ANSI::SequencedString] the decorated text
58
+ # @rbs (String text) -> ANSI::SequencedString
59
+ def decorate: (String text) -> ANSI::SequencedString
136
60
 
137
61
  alias apply decorate
138
62
 
@@ -140,78 +64,6 @@ module Sai
140
64
 
141
65
  alias encode decorate
142
66
 
143
- # Apply a hexadecimal color to the foreground
144
- #
145
- # @author {https://aaronmallen.me Aaron Allen}
146
- # @since 0.1.0
147
- #
148
- # @api public
149
- #
150
- # @example
151
- # decorator.hex("#EB4133").decorate('Hello, world!') #=> "\e[38;2;235;65;51mHello, world!\e[0m"
152
- #
153
- # @param code [String] the hex color code
154
- #
155
- # @raise [ArgumentError] if the hex code is invalid
156
- # @return [Decorator] a new instance of Decorator with the hex color applied
157
- # @rbs (String code) -> Decorator
158
- def hex: (String code) -> Decorator
159
-
160
- # Apply a hexadecimal color to the background
161
- #
162
- # @author {https://aaronmallen.me Aaron Allen}
163
- # @since 0.1.0
164
- #
165
- # @api public
166
- #
167
- # @example
168
- # decorator.on_hex("#EB4133").decorate('Hello, world!') #=> "\e[48;2;235;65;51mHello, world!\e[0m"
169
- #
170
- # @param code [String] the hex color code
171
- #
172
- # @raise [ArgumentError] if the hex code is invalid
173
- # @return [Decorator] a new instance of Decorator with the hex color applied
174
- # @rbs (String code) -> Decorator
175
- def on_hex: (String code) -> Decorator
176
-
177
- # Apply an RGB color to the background
178
- #
179
- # @author {https://aaronmallen.me Aaron Allen}
180
- # @since 0.1.0
181
- #
182
- # @api public
183
- #
184
- # @example
185
- # decorator.on_rgb(235, 65, 51).decorate('Hello, world!') #=> "\e[48;2;235;65;51mHello, world!\e[0m"
186
- #
187
- # @param red [Integer] the red component
188
- # @param green [Integer] the green component
189
- # @param blue [Integer] the blue component
190
- #
191
- # @raise [ArgumentError] if the RGB values are out of range
192
- # @return [Decorator] a new instance of Decorator with the RGB color applied
193
- # @rbs (Integer red, Integer green, Integer blue) -> Decorator
194
- def on_rgb: (Integer red, Integer green, Integer blue) -> Decorator
195
-
196
- # Apply an RGB color to the foreground
197
- #
198
- # @author {https://aaronmallen.me Aaron Allen}
199
- # @since 0.1.0
200
- #
201
- # @api public
202
- #
203
- # @example
204
- # decorator.rgb(235, 65, 51).decorate('Hello, world!') #=> "\e[38;2;235;65;51mHello, world!\e[0m"
205
- #
206
- # @param red [Integer] the red component
207
- # @param green [Integer] the green component
208
- # @param blue [Integer] the blue component
209
- #
210
- # @raise [ArgumentError] if the RGB values are out of range
211
- # @return [Decorator] a new instance of Decorator with the RGB color applied
212
- # @rbs (Integer red, Integer green, Integer blue) -> Decorator
213
- def rgb: (Integer red, Integer green, Integer blue) -> Decorator
214
-
215
67
  # Apply a specific color mode to the decorator
216
68
  #
217
69
  # @author {https://aaronmallen.me Aaron Allen}
@@ -230,35 +82,6 @@ module Sai
230
82
 
231
83
  private
232
84
 
233
- # Apply a named color to the specified style type
234
- #
235
- # @author {https://aaronmallen.me Aaron Allen}
236
- # @since 0.1.0
237
- #
238
- # @api private
239
- #
240
- # @param style_type [Symbol] the style type to apply the color to
241
- # @param color [Symbol] the color to apply
242
- #
243
- # @raise [ArgumentError] if the color is invalid
244
- # @return [Decorator] a new instance of Decorator with the color applied
245
- # @rbs (Conversion::ColorSequence::style_type style_type, Symbol color) -> Decorator
246
- def apply_named_color: (Conversion::ColorSequence::style_type style_type, Symbol color) -> Decorator
247
-
248
- # Apply a style to the text
249
- #
250
- # @author {https://aaronmallen.me Aaron Allen}
251
- # @since 0.1.0
252
- #
253
- # @api private
254
- #
255
- # @param style [String, Symbol] the style to apply
256
- #
257
- # @raise [ArgumentError] if the style is invalid
258
- # @return [Decorator] a new instance of Decorator with the style applied
259
- # @rbs (String | Symbol style) -> self
260
- def apply_style: (String | Symbol style) -> self
261
-
262
85
  # Check if text should be decorated
263
86
  #
264
87
  # @author {https://aaronmallen.me Aaron Allen}
@@ -13,7 +13,7 @@ module Sai
13
13
  # @author {https://aaronmallen.me Aaron Allen}
14
14
  # @since 0.2.0
15
15
  #
16
- # @api private
16
+ # @api public
17
17
  #
18
18
  # @example
19
19
  # Sai.mode.advanced #=> 3
@@ -46,31 +46,31 @@ module Sai
46
46
  # @api public
47
47
  #
48
48
  # @example With color support enabled
49
- # ENV['COLORTERM'] #=> nil
50
- # ENV['TERM'] #=> 'xterm-256color'
51
- # Sai.mode.ansi_auto #=> 3
52
- # Sai.mode.four_bit_auto #=> 3
53
- # Sai.mode.color_16_auto #=> 3
49
+ # ENV['COLORTERM'] #=> nil
50
+ # ENV['TERM'] #=> 'xterm-256color'
51
+ # Sai.mode.advanced_auto #=> 3
52
+ # Sai.mode.eight_bit_auto #=> 3
53
+ # Sai.mode.color256_auto #=> 3
54
54
  #
55
55
  # @example With only 4-bit color support
56
- # ENV['NO_COLOR'] #=> nil
57
- # ENV['TERM'] #=> 'ansi'
58
- # Sai.mode.ansi_auto #=> 2
59
- # Sai.mode.four_bit_auto #=> 2
60
- # Sai.mode.color_16_auto #=> 2
56
+ # ENV['NO_COLOR'] #=> nil
57
+ # ENV['TERM'] #=> 'ansi'
58
+ # Sai.mode.advanced_auto #=> 2
59
+ # Sai.mode.eight_bit_auto #=> 2
60
+ # Sai.mode.color256_auto #=> 2
61
61
  #
62
62
  # @example With only 3-bit color support
63
63
  # ENV['TERM'] #=> nil
64
64
  # ENV['NO_COLOR'] #=> nil
65
- # Sai.mode.ansi_auto #=> 1
66
- # Sai.mode.four_bit_auto #=> 1
67
- # Sai.mode.color16_auto #=> 1
65
+ # Sai.mode.advanced_auto #=> 1
66
+ # Sai.mode.eight_bit_auto #=> 1
67
+ # Sai.mode.color256_auto #=> 1
68
68
  #
69
69
  # @example With color support disabled
70
70
  # ENV['NO_COLOR'] #=> 'true'
71
- # Sai.mode.ansi_auto #=> 0
72
- # Sai.mode.four_bit_auto #=> 0
73
- # Sai.mode.color16_auto #=> 0
71
+ # Sai.mode.advanced_auto #=> 0
72
+ # Sai.mode.eight_bit_auto #=> 0
73
+ # Sai.mode.color256_auto #=> 0
74
74
  #
75
75
  # @return [Integer] the color mode
76
76
  # @rbs () -> Integer
@@ -128,7 +128,7 @@ module Sai
128
128
  # ENV['TERM'] #=> 'ansi'
129
129
  # Sai.mode.ansi_auto #=> 2
130
130
  # Sai.mode.four_bit_auto #=> 2
131
- # Sai.mode.color_16_auto #=> 2
131
+ # Sai.mode.color16_auto #=> 2
132
132
  #
133
133
  # @example With only 3-bit color support
134
134
  # ENV['TERM'] #=> nil
@@ -295,7 +295,7 @@ module Sai
295
295
  # @author {https://aaronmallen.me Aaron Allen}
296
296
  # @since 0.2.0
297
297
  #
298
- # @api private
298
+ # @api public
299
299
  #
300
300
  # @example
301
301
  # Sai.mode.true_color #=> 4
@@ -0,0 +1,65 @@
1
+ # Generated from lib/sai/named_colors.rb with RBS::Inline
2
+
3
+ module Sai
4
+ # A collection of named colors and their RGB values
5
+ #
6
+ # @author {https://aaronmallen.me Aaron Allen}
7
+ # @since 0.3.1
8
+ #
9
+ # @api private
10
+ module NamedColors
11
+ # Standard ANSI color names and their RGB values
12
+ #
13
+ # @author {https://aaronmallen.me Aaron Allen}
14
+ # @since 0.3.1
15
+ #
16
+ # @api private
17
+ #
18
+ # @return [Hash{Symbol => Array<Integer>}] the color names and RGB values
19
+ ANSI: untyped
20
+
21
+ # CSS color names and their RGB values
22
+ #
23
+ # @author {https://aaronmallen.me Aaron Allen}
24
+ # @since 0.3.1
25
+ #
26
+ # @api private
27
+ #
28
+ # @return [Hash{Symbol => Array<Integer>}] the color names and RGB values
29
+ CSS: Hash[Symbol, Array[Integer]]
30
+
31
+ # XTERM color names and their RGB values
32
+ #
33
+ # @author {https://aaronmallen.me Aaron Allen}
34
+ # @since 0.3.1
35
+ #
36
+ # @api private
37
+ #
38
+ # @return [Hash{Symbol => Array<Integer>}] the color names and RGB values
39
+ # rubocop:disable Naming/VariableNumber
40
+ XTERM: Hash[Symbol, Array[Integer]]
41
+
42
+ # Look up an RGB value by color name
43
+ #
44
+ # @author {https://aaronmallen.me Aaron Allen}
45
+ # @since 0.3.1
46
+ #
47
+ # @api private
48
+ #
49
+ # @param name [String, Symbol] the color name
50
+ #
51
+ # @return [Array<Integer>] the RGB value
52
+ # @rbs (String | Symbol name) -> Array[Integer]?
53
+ def self.[]: (String | Symbol name) -> Array[Integer]?
54
+
55
+ # Get a list of all color names
56
+ #
57
+ # @author {https://aaronmallen.me Aaron Allen}
58
+ # @since 0.3.1
59
+ #
60
+ # @api private
61
+ #
62
+ # @return [Array<Symbol>] the color names
63
+ def self.names: () -> untyped
64
+ end
65
+ end