sai 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/README.md +25 -4
- data/docs/USAGE.md +9 -16
- data/lib/sai/ansi.rb +40 -0
- data/lib/sai/conversion/rgb/color_space.rb +2 -2
- data/lib/sai/decorator/delegator.rb +79 -0
- data/lib/sai/decorator/named_colors.rb +38 -744
- data/lib/sai/decorator.rb +2 -3
- data/lib/sai/registry.rb +134 -0
- data/lib/sai.rb +28 -765
- data/sig/sai/ansi.rbs +21 -0
- data/sig/sai/decorator/delegator.rbs +95 -0
- data/sig/sai/decorator/named_colors.rbs +30 -1460
- data/sig/sai/decorator.rbs +2 -3
- data/sig/sai/{named_colors.rbs → registry.rbs} +31 -52
- data/sig/sai.rbs +14 -1519
- metadata +8 -8
- data/lib/sai/decorator/delegation.rb +0 -85
- data/lib/sai/named_colors.rb +0 -522
- data/sig/sai/decorator/delegation.rbs +0 -47
data/lib/sai/decorator.rb
CHANGED
@@ -23,11 +23,10 @@ module Sai
|
|
23
23
|
# @note For each named color, two methods are dynamically generated:
|
24
24
|
# * color_name - Applies the color to the foreground
|
25
25
|
# * on_color_name - Applies the color to the backgroundAll color methods return {Decorator}
|
26
|
-
# @see Sai::NamedColors Sai::NamedColors for available color names
|
27
26
|
#
|
28
27
|
# @example Using a named color
|
29
|
-
# decorator.
|
30
|
-
# decorator.
|
28
|
+
# decorator.blue.decorate('Hello').to_s #=> "\e[38;2;0;0;238mHello\e[0m"
|
29
|
+
# decorator.on_blue.decorate('Hello').to_s #=> "\e[48;2;0;0;238mHello\e[0m"
|
31
30
|
class Decorator
|
32
31
|
include ColorManipulations
|
33
32
|
include Gradients
|
data/lib/sai/registry.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sai
|
4
|
+
# The named color registry
|
5
|
+
#
|
6
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
7
|
+
# @since 0.4.0
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
module Registry
|
11
|
+
class << self
|
12
|
+
# Look up an RGB value by color name
|
13
|
+
#
|
14
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
15
|
+
# @since 0.3.1
|
16
|
+
#
|
17
|
+
# @api private
|
18
|
+
#
|
19
|
+
# @param name [String, Symbol] the color name
|
20
|
+
#
|
21
|
+
# @return [Array<Integer>] the RGB value
|
22
|
+
# @rbs (String | Symbol name) -> Array[Integer]?
|
23
|
+
def [](name)
|
24
|
+
lookup[name.to_sym]
|
25
|
+
end
|
26
|
+
|
27
|
+
# Get a list of all color names
|
28
|
+
#
|
29
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
30
|
+
# @since 0.3.1
|
31
|
+
#
|
32
|
+
# @api private
|
33
|
+
#
|
34
|
+
# @return [Array<Symbol>] the color names
|
35
|
+
def names
|
36
|
+
@names ||= lookup.keys.uniq.sort
|
37
|
+
end
|
38
|
+
|
39
|
+
# Register a named color with an RGB or Hexadecimal value
|
40
|
+
#
|
41
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
42
|
+
# @since 0.4.0
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
#
|
46
|
+
# @param name [String, Symbol] the name of the color being registered
|
47
|
+
# @param rgb_or_hex [Array<Integer>, String] the RGB or Hexadecimal value of the color
|
48
|
+
#
|
49
|
+
# @return [Boolean] `true` if the color was registered
|
50
|
+
# @rbs (String | Symbol name, Array[Integer] | String rgb_or_hex) -> void
|
51
|
+
def register(name, rgb_or_hex)
|
52
|
+
key = name.to_s.downcase.to_sym
|
53
|
+
rgb = Conversion::RGB.resolve(rgb_or_hex)
|
54
|
+
thread_lock.synchronize { lookup[key] = rgb }
|
55
|
+
broadcast_registration(key)
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
# Subscribe to registry changes
|
60
|
+
#
|
61
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
62
|
+
# @since 0.4.0
|
63
|
+
#
|
64
|
+
# @api private
|
65
|
+
#
|
66
|
+
# @param subscriber [Object] the subscriber
|
67
|
+
#
|
68
|
+
# @return [void]
|
69
|
+
# @rbs (Object subscriber) -> void
|
70
|
+
def subscribe(subscriber)
|
71
|
+
thread_lock.synchronize { subscribers << subscriber }
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
# Broadcast a color registration to all subscribers
|
77
|
+
#
|
78
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
79
|
+
# @since 0.4.0
|
80
|
+
#
|
81
|
+
# @api private
|
82
|
+
#
|
83
|
+
# @param color_name [Symbol] the color name
|
84
|
+
#
|
85
|
+
# @return [void]
|
86
|
+
# @rbs (Symbol name) -> void
|
87
|
+
def broadcast_registration(color_name)
|
88
|
+
subscribers.each do |subscriber|
|
89
|
+
next unless subscriber.respond_to?(:on_color_registration, true)
|
90
|
+
|
91
|
+
subscriber.send(:on_color_registration, color_name)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# The Sai named colors lookup
|
96
|
+
#
|
97
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
98
|
+
# @since 0.4.0
|
99
|
+
#
|
100
|
+
# @api private
|
101
|
+
#
|
102
|
+
# @return [Hash{Symbol => Array<Integer>}] the named colors lookup
|
103
|
+
def lookup
|
104
|
+
@lookup ||= {}
|
105
|
+
end
|
106
|
+
|
107
|
+
# The registry subscribers
|
108
|
+
#
|
109
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
110
|
+
# @since 0.4.0
|
111
|
+
#
|
112
|
+
# @api private
|
113
|
+
#
|
114
|
+
# @return [Array<Class, Module, Object>] the subscribers
|
115
|
+
# @rbs () -> Array[Class | Module | Object]
|
116
|
+
def subscribers
|
117
|
+
@subscribers ||= []
|
118
|
+
end
|
119
|
+
|
120
|
+
# A Mutex for thread safety
|
121
|
+
#
|
122
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
123
|
+
# @since 0.4.0
|
124
|
+
#
|
125
|
+
# @api private
|
126
|
+
#
|
127
|
+
# @return [Mutex] the thread lock
|
128
|
+
# @rbs () -> Mutex
|
129
|
+
def thread_lock
|
130
|
+
@thread_lock ||= Mutex.new
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|