ex_aequo_base 0.1.2 → 0.1.4

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: 875a596526632b322c4bec51e6c1cc44601fb35d6e3212eee2279156491fd3d3
4
- data.tar.gz: b2587bd9aa314519a1bf5661acfacdead4d36ec837d342a5a5f615f08e7d7899
3
+ metadata.gz: 2f42430967e4c2f9e06b956df55783e74a92a1e820d226b580517f21f50c00e2
4
+ data.tar.gz: fb27b96a0dcabf3730527c162013114a465c00ac4a369595c25ac17fc9d032ad
5
5
  SHA512:
6
- metadata.gz: f35274645d41f0ed31f8a302c74dd4abfc927f602e5f4e4e22229371245a3e54a1e14df8cc16f4b4b640496f17c1ae22620d416955c06d2efb228372c0e8fed3
7
- data.tar.gz: a62de5273b819ad9f3637e32cb73aa9edc8592c2a31b6dcebc8975beaa107c4a1721ed7bfd4d00e97aa6c2a9226eb1a383f1b8adebc265bf89156f3ea103c8e9
6
+ metadata.gz: 84104dce64e79d0fe8945d1aeaac2f63bbb14329933eb4e9f84aefbfae68f2b842c9c0de5c82dca1c1c1cfddc3d2060dd532f7d03001efeffe5d3625b15b8a23
7
+ data.tar.gz: ce17b03ae58f71e3b0d9c2c45d9c7e55a32b1ad7788f1c31163f4fd263879c340a1a6558495026060caf950dcf0a394eb43ccef3b1112f1164165e8b3a241343
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require_relative 'colorize'
3
+ require_relative 'kernel'
3
4
  require_relative 'open_struct'
4
5
  # require_relative 'match_data'
5
6
  Colorize = ExAequo::Base::Colorize
@@ -480,30 +480,30 @@ ANSI_COLORS = {
480
480
  }
481
481
 
482
482
  module ExAequo
483
- module Colorizer
484
- module ColorDefinitions extend self
483
+ module Base
484
+ module Colorize
485
+ module ColorDefinitions extend self
485
486
 
486
- def get(color, no_color)
487
- color = find_color(color)
488
- if color
489
- return '' if no_color
490
- color
487
+ def get(color, no_color)
488
+ color = find_color(color)
489
+ if color
490
+ return '' if no_color
491
+ color
492
+ end
491
493
  end
492
- end
493
494
 
494
- private
495
- def find_color(color)
496
- case ANSI_COLORS.fetch(color.to_sym, nil)
497
- in [r, g, b]
498
- # not yet implemented
499
- nil
500
- in [:color, col]
501
- # not yet implemented
502
- nil
503
- in nil
504
- nil
505
- in value
506
- "\e[#{value}m"
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
507
  end
508
508
  end
509
509
  end
@@ -0,0 +1,154 @@
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,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'colorizer'
3
+ require_relative 'kernel'
4
+ require_subdir __FILE__
4
5
  require 'forwardable'
5
6
  module ExAequo
6
7
  module Base
7
8
  module Colorize extend self
8
9
  extend Forwardable
9
10
 
10
- def_delegators Colorizer, :colorize, :colorize_file, :colorize_lines
11
+ def_delegators Colorizer, :colored, :colorize, :colorize_file, :colorize_lines, :pcolored
11
12
 
12
13
  end
13
14
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ExAequo
4
+ module Base
5
+ RbExtensionRgx = %r{ \. rb \z }x
6
+ end
7
+ end
8
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'constants'
4
+ module ExAequo::Base::KernelHelper extend self
5
+
6
+ def require_subdir(file, descend:, &blk)
7
+
8
+ raise ArgumentError, "must not provide positional file parameter #{file.inspect} and a block" if file && blk
9
+ raise ArgumentError, "must provide file or block" unless file || blk
10
+ if file
11
+ req_subdir_file(file, descend:)
12
+ else
13
+ req_subdir_blk(descend:, &blk)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def check_file(file)
20
+ return file if File.directory?(file)
21
+
22
+ file = file.sub(ExAequo::Base::RbExtensionRgx, '')
23
+ return file if File.directory?(file)
24
+
25
+ raise ArgumentError, "#{file} is not a directory nor does a subdirectory named after this ruby file ('.rb' removed) exist"
26
+ end
27
+
28
+ def req_subdir_blk(descend:, &blk)
29
+ subdir = blk.()
30
+ blk.source_location => src, _
31
+ if subdir
32
+ dir = File.dirname(src)
33
+ req_subdir_file(File.join(dir, subdir), descend:, exclude: src)
34
+ else
35
+ req_subdir_file(src, descend:, exclude: src)
36
+ end
37
+ end
38
+
39
+ def req_subdir_file(file, descend:, exclude: nil)
40
+ dir = check_file(file)
41
+ wc = descend ? '/**/*.rb' : '/*.rb'
42
+ Dir.glob(File.join(dir, wc)).each do |f|
43
+ require f.sub(ExAequo::Base::RbExtensionRgx, '') unless f == exclude
44
+ end
45
+ end
46
+ end
47
+
48
+ module Kernel
49
+ def require_subdir(file=nil, descend: false, &blk)
50
+ ExAequo::Base::KernelHelper.require_subdir(file, descend:, &blk)
51
+ end
52
+ end
53
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -4,7 +4,7 @@ require 'ostruct'
4
4
  require 'forwarder'
5
5
  class OpenStruct
6
6
  extend Forwarder
