mp-utils 0.3.0 → 0.4.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: 2f99dcd4e1535b4b1777fd10c0659634b4687ac24c47ba7a0c2c967f77beb9a3
4
- data.tar.gz: c2af79547fa099dba14ffdadb49bcf5892a30fab76f02f98aff3aa048a256336
3
+ metadata.gz: bd4e848d53092d07f0ffe66865c1dd718fec86eac82143a3c487e7163755d86a
4
+ data.tar.gz: cd14a3a2876dd20e67591a3da7f1da71ffb79ce6267647a6453884e47724b87e
5
5
  SHA512:
6
- metadata.gz: 0c35d7a60dcf86f0c908c2b9f2380ae4bb41ff5e73d1e42315f919646c77f1471b43ecf679aaece84182922cef639c21f75ce88320f60a94a70ce126b2ddf540
7
- data.tar.gz: ff1a0de07a11334ac444eaf9328c482d45b8b7271dd13e9df622fc4528753cf5c0d1a0e556eaab50ee458e24118d3c6ae1a3bb8bf76ab97318c4effb9a4287f4
6
+ metadata.gz: c43bbb206cd8778bfef333f1ac572b8d1cb5daadf2bda9c179849aaf8d39e2b77fb7507a165e57308e2b78e6ef8c77bb3025a502011a11a4f32b81a9ed88298c
7
+ data.tar.gz: 4dd356d68a6304642c639ecc8f82a6e5da7583876d507e726425fcec4fa80cf5b062047412aac2637e07b82a2d0dc6c1678332465eb377f10243a29d779ab9c2
data/lib/mp_utils.rb CHANGED
@@ -7,4 +7,5 @@ require_relative 'utils/message'
7
7
  require_relative 'utils/question'
8
8
  require_relative 'utils/version_manager'
9
9
  require_relative 'utils/ansi_style_manager'
10
+ require_relative 'utils/directory_structure'
10
11
  require_relative 'resources/path_helper'
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # The Resources module manages and provides access to file system paths used by a script or application.
4
+ #
4
5
  # It facilitates the definition and retrieval of a default library path, alongside a customizable path
5
- # that can be set at runtime either programmatically using the {define} method or through the environment
6
- # variable "SCRIPT_CUSTOM_RESOURCES". This flexibility allows applications to dynamically access resources
7
- # stored in various locations, depending on execution environment or user-defined settings.
6
+ # that can be set at runtime either programmatically using the {define} method or through the
7
+ # environment variable "SCRIPT_CUSTOM_RESOURCES".
8
+ #
9
+ # This flexibility allows applications to dynamically access resources stored in various locations,
10
+ # depending on execution environment or user-defined settings.
8
11
  module Resources
9
12
  # Retrieves the path to the directory containing this module, which serves as the default library path.
10
13
  #
@@ -4,11 +4,81 @@ require_relative 'constants'
4
4
  require_relative 'ansi'
5
5
 
6
6
  # The ANSIStyleManager class is responsible for managing and applying ANSI styles to a given string.
7
- # It can replace tokens in the string with corresponding ANSI codes for colors and effects.
8
7
  #
