ex_aequo_base 0.1.17 → 0.3.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: 427f016fdf311c7a305b83d2cc2ef8a1199f37940195b574afb9c0459206512e
4
- data.tar.gz: 9aa8f0cd446fe43423df6ae2c672c6a6318f176c3c93eeb4f018e524952cdb2d
3
+ metadata.gz: f92a77061871fa1b02dcfee2d0d9a5342313fc5c8b149ce986837703d30419c1
4
+ data.tar.gz: c2ba2d3248de83b4bcd17cde7512a95917ef7a424bad93cdf4ff661bb65bf8b1
5
5
  SHA512:
6
- metadata.gz: c86372c80062a5009605cd1820cef0ee38c0c2524d3b373da9ef80619d0149f937976af1b98d85e973d316b8058db099e2c0eb0552136497f8a81606052e19b2
7
- data.tar.gz: a20308a7e791bc318001b7a92d965d281abd9ef7168a7890b0c8f20b8dc77c51b7ebef41ceada98d0d253e28db104dd0d657903e6a7ec7d5b76f56992de31ad2
6
+ metadata.gz: 41bf821a4a87fbdbcbd2d5197caab778b0d3efd67ec30e740a974cbd2045e81295edb5fe302bc91a4948301083492016577979b8b769f127e149996be63c2ca9
7
+ data.tar.gz: 570f9e2fdbfb7850a657cf49387d65271b281e2af1412b0d6c3a169327c54c24bf24c5b15435675f2dc68b2717d57e67c07845065617298c7b255f95273b453c
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
- require_relative 'colorize'
2
+ #
3
+ require_relative 'kernel'
4
+
3
5
  require_relative 'enumerable'
4
6
  require_relative 'fn'
5
7
  require_relative 'forwarder'
6
- require_relative 'kernel'
8
+ require_relative 'function'
7
9
  require_relative 'import_none'
8
10
  require_relative 'open_struct'
9
11
  require_relative 'regexp'
10
12
  require_relative 'string'
13
+ require_relative 'to_proc'
11
14
  # require_relative 'match_data'
12
15
  # Colorize = ExAequo::Base::Colorize
13
16
  # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -6,7 +6,7 @@ module ExAequo
6
6
  module Base
7
7
  module Fn extend self
8
8
  extend Forwarder
9
- forward_all :curry, to: Helpers
9
+ forward_all :curry_send, :curry, to: Helpers
10
10
  end
11
11
  end
12
12
  end
@@ -3,52 +3,117 @@
3
3
  module ExAequo
4
4
  module Base
5
5
  module Fn
6
- Placeholder = BasicObject.new
6
+ PlaceholderClass = Class.new(BasicObject)
7
+ Placeholder = PlaceholderClass.new
7
8
 
8
9
  module Helpers extend self
9
- def curry(rcv, *positionals, **keywords, &blk)
10
- return _send_curry(*positionals, **keywords, &blk) if rcv == Placeholder
11
-
12
- _send_to(rcv, *positionals, **keywords, &blk)
10
+ def curry_send(*positionals, **keywords, &b)
11
+ -> (*args, **kwds, &blk) do
12
+ actual = _make_actual_params(positionals, args, keywords, kwds, arity: -1)
13
+ case actual
14
+ in {actual_args: [rcv, *actual_args], actual_kwds:, complete: true, **nil}
15
+ rcv.send(*actual_args, **actual_kwds, &(blk || b))
16
+ else
17
+ # This is a kludge to fix the simplecov missing branch _error_
18
+ actual in {actual_args: [_, *] => actual_args, actual_kwds:, complete: false, **nil}
19
+ curry_send(*actual_args, **actual_kwds, &(blk || b))
20
+ end
21
+ end
13
22
  end
14
23
 
15
- private
16
- def _make_actual_params(positionals, args, keywords, kwds, offset:)
17
- as = []
18
- ks = {}
19
- n = 0
24
+ def curry(*positionals, **keywords, &blk)
25
+ raise ArgumentError, "#{__method__} needs a block" unless blk
20
26
 
21
- positionals.each do
22
- if it == Placeholder
23
- as << args[n]
24
- n += 1
27
+ -> (*args, **kwds, &b) do
28
+ _make_actual_params(positionals, args, keywords, kwds, arity: blk.arity) => {actual_args:, actual_kwds:, complete:, **nil}
29
+ if complete
30
+ blk.(*actual_args, **actual_kwds, &b)
25
31
  else
26
- as << it
32
+ raise ArgumentError, "must not provide a block if function is still a partial application" if b
33
+ curry(*actual_args, **actual_kwds, &blk)
27
34
  end
28
35
  end
36
+ end
29
37
 