7
- forward_all :deconstruct_keys, :empty?, :fetch, to: :to_h
7
+ forward_all :deconstruct_keys, :empty?, :fetch, :has_key?, to: :to_h
8
8
 
9
9
  def update(hashy)
10
10
  hashy.to_h.each do |k, v|
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../colorize/colorizer'
4
+ class String
5
+ def colored(color, reset: true)
6
+ colors = [color, self, (reset ? :reset : nil)].compact
7
+ Colorize::Colorizer.colored(*colors)
8
+ end
9
+
10
+ def pcolored(color, device: $stdout) = Colorize::Colorizer.pcolored(color, self, device:)
11
+ end
12
+ # 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.2'
5
+ VERSION = '0.1.4'
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.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -63,9 +63,12 @@ files:
63
63
  - lib/ex_aequo/base/all.rb
64
64
  - lib/ex_aequo/base/colorize.rb
65
65
  - lib/ex_aequo/base/colorize/color_definitions.rb
66
- - lib/ex_aequo/base/colorizer.rb
66
+ - lib/ex_aequo/base/colorize/colorizer.rb
67
+ - lib/ex_aequo/base/constants.rb
68
+ - lib/ex_aequo/base/kernel.rb
67
69
  - lib/ex_aequo/base/match_data.rb
68
70
  - lib/ex_aequo/base/open_struct.rb
71
+ - lib/ex_aequo/base/string/colorize.rb
69
72
  - lib/ex_aequo/base/version.rb
70
73
  homepage: https://codeberg.org/lab419/rb_ex_aequo_base
71
74
  licenses:
@@ -1,130 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative './colorize/color_definitions'
4
-
5
- module ExAequo
6
- module Colorizer extend self
7
-
8
- def colorize(line, reset: false)
9
- line = line.chomp
10
- no_color = ENV.fetch('NO_COLOR', false)
11
- case parse(line.grapheme_clusters, no_color)
12
- in {ok: true, result:}
13
- {ok: true, result: join_result(result, no_color:, reset:)}
14
- in error
15
- error
16
- end
17
- end
18
-
19
- def colorize_file(file, reset: false)
20
- file = File.open(file, 'r') if String === file
21
-
22
- at_exit do
23
- file.close rescue nil
24
- end
25
- colorize_lines( file.readlines(chomp: true) )
26
- end
27
-
28
- def colorize_lines(lines, reset: false)
29
- lines = lines.split(%r{\n\r?}) if String === lines
30
- lines
31
- .each_with_index do | line, idx |
32
- case colorize(line, reset:)
33
- in {ok: true, result:}
34
- puts result
35
- in {ok: false, error:}
36
- $stderr.puts("ERROR in line #{idx.succ} ** #{error} **")
37
- end
38
- end
39
- end
40
-
41
- private
42
-
43
- def app(ary, ary_or_elem)
44
- case ary_or_elem
45
- when Array
46
- [*ary, *ary_or_elem]
47
- else
48
- [*ary, ary_or_elem]
49
- end
50
- end
51
-
52
- def check_color_definition(rest, no_color,color)
53
- case ColorDefinitions.get(color, no_color)
54
- in nil
55
- {ok: false, error: "undefined color #{color}"}
56
- in color
57
- {ok: true, color:}
58
- end
59
- end
60
-
61
- def get_color!(color, no_color)
62
- case ColorDefinitions.get(color, no_color)
63
- in nil
64
- raise "This should not have happened"
65
- in color
66
- color
67
- end
68
- end
69
-
70
- def join_result(result, no_color:, reset:)
71
- app(result, reset ? get_color!(:reset, no_color) : '')
72
- .join
73
- end
74
-
75
- def parse(line, no_color, result = [])
76
- # p(line:, result:)
77
- case line
78
- in []
79
- {ok: true, result:}
80
- in ['<', '<', *rest]
81
- parse(rest, no_color, app(result, '<'))
82
- in ['$', '$', *rest]
83
- parse(rest, no_color, app(result, '$'))
84
- in ['$', *rest]
85
- parse(rest, no_color, app(result, reset(no_color)))
86
- in ['<', *rest]
87
- parse_color(rest, no_color, result)
88
- in [char, *rest]
89
- parse(rest, no_color, app(result, char))
90
- end
91
- end
92
-
93
- def parse_color(line, no_color, result, color=[])
94
- # p(line:line, result:, color:)
95
- case line
96
- in []
97
- {ok: false, error: "Incomplete Color Spec #{color.join}"}
98
- in ['>', *rest]
99
- parse_continue_after_color(rest, no_color, result, color.join)
100
- in [',', *rest]
101
- parse_next_color(rest, no_color, result, color.join)
102
- in [char, *rest]
103
- parse_color(rest, no_color, result, app(color, char))
104
- end
105
- end
106
-
107
- def parse_continue_after_color(rest, no_color, result, color)
108
- case check_color_definition(rest, no_color, color)
109
- in {ok: true, color:}
110
- parse(rest, no_color, app(result, color))
111
- in error
112
- error
113
- end
114
- end
115
-
116
- def parse_next_color(rest, no_color, result, color)
117
- case check_color_definition(rest, no_color, color)
118
- in {ok: true, color:}
119
- parse_color(rest, no_color, app(result, color))
120
- in error
121
- error
122
- end
123
- end
124
-
125
- def reset(no_color)
126
- get_color!(:reset, no_color)
127
- end
128
- end
129
- end
130
- # SPDX-License-Identifier: AGPL-3.0-or-later