9
- # @example
10
- # manager = ANSIStyleManager.new("Hello <color:red>World</color>")
11
- # puts manager.to_s # => "Hello \e[31mWorld\e[39m"
8
+ # It can replace tokens in the string with corresponding ANSI codes for colors, backgrounds and effects.
9
+ #
10
+ # ## Effects
11
+ # Effects can be used by adding `<effect:effect_name>` at the beginning and `</effect>` at the end of
12
+ # the sequence of characters to which you want the effect to be applied.
13
+ #
14
+ # Example:
15
+ # ```ruby
16
+ # manager = ANSIStyleManager.new('The text <effect:bold>Potato</effect> will be bold.')
17
+ # puts manager
18
+ # ```
19
+ # Output:
20
+ #
21
+ # ![bold output](./.resources/images/bold_effect_example.png)
22
+ #
23
+ # Below is the table with all available effects:
24
+ #
25
+ # | Effect Name | Description |
26
+ # |---------------|----------------------------|
27
+ # | bold | set bold mode. |
28
+ # | faint | set dim/faint mode. |
29
+ # | italic | set italic mode. |
30
+ # | underline | set underline mode. |
31
+ # | blinking | set blinking mode. |
32
+ # | inverse | set inverse/reverse mode. |
33
+ # | hidden | set hidden/invisible mode. |
34
+ # | strike | set strikethrough mode. |
35
+ # | plain | set double underline mode. |
36
+ #
37
+ # > **Note:** Some terminals may not support some of the effects listed above.
38
+ #
39
+ # ## Colors
40
+ # The ANSIStyleManager supports 3 types of coloring: Named Colors, RGB Colors, or 256 Colors.
41
+ #
42
+ # The foreground color can be changed by adding `<color:color_type>` at the beginning and `</color>`
43
+ # at the end of the character sequence you want to color.
44
+ #
45
+ # Example:
46
+ # ```ruby
47
+ # text = 'A <color:green>colorful <color:red>world <color:yellow>is <color:blue>much '
48
+ # text += '</color>more </color>beautiful</color>!</color> ;)'
49
+ # manager = ANSIStyleManager.new(text)
50
+ # puts manager
51
+ # ```
52
+ # Output:
53
+ #
54
+ # ![colored output](./.resources/images/foreground_colored_example.png)
55
+ #
56
+ # It is also possible to set the background color of a text.
57
+ #
58
+ # The background color can be changed by adding `<color:color_type:color_type>` at the
59
+ # beginning and `</color>` at the end of the character sequence you want to color.
60
+ #
61
+ # Example:
62
+ # ```ruby
63
+ # text = 'A <color:green:white>colorful <color:196>world <color:yellow:111>is <color:blue:255;255;255>much '
64
+ # text += '</color>more </color>beautiful</color>!</color> ;)'
65
+ # manager = ANSIStyleManager.new(text)
66
+ # puts manager
67
+ # ```
68
+ #
69
+ # Output:
70
+ #
71
+ # ![colored output](./.resources/images/background_colored_exemple.png)
72
+ #
73
+ # Below is the table with all available color type patterns:
74
+ #
75
+ # |Color Type |Pattern |Description |
76
+ # |-------------|--------|---------------------------------------------------------------------------------------------|
77
+ # |Reset colors |reset |Resets to the terminal's default color. |
78
+ # |256 Colors |number |Accepts numbers between 0 and 255. |
79
+ # |RGB Colors |R;G;B |R, G, and B accept values between 0 and 255. |
80
+ # |Named Colors |name |Accepts the following color names: black, red, green, yellow, blue, magenta, cyan and white. |
81
+ #
12
82
  class ANSIStyleManager
13
83
  # @return [String] the string to which ANSI styles will be applied.
14
84
  attr_reader :string