30
- keywords.each do |k, v|
31
- if v == Placeholder
32
- ks[k] = args[n]
33
- n += 1
34
- end
38
+ private
39
+ def _check_for_completeness(actual_args:, actual_kwds:, arity:)
40
+ if arity < 0
41
+ _check_for_min_completeness(actual_args:, actual_kwds:, arity: arity.abs.pred)
42
+ else
43
+ _check_for_exact_completeness(actual_args:, actual_kwds:, arity:)
35
44
  end
36
- as += Array(args[positionals.length-offset..])
37
- [as, keywords.merge(ks).merge(kwds)]
38
45
  end
39
46
 
40
- def _send_curry(*positionals, **keywords, &b)
41
- -> (rcv, *args, **kwds, &blk) do
42
- _make_actual_params(positionals, args, keywords, kwds, offset: 1) => actual_pos, actual_kwds
43
- rcv.send(*actual_pos, **actual_kwds, &(blk || b))
47
+ def _check_for_exact_completeness(actual_args:, actual_kwds:, arity:)
48
+ arity = arity - 1 unless actual_kwds.empty?
49
+ _check_for_placeholders(actual_args:, actual_kwds:) => {complete:, size:, **nil}
50
+ raise ArgumentError, "too many arguments arity = #{arity} but #{size} provided" if size > arity
51
+
52
+ return {actual_args:, actual_kwds:, complete:} if size == arity
53
+
54
+ {
55
+ actual_args: actual_args + [Placeholder] * (arity - size),
56
+ actual_kwds:,
57
+ complete: false
58
+ }
59
+ end
60
+
61
+ def _check_for_min_completeness(actual_args:, actual_kwds:, arity:)
62
+ _check_for_placeholders(actual_args:, actual_kwds:) => {complete:, size:, **nil}
63
+ return {actual_args:, actual_kwds:, complete:} if size >= arity
64
+
65
+ {
66
+ actual_args: actual_args + [Placeholder] * (arity - size),
67
+ actual_kwds:,
68
+ complete: false
69
+ }
70
+ end
71
+
72
+ def _check_for_placeholders(actual_args:, actual_kwds:)
73
+ size = actual_args.size
74
+ needed_kwds = actual_kwds.values.count { it == Placeholder }
75
+ complete = actual_args.none? { it == Placeholder }
76
+
77
+ complete &&= needed_kwds.zero?
78
+ {complete:, size:}
79
+ end
80
+
81
+ def _make_actual_params(curried_args, current_args, curried_kwds, current_kwds, arity:)
82
+ _make_actual_args(curried_args, current_args) in actual_args, remaining_args
83
+ _make_actual_kwds(curried_kwds, current_kwds, remaining_args) in actual_kwds, remaining_args
84
+
85
+ actual_args += remaining_args
86
+ actual_kwds = curried_kwds.merge(actual_kwds).merge(current_kwds)
87
+ _check_for_completeness(actual_args:, actual_kwds:, arity:)
88
+ end
89
+
90
+ def _make_actual_args(curried_args, actual_args)
91
+ as = []
92
+ curried_args.each do
93
+ if actual_args.empty?
94
+ as << it
95
+ elsif it == Placeholder
96
+ as << actual_args.first
97
+ actual_args = actual_args.drop(1)
98
+ else
99
+ as << it
100
+ end
44
101
  end
102
+ [as, actual_args]
45
103
  end
46
104
 
47
- def _send_to(rcv, *positionals, **keywords, &b)
48
- -> (*args, **kwds, &blk) do
49
- _make_actual_params(positionals, args, keywords, kwds, offset: 0) => actual_pos, actual_kwds
50
- rcv.send(*actual_pos, **actual_kwds, &(blk || b))
105
+ def _make_actual_kwds(curried_kwds, actual_kwds, remaining_args)
106
+ ks = {}
107
+ curried_kwds.each do |k, v|
108
+ if v == Placeholder
109
+ case remaining_args
110
+ in [arg, *remaining_args]
111
+ ks[k] = arg
112
+ else
113
+ end
114
+ end
51
115
  end
116
+ [ks, remaining_args]
52
117
  end
53
118
  end
54
119
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'fn/curry'
3
4
  require_relative 'to_proc'
4
5
  module ExAequo
5
6
  module Base
@@ -10,6 +11,11 @@ module ExAequo
10
11
  '!' => _not,
11
12
  }
12
13
 
14
+ def apply(fn_desc=nil, &blk)
15
+ fun = mk_fun_or(fn_desc, &blk)
16
+ -> (args) { fun.(*args) }
17
+ end
18
+
13
19
  def mk_fun(fn_desc)
14
20
  case fn_desc
