Module: Hoodie::ANSI
Overview
ANSI Mixin Module
Standard use is to mix this module into String.
like:
"bold red".red.bold
Constant Summary collapse
- ANSI_COLORS =
Defines our ANSI color codes
{ black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 }
- ANSI_ATTRIBUTES =
Defines our ANSI attribute codes
{ normal: 0, bold: 1 }
- ANSI_REGEX =
Defines a RegEx for stripping ANSI codes from strings
/\e\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/
Instance Method Summary collapse
-
#black(string = nil, &block) ⇒ String
Sets the foreground color to black for the supplied string.
-
#blue(string = nil, &block) ⇒ String
Sets the foreground color to blue for the supplied string.
-
#bold(string = nil, &block) ⇒ String
Sets the foreground color to bold for the supplied string.
-
#build_ansi_methods(hash) ⇒ Boolean
Dynamicly constructs ANSI methods for the color methods based on the ANSI code hash passed in.
-
#cyan(string = nil, &block) ⇒ String
Sets the foreground color to cyan for the supplied string.
- #goto(x = 0, y = 0) ⇒ Object
-
#green(string = nil, &block) ⇒ String
Sets the foreground color to green for the supplied string.
-
#magenta(string = nil, &block) ⇒ String
Sets the foreground color to magenta for the supplied string.
-
#normal(string = nil, &block) ⇒ String
Sets the foreground color to normal for the supplied string.
-
#red(string = nil, &block) ⇒ String
Sets the foreground color to red for the supplied string.
- #reset(string = nil, &block) ⇒ Object
-
#uncolor(string = nil, &block) ⇒ String
Removes ANSI code sequences from a string.
-
#white(string = nil, &block) ⇒ String
Sets the foreground color to white for the supplied string.
-
#yellow(string = nil, &block) ⇒ String
Sets the foreground color to yellow for the supplied string.
Instance Method Details
#black(string = nil, &block) ⇒ String
Sets the foreground color to black for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#blue(string = nil, &block) ⇒ String
Sets the foreground color to blue for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#bold(string = nil, &block) ⇒ String
Sets the foreground color to bold for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 102
|
#build_ansi_methods(hash) ⇒ Boolean
Dynamicly constructs ANSI methods for the color methods based on the ANSI code hash passed in.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/hoodie/utils/ansi.rb', line 127 def build_ansi_methods(hash) hash.each do |key, value| define_method(key) do |string=nil, &block| result = Array.new result << %(\e[#{value}m) if block_given? result << block.call elsif string.respond_to?(:to_str) result << string.to_str elsif respond_to?(:to_str) result << to_str else return result end result << %(\e[0m) result.join end end true end |
#cyan(string = nil, &block) ⇒ String
Sets the foreground color to cyan for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#goto(x = 0, y = 0) ⇒ Object
192 193 194 |
# File 'lib/hoodie/utils/ansi.rb', line 192 def goto(x=0, y=0) %(\e[#{x};#{y}H) end |
#green(string = nil, &block) ⇒ String
Sets the foreground color to green for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#magenta(string = nil, &block) ⇒ String
Sets the foreground color to magenta for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#normal(string = nil, &block) ⇒ String
Sets the foreground color to normal for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 102
|
#red(string = nil, &block) ⇒ String
Sets the foreground color to red for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#reset(string = nil, &block) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/hoodie/utils/ansi.rb', line 175 def reset(string = nil, &block) result = Array.new result << %(\e[2J) if block_given? result << block.call elsif string.respond_to?(:to_str) result << string.to_str elsif respond_to?(:to_str) result << to_str else return result end result.join end |
#uncolor(string = nil, &block) ⇒ String
Removes ANSI code sequences from a string.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/hoodie/utils/ansi.rb', line 163 def uncolor(string = nil, &block) if block_given? block.call.to_str.gsub(ANSI_REGEX, '') elsif string.respond_to?(:to_str) string.to_str.gsub(ANSI_REGEX, '') elsif respond_to?(:to_str) to_str.gsub(ANSI_REGEX, '') else '' end end |
#white(string = nil, &block) ⇒ String
Sets the foreground color to white for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|
#yellow(string = nil, &block) ⇒ String
Sets the foreground color to yellow for the supplied string.
|
|
# File 'lib/hoodie/utils/ansi.rb', line 48
|