@@ -0,0 +1,215 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ # Class to represent and process a directory structure.
6
+ # It can be initialized with a hash or a YAML file.
7
+ #
8
+ # ## Using HASH
9
+ #
10
+ # The following example shows how to use DirectoryStructure with a hash.
11
+ # ```ruby
12
+ # hash = {
13
+ # 'lib' => {
14
+ # 'version.rb' => nil,
15
+ # 'source' => [
16
+ # 'file_1.rb',
17
+ # 'file_2.rb',
18
+ # 'file_3.rb'
19
+ # ],
20
+ # 'resource' => {
21
+ # 'images' => [
22
+ # 'potato.png',
23
+ # 'fries.jpeg',
24
+ # 'franch_fries.png'
25
+ # ],
26
+ # 'scripts' => {
27
+ # validation: 'teste.rb',
28
+ # sorting: [
29
+ # 'shellsort.rb',
30
+ # 'quicksort.rb'
31
+ # ],
32
+ # 'build.rb' => nil
33
+ # }
34
+ # },
35
+ # generated: 'file.generated.rb'
36
+ # }
37
+ # }
38
+ # directory_structure = DirectoryStructure.new(hash)
39
+ # puts "That is my directory structure:\n#{directory_structure}"
40
+ # # Output:
41
+ # # That is my directory structure:
42
+ # # lib
43
+ # # ╠═ version.rb
44
+ # # ╠═ source
45
+ # # ║ ╠═ file_1.rb
46
+ # # ║ ╠═ file_2.rb
47
+ # # ║ ╚═ file_3.rb
48
+ # # ╠═ resource
49
+ # # ║ ╠═ images
50
+ # # ║ ║ ╠═ potato.png
51
+ # # ║ ║ ╠═ fries.jpeg
52
+ # # ║ ║ ╚═ franch_fries.png
53
+ # # ║ ╚═ scripts
54
+ # # ║ ╠═ validation
55
+ # # ║ ║ ╚═ teste.rb
56
+ # # ║ ╠═ sorting
57
+ # # ║ ║ ╠═ shellsort.rb
58
+ # # ║ ║ ╚═ quicksort.rb
59
+ # # ║ ╚═ build.rb
60
+ # # ╚═ generated
61
+ # # ╚═ file.generated.rb
62
+ # ```
63
+ #
64
+ # ## Using a YAML/YML File
65
+ #
66
+ # First, you need to create a yml file.
67
+ #
68
+ # Example:
69
+ # ```yml
70
+ # lib:
71
+ # version.rb:
72
+ # source:
73
+ # - file_1.rb
74
+ # - file_2.rb
75
+ # - file_3.rb
76
+ # resource:
77
+ # images:
78
+ # - potato.png
79
+ # - fries.jpeg
80
+ # - franch_fries.png
81
+ # scripts:
82
+ # validation: teste.rb
83
+ # sorting:
84
+ # - shellsort.rb
85
+ # - quicksort.rb
86
+ # build.rb:
87
+ # generated: file.generated.rb
88
+ # ```
89
+ #
90
+ # Considering that the file path is passed as a reference.
91
+ #
92
+ # You can initialize the class as in the example below:
93
+ #
94
+ # ```ruby
95
+ # path = 'Replace/By/Your/YAML/FILE/PATH'
96
+ # directory_structure = DirectoryStructure.new(path)
97
+ # puts "That is my directory structure:\n#{directory_structure}"
98
+ # # Output:
99
+ # # That is my directory structure:
100
+ # # lib
101
+ # # ╠═ version.rb
102
+ # # ╠═ source
103
+ # # ║ ╠═ file_1.rb
104
+ # # ║ ╠═ file_2.rb
105
+ # # ║ ╚═ file_3.rb
106
+ # # ╠═ resource
107
+ # # ║ ╠═ images
108
+ # # ║ ║ ╠═ potato.png
109
+ # # ║ ║ ╠═ fries.jpeg
110
+ # # ║ ║ ╚═ franch_fries.png
111
+ # # ║ ╚═ scripts
112
+ # # ║ ╠═ validation
113
+ # # ║ ║ ╚═ teste.rb
114
+ # # ║ ╠═ sorting
115
+ # # ║ ║ ╠═ shellsort.rb
116
+ # # ║ ║ ╚═ quicksort.rb
117
+ # # ║ ╚═ build.rb
118
+ # # ╚═ generated
119
+ # # ╚═ file.generated.rb
120
+ # ```
121
+ class DirectoryStructure
122
+ # Initializes the DirectoryStructure object.
123
+ #
124
+ # @param content [Hash, String] A hash representing the directory structure or a path to a YAML file.
125
+ # @raise [RuntimeError] If the content is not a Hash or a valid YAML file path.
126
+ def initialize(content)
127
+ if content.is_a?(Hash)
128
+ @dir_hash = content
129
+ elsif File.exist?(content)
130
+ @dir_hash = YAML.load_file(content)
131
+ else
132
+ raise 'Need be initialized with a Hash or yaml file path'
133
+ end
134
+ end
135
+
136
+ # Converts the directory structure to a string representation.
137
+ #
138
+ # @return [String] The string representation of the directory structure.
139
+ def to_s
140
+ @output = String.new('')
141
+ process_node(@dir_hash)
142
+ @output
143
+ end
144
+
145
+ private
146
+
147
+ # Generates the indentation string based on the key.
148
+ #
149
+ # @param key [Symbol] The key indicating the type of indentation.
150
+ # @return [String] The indentation string.
151
+ def generate_indent(key)
152
+ { middle: '║ ', last: ' ' }[key] || ''
153
+ end
154
+
155
+ # Concatenates the node to the output string with the given prefix and indentation.
156
+ #
157
+ # @param node [String] The node to be concatenated.
158
+ # @param prefix [Symbol] The prefix indicating the type of node.
159
+ # @param indent [Array<Symbol>] The array of indentation keys.
160
+ # @return [void]
161
+ def concant(node, prefix:, indent:)
162
+ prefix_string = { middle: '╠═ ', last: '╚═ ' }[prefix] || ''
163
+ indent_string = indent.map { |i| generate_indent(i) }.join
164
+ @output << "#{indent_string}#{prefix_string}#{node}\n"
165
+ end
166
+
167
+ # Processes an array of nodes and concatenates them to the output string.
168
+ #
169
+ # @param array [Array] The array of nodes to be processed.
170
+ # @param indent [Array<Symbol>] The array of indentation keys.
171
+ # @return [void]
172
+ def process_array(array, indent:)
173
+ array.each_with_index do |item, index|
174
+ prefix = index == array.size - 1 ? :last : :middle
175
+ concant(item, prefix: prefix, indent: indent)
176
+ end
177
+ end
178
+
179
+ # Processes a hash of nodes and concatenates them to the output string.
180
+ #
181
+ # @param hash [Hash] The hash of nodes to be processed.
182
+ # @param level [Integer] The current level of indentation.
183
+ # @param indent [Array<Symbol>] The array of indentation keys.
184
+ # @return [void]
185
+ def process_hash(hash, level:, indent:)
186
+ hash.each_with_index do |(key, value), index|
187
+ new_indent = indent.dup
188
+ prefix = :empty
189
+
190
+ if level > new_indent.size
191
+ prefix = index == hash.size - 1 ? :last : :middle
192
+ new_indent = indent + [prefix]
193
+ end
194
+
195
+ concant(key, prefix: prefix, indent: indent)
196
+ process_node(value, indent: new_indent, level: level)
197
+ end
198
+ end
199
+
200
+ # Processes a node (hash, array, or other) and concatenates it to the output string.
201
+ #
202
+ # @param node [Object] The node to be processed.
203
+ # @param level [Integer] The current level of indentation.
204
+ # @param indent [Array<Symbol>] The array of indentation keys.
205
+ # @return [void]
206
+ def process_node(node, level: 0, indent: [''])
207
+ if node.is_a?(Hash)
208
+ process_hash(node, indent: indent, level: level + 1)
209
+ elsif node.is_a?(Array)
210
+ process_array(node, indent: indent)
211
+ elsif !node.nil?
212
+ concant(node, prefix: :last, indent: indent)
213
+ end
214
+ end
215
+ end
data/lib/utils/key.rb CHANGED
@@ -45,10 +45,7 @@ class Key
45
45
  def to_regexp