15
21
  in String
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExAequo
4
+ module Base
5
+ class Function
6
+ module ClassMethods
7
+ def curried(*args, **kwds, &blk)
8
+ new(Fn.curry(*args, **kwds, &blk), level: 3)
9
+ end
10
+
11
+ def sending(*args, **kwds, &blk)
12
+ new(Fn.curry_send(*args, **kwds, &blk), level: 3)
13
+ end
14
+ end
15
+ extend ClassMethods
16
+ end
17
+ end
18
+ end
19
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExAequo
4
+ module Base
5
+ class Function
6
+ module CombinatorMethods
7
+ define_method :> do |other|
8
+ self.class.new(name: "(#{name} > #{other})") do |*args, **kwds, &blk|
9
+ other.(self.(*args, **kwds, &blk))
10
+ end
11
+ end
12
+ end
13
+ include CombinatorMethods
14
+ end
15
+ end
16
+ end
17
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ex_aequo/base/fn'
4
+ require 'ex_aequo/base/function/class_methods'
5
+ require 'ex_aequo/base/function/combinator_methods'
6
+ module ExAequo
7
+ module Base
8
+ class Function
9
+ attr_reader :arity, :function, :name
10
+
11
+ def call(*args, **kwds, &blk)
12
+ function.(*args, **kwds, &blk)
13
+ rescue StandardError => e
14
+ raise(e, e.message + " in #{name}")
15
+ end
16
+
17
+ def named(name)
18
+ @name = name
19
+ self
20
+ end
21
+
22
+ def to_proc = function
23
+
24
+ def to_s = name
25
+
26
+ private
27
+ def initialize(func_desc=nil, name: nil, level: 1, &blk)
28
+ raise ArgumentError, "must not provide function description and a blk" if func_desc && blk
29
+ raise ArgumentError, "must provide either a function description or a blk" unless func_desc || blk
30
+ @function = blk ? blk : Fn.mk_fun(func_desc)
31
+ @arity = function.arity
32
+ @level = level
33
+ @name = name || _mk_name
34
+ end
35
+
36
+ def _mk_name
37
+ @name = caller[@level]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -2,8 +2,8 @@
2
2
 
3
3
  require_relative 'all'
4
4
 
5
- Colorize = ExAequo::Base::Colorize
6
5
  Forwarder = ExAequo::Base::Forwarder
6
+ Function = ExAequo::Base::Function
7
7
 
8
8
  require_relative 'fn/all'
9
9
 
@@ -39,7 +39,7 @@ module ExAequo::Base::KernelHelper extend self
39
39
  dir = check_file(file)
40
40
  wc = descend ? '/**/*.rb' : '/*.rb'
41
41
  Dir.glob(File.join(dir, wc)).each do |f|
42
- require f.sub(ExAequo::Base::RbExtensionRgx, '') unless f == exclude
42
+ require f.sub(ExAequo::Base::RbExtensionRgx, '') unless f.gsub('./', '') == exclude
43
43
  end
44
44
  end
45
45
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UnboundMethod
4
+ def to_proc
5
+ ->(rcv, *a, **k, &b) do
6
+ self.bind(rcv).(*a, **k, &b)
7
+ end
8
+ end
9
+ end
10
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ExAequo
4
4
  module Base
5
- VERSION = '0.1.17'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ex_aequo_base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -47,9 +47,6 @@ files:
47
47
  - README.md
48
48
  - lib/ex_aequo/base.rb
49
49
  - lib/ex_aequo/base/all.rb
50
- - lib/ex_aequo/base/colorize.rb
51
- - lib/ex_aequo/base/colorize/color_definitions.rb
52
- - lib/ex_aequo/base/colorize/colorizer.rb
53
50
  - lib/ex_aequo/base/constants.rb
54
51
  - lib/ex_aequo/base/enumerable.rb
55
52
  - lib/ex_aequo/base/enumerable/array.rb
@@ -62,6 +59,9 @@ files:
62
59
  - lib/ex_aequo/base/fn/curry.rb
63
60
  - lib/ex_aequo/base/fn/helpers.rb
64
61
  - lib/ex_aequo/base/forwarder.rb
62
+ - lib/ex_aequo/base/function.rb
63
+ - lib/ex_aequo/base/function/class_methods.rb
64
+ - lib/ex_aequo/base/function/combinator_methods.rb
65
65
  - lib/ex_aequo/base/import_all.rb
66
66
  - lib/ex_aequo/base/import_none.rb
67
67
  - lib/ex_aequo/base/kernel.rb
@@ -78,6 +78,7 @@ files:
78
78
  - lib/ex_aequo/base/to_proc/regexp.rb
79
79
  - lib/ex_aequo/base/to_proc/string.rb
80
80
  - lib/ex_aequo/base/to_proc/true_class.rb
81
+ - lib/ex_aequo/base/to_proc/unbound_method.rb
81
82
  - lib/ex_aequo/base/version.rb
82
83
  homepage: https://codeberg.org/lab419/rb_ex_aequo_base
83
84
  licenses:
