cli_class_tool 1.0.0 → 1.1.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 +4 -4
- data/CHANGELOG +6 -0
- data/README.md +24 -0
- data/lib/cli_class_tool/string.rb +95 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 645eabaadb2f9f7b6497f524dc2e847bbe6fa2b3bd0085449e7a5c6e49e6d943
|
|
4
|
+
data.tar.gz: 94cb2bfe852dce7a0f1a55a7888a813e4beea307f0a41c056c69d26f0914e4e7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 576fc426eedf2ed6578dce762fae533432cee75c1dce31d31ebf1c44b2b6c82b0a6418d74670fb5b6a6812b278c0444cc8295adb79d5fa2be4507cbbc9136d16
|
|
7
|
+
data.tar.gz: 4c6544ef6d4eecb54d5f2e7de75106e33936523d3e2bcea82ad729e0a388d9e33b193727b67637ef9901f29c23fccfa5ee9caed484043c4b2e7e8f60da7ca7db
|
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
------------------
|
|
2
|
+
1.1.0 (2026-09-09)
|
|
3
|
+
------------------
|
|
4
|
+
|
|
5
|
+
* Extend String class with support for all 16 standard/high-intensity ANSI colors and a chainable `.bold` method that merges ANSI escape sequences
|
|
6
|
+
|
|
1
7
|
------------------
|
|
2
8
|
1.0.0 (2026-09-09)
|
|
3
9
|
------------------
|
data/README.md
CHANGED
|
@@ -421,4 +421,28 @@ rescue MyProjectError => e
|
|
|
421
421
|
end
|
|
422
422
|
```
|
|
423
423
|
|
|
424
|
+
---
|
|
425
|
+
|
|
426
|
+
## String Colorization & Styling Extension
|
|
427
|
+
|
|
428
|
+
`CLIClassTool` extends the core Ruby `String` class with standard ANSI escape sequence methods for terminal colorization and formatting.
|
|
429
|
+
|
|
430
|
+
### 16-Color Palette Support
|
|
431
|
+
|
|
432
|
+
The following color methods are available on any string:
|
|
433
|
+
|
|
434
|
+
* **Standard Colors**: `.black`, `.red`, `.green`, `.brown` (alias `.yellow`), `.blue`, `.magenta`, `.cyan`, `.white`
|
|
435
|
+
* **High-Intensity / Bright Colors**: `.gray` (alias `.grey`), `.light_red`, `.light_green`, `.light_yellow`, `.light_blue`, `.light_magenta`, `.light_cyan`, `.light_white`
|
|
436
|
+
|
|
437
|
+
### Bold Mode and Method Chaining
|
|
438
|
+
|
|
439
|
+
You can style text as bold using the `.bold` method. All methods support seamless chaining/merging, meaning ANSI escape sequences are parsed and merged into a single escape code (e.g., `\e[1;31m`) rather than nested redundantly.
|
|
440
|
+
|
|
441
|
+
```ruby
|
|
442
|
+
puts "Success!".green # Green text
|
|
443
|
+
puts "Warning!".brown.bold # Bold brown (yellow) text
|
|
444
|
+
puts "Fatal Error!".bold.red # Bold red text
|
|
445
|
+
puts "System Message".bold # Bold text using default terminal color
|
|
424
446
|
```
|
|
447
|
+
|
|
448
|
+
These methods automatically check if `$stdout` is a TTY and safely fall back to standard, unformatted strings if output is piped or redirected (non-TTY mode).
|
|
@@ -10,12 +10,33 @@ class String
|
|
|
10
10
|
def colorize(color_code)
|
|
11
11
|
@@is_a_tty = $stdout.isatty() if @@is_a_tty == nil
|
|
12
12
|
if @@is_a_tty then
|
|
13
|
-
|
|
13
|
+
if self =~ /\A\e\[([\d;]+)m(.*)\e\[0m\z/
|
|
14
|
+
codes = $1.split(';')
|
|
15
|
+
content = $2
|
|
16
|
+
new_codes = codes.dup
|
|
17
|
+
if color_code.to_i == 1
|
|
18
|
+
new_codes.unshift("1") unless new_codes.include?("1")
|
|
19
|
+
else
|
|
20
|
+
new_codes.reject! { |c| (30..37).include?(c.to_i) || (90..97).include?(c.to_i) }
|
|
21
|
+
new_codes << color_code.to_s
|
|
22
|
+
end
|
|
23
|
+
new_codes.uniq!
|
|
24
|
+
new_codes.sort_by! { |c| c.to_i }
|
|
25
|
+
return "\e[#{new_codes.join(';')}m#{content}\e[0m"
|
|
26
|
+
else
|
|
27
|
+
return "\e[#{color_code}m#{self}\e[0m"
|
|
28
|
+
end
|
|
14
29
|
else
|
|
15
30
|
return self
|
|
16
31
|
end
|
|
17
32
|
end
|
|
18
33
|
|
|
34
|
+
# Make the string black
|
|
35
|
+
# @return [String] Black string
|
|
36
|
+
def black
|
|
37
|
+
colorize(30)
|
|
38
|
+
end
|
|
39
|
+
|
|
19
40
|
# Make the string red
|
|
20
41
|
# @return [String] Red string
|
|
21
42
|
def red
|
|
@@ -34,6 +55,12 @@ class String
|
|
|
34
55
|
colorize(33)
|
|
35
56
|
end
|
|
36
57
|
|
|
58
|
+
# Make the string yellow
|
|
59
|
+
# @return [String] Yellow string
|
|
60
|
+
def yellow
|
|
61
|
+
colorize(33)
|
|
62
|
+
end
|
|
63
|
+
|
|
37
64
|
# Make the string blue
|
|
38
65
|
# @return [String] Blue string
|
|
39
66
|
def blue
|
|
@@ -45,4 +72,71 @@ class String
|
|
|
45
72
|
def magenta
|
|
46
73
|
colorize(35)
|
|
47
74
|
end
|
|
75
|
+
|
|
76
|
+
# Make the string cyan
|
|
77
|
+
# @return [String] Cyan string
|
|
78
|
+
def cyan
|
|
79
|
+
colorize(36)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Make the string white
|
|
83
|
+
# @return [String] White string
|
|
84
|
+
def white
|
|
85
|
+
colorize(37)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Make the string bold
|
|
89
|
+
# @return [String] Bold string
|
|
90
|
+
def bold
|
|
91
|
+
colorize(1)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Make the string gray/grey / light black
|
|
95
|
+
# @return [String] Gray string
|
|
96
|
+
def gray
|
|
97
|
+
colorize(90)
|
|
98
|
+
end
|
|
99
|
+
alias grey gray
|
|
100
|
+
|
|
101
|
+
# Make the string light red
|
|
102
|
+
# @return [String] Light red string
|
|
103
|
+
def light_red
|
|
104
|
+
colorize(91)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Make the string light green
|
|
108
|
+
# @return [String] Light green string
|
|
109
|
+
def light_green
|
|
110
|
+
colorize(92)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Make the string light yellow
|
|
114
|
+
# @return [String] Light yellow string
|
|
115
|
+
def light_yellow
|
|
116
|
+
colorize(93)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Make the string light blue
|
|
120
|
+
# @return [String] Light blue string
|
|
121
|
+
def light_blue
|
|
122
|
+
colorize(94)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Make the string light magenta
|
|
126
|
+
# @return [String] Light magenta string
|
|
127
|
+
def light_magenta
|
|
128
|
+
colorize(95)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Make the string light cyan
|
|
132
|
+
# @return [String] Light cyan string
|
|
133
|
+
def light_cyan
|
|
134
|
+
colorize(96)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Make the string light white
|
|
138
|
+
# @return [String] Light white string
|
|
139
|
+
def light_white
|
|
140
|
+
colorize(97)
|
|
141
|
+
end
|
|
48
142
|
end
|