46
46
  /#{Regexp.escape(to_s)}/
47
47
  end
48
- end
49
48
 
50
- # Static Methods
51
- class Key
52
49
  # Finds and returns all Key instances within the given string.
53
50
  #
54
51
  # @param value [#to_s] the string to search for keys.
data/lib/utils/message.rb CHANGED
@@ -4,11 +4,15 @@ require_relative File.join('..', 'resources', 'path_helper')
4
4
  require_relative 'key'
5
5
 
6
6
  # The Message class represents a mechanism for dynamically handling and formatting messages.
7
+ #
7
8
  # It supports the substitution of placeholders within a message template with actual data.
8
- # The class leverages file-based message templates, allowing for easy localization or customization
9
- # of messages. It integrates seamlessly with the Resources module to access these templates from
10
- # a customizable path set via the "SCRIPT_CUSTOM_RESOURCES" environment variable or from a default
11
- # library path.
9
+ #
10
+ # The class leverages file-based message templates, allowing for easy localization or
11
+ # customization of messages.
12
+ #
13
+ # It integrates seamlessly with the Resources module to access these templates from
14
+ # a customizable path set via the "SCRIPT_CUSTOM_RESOURCES" environment variable or
15
+ # from a default library path.
12
16
  #
13
17
  # @example Creating a new Message instance and formatting it
14
18
  # # Assuming "hellow_world" is a file that says "Hello, world!"
@@ -46,10 +50,7 @@ class Message
46
50
  new_message = replace_message_keys(String.new(@message))
47
51
  replace_all_to_replace_elements(new_message)
48
52
  end
49
- end
50
53
 
51
- # Private Methods
52
- class Message
53
54
  private
54
55
 
55
56
  # Determines the custom path for message files, if set through the "SCRIPT_CUSTOM_RESOURCES" environment variable.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mp-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcio F Paludo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-30 00:00:00.000000000 Z
11
+ date: 2024-07-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Helpers to facilitate scripts Writing.
@@ -32,6 +32,7 @@ files:
32
32
  - lib/utils/ansi_style_manager.rb
33
33
  - lib/utils/array.rb
34
34
  - lib/utils/constants.rb
35
+ - lib/utils/directory_structure.rb
35
36
  - lib/utils/key.rb
36
37
  - lib/utils/message.rb
37
38
  - lib/utils/question.rb
@@ -42,6 +43,7 @@ licenses:
42
43
  metadata:
43
44
  homepage_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
44
45
  source_code_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
46
+ documentation_uri: https://marciofpaludo.github.io/ruby-mp-utils
45
47
  rubygems_mfa_required: 'true'
46
48
  post_install_message:
47
49
  rdoc_options: []