@@ -1,513 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ANSI_COLORS = {
4
- aqua: [0, 255, 255],
5
- aquamarine1: [95, 255, 215],
6
- aquamarine3: [95, 215, 175],
7
- azure1: [240, 255, 255],
8
- bg_black: 40,
9
- bg_red: 41,
10
- bg_green: 42,
11
- bg_yellow: 43,
12
- bg_blue: 44,
13
- bg_magenta: 45,
14
- bg_cyan: 46,
15
- bg_white: 47,
16
- black: 30,
17
- blue1: [0, 0, 255],
18
- blue3: [0, 0, 175],
19
- blue: 34,
20
- blue_violet: [95, 0, 255],
21
- bold: 1,
22
- cadet_blue: [95, 175, 135],
23
- chartreuse1: [135, 255, 0],
24
- chartreuse2: [95, 255, 0],
25
- chartreuse3: [95, 175, 0],
26
- chartreuse4: [95, 135, 0],
27
- color0: [:color, 0],
28
- color1: [:color, 1],
29
- color2: [:color, 2],
30
- color3: [:color, 3],
31
- color4: [:color, 4],
32
- color5: [:color, 5],
33
- color6: [:color, 6],
34
- color7: [:color, 7],
35
- color8: [:color, 8],
36
- color9: [:color, 9],
37
- color10: [:color, 10],
38
- color11: [:color, 11],
39
- color12: [:color, 12],
40
- color13: [:color, 13],
41
- color14: [:color, 14],
42
- color15: [:color, 15],
43
- color16: [:color, 16],
44
- color17: [:color, 17],
45
- color18: [:color, 18],
46
- color19: [:color, 19],
47
- color20: [:color, 20],
48
- color21: [:color, 21],
49
- color22: [:color, 22],
50
- color23: [:color, 23],
51
- color24: [:color, 24],
52
- color25: [:color, 25],
53
- color26: [:color, 26],
54
- color27: [:color, 27],
55
- color28: [:color, 28],
56
- color29: [:color, 29],
57
- color30: [:color, 30],
58
- color31: [:color, 31],
59
- color32: [:color, 32],
60
- color33: [:color, 33],
61
- color34: [:color, 34],
62
- color35: [:color, 35],
63
- color36: [:color, 36],
64
- color37: [:color, 37],
65
- color38: [:color, 38],
66
- color39: [:color, 39],
67
- color40: [:color, 40],
68
- color41: [:color, 41],
69
- color42: [:color, 42],
70
- color43: [:color, 43],
71
- color44: [:color, 44],
72
- color45: [:color, 45],
73
- color46: [:color, 46],
74
- color47: [:color, 47],
75
- color48: [:color, 48],
76
- color49: [:color, 49],
77
- color50: [:color, 50],
78
- color51: [:color, 51],
79
- color52: [:color, 52],
80
- color53: [:color, 53],
81
- color54: [:color, 54],
82
- color55: [:color, 55],
83
- color56: [:color, 56],
84
- color57: [:color, 57],
85
- color58: [:color, 58],
86
- color59: [:color, 59],
87
- color60: [:color, 60],
88
- color61: [:color, 61],
89
- color62: [:color, 62],
90
- color63: [:color, 63],
91
- color64: [:color, 64],
92
- color65: [:color, 65],
93
- color66: [:color, 66],
94
- color67: [:color, 67],
95
- color68: [:color, 68],
96
- color69: [:color, 69],
97
- color70: [:color, 70],
98
- color71: [:color, 71],
99
- color72: [:color, 72],
100
- color73: [:color, 73],
101
- color74: [:color, 74],
102
- color75: [:color, 75],
103
- color76: [:color, 76],
104
- color77: [:color, 77],
105
- color78: [:color, 78],
106
- color79: [:color, 79],
107
- color80: [:color, 80],
108
- color81: [:color, 81],
109
- color82: [:color, 82],
110
- color83: [:color, 83],
111
- color84: [:color, 84],
112
- color85: [:color, 85],
113
- color86: [:color, 86],
114
- color87: [:color, 87],
115
- color88: [:color, 88],
116
- color89: [:color, 89],
117
- color90: [:color, 90],
118
- color91: [:color, 91],
119
- color92: [:color, 92],
120
- color93: [:color, 93],
121
- color94: [:color, 94],
122
- color95: [:color, 95],
123
- color96: [:color, 96],
124
- color97: [:color, 97],
125
- color98: [:color, 98],
126
- color99: [:color, 99],
127
- color100: [:color, 100],
128
- color101: [:color, 101],
129
- color102: [:color, 102],
130
- color103: [:color, 103],
131
- color104: [:color, 104],
132
- color105: [:color, 105],
133
- color106: [:color, 106],
134
- color107: [:color, 107],
135
- color108: [:color, 108],
136
- color109: [:color, 109],
137
- color110: [:color, 110],
138
- color111: [:color, 111],
139
- color112: [:color, 112],
140
- color113: [:color, 113],
141
- color114: [:color, 114],
142
- color115: [:color, 115],
143
- color116: [:color, 116],
144
- color117: [:color, 117],
145
- color118: [:color, 118],
146
- color119: [:color, 119],
147
- color120: [:color, 120],
148
- color121: [:color, 121],
149
- color122: [:color, 122],
150
- color123: [:color, 123],
151
- color124: [:color, 124],
152
- color125: [:color, 125],
153
- color126: [:color, 126],
154
- color127: [:color, 127],
155
- color128: [:color, 128],
156
- color129: [:color, 129],
157
- color130: [:color, 130],
158
- color131: [:color, 131],
159
- color132: [:color, 132],
160
- color133: [:color, 133],
161
- color134: [:color, 134],
162
- color135: [:color, 135],
163
- color136: [:color, 136],
164
- color137: [:color, 137],
165
- color138: [:color, 138],
166
- color139: [:color, 139],
167
- color140: [:color, 140],
168
- color141: [:color, 141],
169
- color142: [:color, 142],
170
- color143: [:color, 143],
171
- color144: [:color, 144],
172
- color145: [:color, 145],
173
- color146: [:color, 146],
174
- color147: [:color, 147],
175
- color148: [:color, 148],
176
- color149: [:color, 149],
177
- color150: [:color, 150],
178
- color151: [:color, 151],
179
- color152: [:color, 152],
180
- color153: [:color, 153],
181
- color154: [:color, 154],
182
- color155: [:color, 155],
183
- color156: [:color, 156],
184
- color157: [:color, 157],
185
- color158: [:color, 158],
186
- color159: [:color, 159],
187
- color160: [:color, 160],
188
- color161: [:color, 161],
189
- color162: [:color, 162],
190
- color163: [:color, 163],
191
- color164: [:color, 164],
192
- color165: [:color, 165],
193
- color166: [:color, 166],
194
- color167: [:color, 167],
195
- color168: [:color, 168],
196
- color169: [:color, 169],
197
- color170: [:color, 170],
198
- color171: [:color, 171],
199
- color172: [:color, 172],
200
- color173: [:color, 173],
201
- color174: [:color, 174],
202
- color175: [:color, 175],
203
- color176: [:color, 176],
204
- color177: [:color, 177],
205
- color178: [:color, 178],
206
- color179: [:color, 179],
207
- color180: [:color, 180],
208
- color181: [:color, 181],
209
- color182: [:color, 182],
210
- color183: [:color, 183],
211
- color184: [:color, 184],
212
- color185: [:color, 185],
213
- color186: [:color, 186],
214
- color187: [:color, 187],
215
- color188: [:color, 188],
216
- color189: [:color, 189],
217
- color190: [:color, 190],
218
- color191: [:color, 191],
219
- color192: [:color, 192],
220
- color193: [:color, 193],
221
- color194: [:color, 194],
222
- color195: [:color, 195],
223
- color196: [:color, 196],
224
- color197: [:color, 197],
225
- color198: [:color, 198],
226
- color199: [:color, 199],
227
- color200: [:color, 200],
228
- color201: [:color, 201],
229
- color202: [:color, 202],
230
- color203: [:color, 203],
231
- color204: [:color, 204],
232
- color205: [:color, 205],
233
- color206: [:color, 206],
234
- color207: [:color, 207],
235
- color208: [:color, 208],
236
- color209: [:color, 209],
237
- color210: [:color, 210],
238
- color211: [:color, 211],
239
- color212: [:color, 212],
240
- color213: [:color, 213],
241
- color214: [:color, 214],
242
- color215: [:color, 215],
243
- color216: [:color, 216],
244
- color217: [:color, 217],
245
- color218: [:color, 218],
246
- color219: [:color, 219],
247
- color220: [:color, 220],
248
- color221: [:color, 221],
249
- color222: [:color, 222],
250
- color223: [:color, 223],
251
- color224: [:color, 224],
252
- color225: [:color, 225],
253
- color226: [:color, 226],
254
- color227: [:color, 227],
255
- color228: [:color, 228],
256
- color229: [:color, 229],
257
- color230: [:color, 230],
258
- color231: [:color, 231],
259
- color232: [:color, 232],
260
- color233: [:color, 233],
261
- color234: [:color, 234],
262
- color235: [:color, 235],
263
- color236: [:color, 236],
264
- color237: [:color, 237],
265
- color238: [:color, 238],
266
- color239: [:color, 239],
267
- color240: [:color, 240],
268
- color241: [:color, 241],
269
- color242: [:color, 242],
270
- color243: [:color, 243],
271
- color244: [:color, 244],
272
- color245: [:color, 245],
273
- color246: [:color, 246],
274
- color247: [:color, 247],
275
- color248: [:color, 248],
276
- color249: [:color, 249],
277
- color250: [:color, 250],
278
- color251: [:color, 251],
279
- color252: [:color, 252],
280
- color253: [:color, 253],
281
- color254: [:color, 254],
282
- color255: [:color, 255],
283
- cornflower_blue: [95, 135, 255],
284
- cornsilk1: [255, 255, 215],
285
- cyan1: [0, 255, 255],
286
- cyan2: [0, 255, 215],
287
- cyan3: [0, 215, 175],
288
- cyan: 36,
289
- dark_blue: [0, 0, 135],
290
- dark_cyan: [0, 175, 135],
291
- dark_goldenrod: [175, 135, 0],
292
- dark_green: [0, 95, 0],
293
- dark_khaki: [175, 175, 95],
294
- dark_magenta: [135, 0, 135],
295
- dark_olive_green1: [215, 255, 95],
296
- dark_olive_green2: [175, 255, 95],
297
- dark_olive_green3: [135, 175, 95],
298
- dark_orange3: [175, 95, 0],
299
- dark_orange: [255, 135, 0],
300
- dark_red: [95, 0, 0],
301
- dark_sea_green1: [175, 255, 215],
302
- dark_sea_green2: [175, 215, 175],
303
- dark_sea_green3: [135, 215, 175],
304
- dark_sea_green4: [95, 135, 95],
305
- dark_sea_green: [135, 175, 135],
306
- dark_slate_gray1: [135, 255, 255],
307
- dark_slate_gray2: [95, 255, 255],
308
- dark_slate_gray3: [135, 215, 215],
309
- dark_turquoise: [0, 215, 215],
310
- dark_violet: [135, 0, 215],
311
- deep_pink1: [255, 0, 135],
312
- deep_pink2: [255, 0, 95],
313
- deep_pink3: [215, 0, 95],
314
- deep_pink4: [95, 0, 95],
315
- deep_sky_blue1: [0, 175, 255],
316
- deep_sky_blue2: [0, 175, 215],
317
- deep_sky_blue3: [0, 135, 175],
318
- deep_sky_blue4: [0, 95, 95],
319
- dim: 2,
320
- dodger_blue1: [0, 135, 255],
321
- dodger_blue2: [0, 95, 255],
322
- dodger_blue3: [0, 95, 215],
323
- fuchsia: [255, 0, 255],
324
- gold1: [255, 215, 0],
325
- gold3: [175, 175, 0],
326
- green1: [0, 255, 0],
327
- green3: [0, 175, 0],
328
- green4: [0, 135, 0],
329
- green: 32,
330
- green_yellow: [175, 255, 0],
331
- grey0: [0, 0, 0],
332
- grey100: [255, 255, 255],
333
- grey11: [28, 28, 28],
334
- grey15: [38, 38, 38],
335
- grey19: [48, 48, 48],
336
- grey23: [58, 58, 58],
337
- grey27: [68, 68, 68],
338
- grey30: [78, 78, 78],
339
- grey35: [88, 88, 88],
340
- grey37: [95, 95, 95],
341
- grey39: [98, 98, 98],
342
- grey3: [8, 8, 8],
343
- grey42: [108, 108, 108],
344
- grey46: [118, 118, 118],
345
- grey50: [128, 128, 128],
346
- grey53: [135, 135, 135],
347
- grey54: [138, 138, 138],
348
- grey58: [148, 148, 148],
349
- grey62: [158, 158, 158],
350
- grey63: [175, 135, 175],
351
- grey66: [168, 168, 168],
352
- grey69: [175, 175, 175],
353
- grey70: [178, 178, 178],
354
- grey74: [188, 188, 188],
355
- grey78: [198, 198, 198],
356
- grey7: [18, 18, 18],
357
- grey82: [208, 208, 208],
358
- grey84: [215, 215, 215],
359
- grey85: [218, 218, 218],
360
- grey89: [228, 228, 228],
361
- grey93: [238, 238, 238],
362
- grey: [128, 128, 128],
363
- honeydew2: [215, 255, 215],
364
- hot_pink2: [215, 95, 175],
365
- hot_pink3: [175, 95, 135],
366
- hot_pink: [255, 95, 175],
367
- indian_red1: [255, 95, 95],
368
- indian_red: [175, 95, 95],
369
- italic: 3,
370
- khaki1: [255, 255, 135],
371
- khaki3: [215, 215, 95],
372
- light_coral: [255, 135, 135],
373
- light_cyan1: [215, 255, 255],
374
- light_cyan3: [175, 215, 215],
375
- light_goldenrod1: [255, 255, 95],
376
- light_goldenrod2: [215, 215, 135],
377
- light_goldenrod3: [215, 175, 95],
378
- light_green: [135, 255, 95],
379
- light_pink1: [255, 175, 175],
380
- light_pink3: [215, 135, 135],
381
- light_pink4: [135, 95, 95],
382
- light_salmon1: [255, 175, 135],
383
- light_salmon3: [175, 135, 95],
384
- light_sea_green: [0, 175, 175],
385
- light_sky_blue1: [175, 215, 255],
386
- light_sky_blue3: [135, 175, 175],
387
- light_slate_blue: [135, 135, 255],
388
- light_slate_grey: [135, 135, 175],
389
- light_steel_blue1: [215, 215, 255],
390
- light_steel_blue3: [175, 175, 215],
391
- light_steel_blue: [175, 175, 255],
392
- light_yellow3: [215, 215, 175],
393
- lime: [0, 255, 0],
394
- magenta1: [255, 0, 255],
395
- magenta2: [215, 0, 255],
396
- magenta3: [175, 0, 175],
397
- magenta: 35,
398
- maroon: [128, 0, 0],
399
- medium_orchid1: [215, 95, 255],
400
- medium_orchid3: [175, 95, 175],
401
- medium_orchid: [175, 95, 215],
402
- medium_purple1: [175, 135, 255],
403
- medium_purple2: [175, 95, 255],
404
- medium_purple3: [135, 95, 175],
405
- medium_purple4: [95, 95, 135],
406
- medium_purple: [135, 135, 215],
407
- medium_spring_green: [0, 255, 175],
408
- medium_turquoise: [95, 215, 215],
409
- medium_violet_red: [175, 0, 135],
410
- misty_rose1: [255, 215, 215],
411
- misty_rose3: [215, 175, 175],
412
- navajo_white1: [255, 215, 175],
413
- navajo_white3: [175, 175, 135],
414
- navy: [0, 0, 128],
415
- navy_blue: [0, 0, 95],
416
- olive: [128, 128, 0],
417
- orange: [255, 128, 0],
418
- orange1: [255, 175, 0],
419
- orange3: [215, 135, 0],
420
- orange4: [95, 95, 0],
421
- orange_red1: [255, 95, 0],
422
- orchid1: [255, 135, 255],
423
- orchid2: [255, 135, 215],
424
- orchid: [215, 95, 215],
425
- pale_green1: [135, 255, 175],
426
- pale_green3: [95, 215, 95],
427
- pale_turquoise1: [175, 255, 255],
428
- pale_turquoise4: [95, 135, 135],
429
- pale_violet_red1: [255, 135, 175],
430
- pink1: [255, 175, 215],
431
- pink3: [215, 135, 175],
432
- plum1: [255, 175, 255],
433
- plum2: [215, 175, 255],
434
- plum3: [215, 135, 215],
435
- plum4: [135, 95, 135],
436
- purple3: [95, 0, 215],
437
- purple4: [95, 0, 135],
438
- purple: [128, 0, 128],
439
- red1: [255, 0, 0],
440
- red3: [175, 0, 0],
441
- red: 31,
442
- reset: 0,
443
- rosy_brown: [175, 135, 135],
444
- royal_blue1: [95, 95, 255],
445
- salmon1: [255, 135, 95],
446
- sandy_brown: [255, 175, 95],
447
- sea_green1: [95, 255, 135],
448
- sea_green2: [95, 255, 95],
449
- sea_green3: [95, 215, 135],
450
- silver: [192, 192, 192],
451
- sky_blue1: [135, 215, 255],
452
- sky_blue2: [135, 175, 255],
453
- sky_blue3: [95, 175, 215],
454
- slate_blue1: [135, 95, 255],
455
- slate_blue3: [95, 95, 175],
456
- spring_green1: [0, 255, 135],
457
- spring_green2: [0, 215, 135],
458
- spring_green3: [0, 175, 95],
459
- spring_green4: [0, 135, 95],
460
- steel_blue1: [95, 175, 255],
461
- steel_blue3: [95, 135, 215],
462
- steel_blue: [95, 135, 175],
463
- tan: [215, 175, 135],
464
- teal: [0, 128, 128],
465
- thistle1: [255, 215, 255],
466
- thistle3: [215, 175, 215],
467
- turquoise2: [0, 215, 255],
468
- turquoise4: [0, 135, 135],
469
- uline: 4,
470
- underline: 4,
471
- violet: [215, 135, 255],
472
- wheat1: [255, 255, 175],
473
- wheat4: [135, 135, 95],
474
- white: 37,
475
- yellow1: [255, 255, 0],
476
- yellow2: [215, 255, 0],
477
- yellow3: [175, 215, 0],
478
- yellow4: [135, 135, 0],
479
- yellow: 33
480
- }
481
-
482
- module ExAequo
483
- module Base
484
- module Colorize
485
- module ColorDefinitions extend self
486
-
487
- def get(color, no_color)
488
- color = find_color(color)
489
- if color
490
- return '' if no_color
491
- color
492
- end
493
- end
494
-
495
- private
496
- def find_color(color)
497
- case ANSI_COLORS.fetch(color.to_sym, nil)
498
- in [r, g, b]
499
- "\e[38;2;#{r};#{g};#{b}m"
500
- in [:color, col]
501
- "\e[35;5;#{col}m"
502
- in nil
503
- nil
504
- in value
505
- "\e[#{value}m"
506
- end
507
- end
508
- end
509
- end
510
-
511
- end
512
- end
513
- # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -1,154 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ExAequo
4
- module Base
5
- module Colorize
6
-
7
- module Colorizer extend self
8
-
9
- def colored(*chunks)
10
- no_color = ENV.fetch('NO_COLOR', false)
11
- chunks
12
- .flatten
13
- .map { colorize_chunk it, no_color }
14
- .join
15
- end
16
-
17
- def colorize(line, reset: false)
18
- line = line.chomp
19
- no_color = ENV.fetch('NO_COLOR', false)
20
- case parse(line.grapheme_clusters, no_color)
21
- in {ok: true, result:}
22
- {ok: true, result: join_result(result, no_color:, reset:)}
23
- in error
24
- error
25
- end
26
- end
27
-
28
- def colorize_file(file, device: $stdout, reset: false)
29
- file = File.open(file, 'r') if String === file
30
-
31
- at_exit do
32
- file.close rescue nil
33
- end
34
- colorize_lines( file.readlines(chomp: true), device:, reset: )
35
- end
36
-
37
- def colorize_lines(lines, reset: false, device: $stdout)
38
- lines = lines.split(%r{\n\r?}) if String === lines
39
- lines
40
- .each_with_index do | line, idx |
41
- case colorize(line, reset:)
42
- in {ok: true, result:}
43
- device.puts result
44
- in {ok: false, error:}
45
- $stderr.puts("ERROR in line #{idx.succ} ** #{error} **")
46
- end
47
- end
48
- end
49
-
50
- def pcolored(*chunks, device: $stderr)
51
- device.puts(colored(*chunks))
52
- end
53
-
54
- private
55
-
56
- def app(ary, ary_or_elem)
57
- case ary_or_elem
58
- when Array
59
- [*ary, *ary_or_elem]
60
- else
61
- [*ary, ary_or_elem]
62
- end
63
- end
64
-
65
- def check_color_definition(rest, no_color,color)
66
- case ColorDefinitions.get(color, no_color)
67
- in nil
68
- {ok: false, error: "undefined color #{color}"}
69
- in color
70
- {ok: true, color:}
71
- end
72
- end
73
-
74
- def colorize_chunk(chunk, no_color)
75
- case chunk
76
- in Symbol
77
- get_color!(chunk, no_color)
78
- else
79
- chunk.to_s
80
- end
81
- end
82
-
83
- def get_color!(color, no_color)
84
- case ColorDefinitions.get(color, no_color)
85
- in nil
86
- raise "Bad color definition #{color.inspect}"
87
- in color
88
- color
89
- end
90
- end
91
-
92
- def join_result(result, no_color:, reset:)
93
- app(result, reset ? get_color!(:reset, no_color) : '')
94
- .join
95
- end
96
-
97
- def parse(line, no_color, result = [])
98
- # p(line:, result:)
99
- case line
100
- in []
101
- {ok: true, result:}
102
- in ['<', '<', *rest]
103
- parse(rest, no_color, app(result, '<'))
104
- in ['$', '$', *rest]
105
- parse(rest, no_color, app(result, '$'))
106
- in ['$', *rest]
107
- parse(rest, no_color, app(result, reset(no_color)))
108
- in ['<', *rest]
109
- parse_color(rest, no_color, result)
110
- in [char, *rest]
111
- parse(rest, no_color, app(result, char))
112
- end
113
- end
114
-
115
- def parse_color(line, no_color, result, color=[])
116
- # p(line:line, result:, color:)
117
- case line
118
- in []
119
- {ok: false, error: "Incomplete Color Spec #{color.join}"}
120
- in ['>', *rest]
121
- parse_continue_after_color(rest, no_color, result, color.join)
122
- in [',', *rest]
123
- parse_next_color(rest, no_color, result, color.join)
124
- in [char, *rest]
125
- parse_color(rest, no_color, result, app(color, char))
126
- end
127
- end
128
-
129
- def parse_continue_after_color(rest, no_color, result, color)
130
- case check_color_definition(rest, no_color, color)
131
- in {ok: true, color:}
132
- parse(rest, no_color, app(result, color))
133
- in error
134
- error
135
- end
136
- end
137
-
138
- def parse_next_color(rest, no_color, result, color)
139
- case check_color_definition(rest, no_color, color)
140
- in {ok: true, color:}
141
- parse_color(rest, no_color, app(result, color))
142
- in error
143
- error
144
- end
145
- end
146
-
147
- def reset(no_color)
148
- get_color!(:reset, no_color)
149
- end
150
- end
151
- end
152
- end
153
- end
154
- # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'kernel'
4
- require_subdir __FILE__
5
- require 'forwardable'
6
- module ExAequo
7
- module Base
8
- module Colorize extend self
9
- extend Forwardable
10
-
11
- def_delegators Colorizer, :colored, :colorize, :colorize_file, :colorize_lines, :pcolored
12
-
13
- end
14
- end
15
- end
16
- # SPDX-License-Identifier: AGPL-3.0-or-later