Class: Charming::UI::ANSICodes
- Inherits:
-
Object
- Object
- Charming::UI::ANSICodes
- Defined in:
- lib/charming/presentation/ui/ansi_codes.rb
Constant Summary collapse
- ATTRIBUTES =
{ bold: 1, faint: 2, italic: 3, underline: 4, reverse: 7, strikethrough: 9 }.freeze
- COLORS =
{ black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, bright_black: 90, bright_red: 91, bright_green: 92, bright_yellow: 93, bright_blue: 94, bright_magenta: 95, bright_cyan: 96, bright_white: 97 }.freeze
Instance Method Summary collapse
- #apply(value) ⇒ Object
- #codes ⇒ Object
-
#initialize(attributes:, foreground:, background:) ⇒ ANSICodes
constructor
A new instance of ANSICodes.
Constructor Details
#initialize(attributes:, foreground:, background:) ⇒ ANSICodes
Returns a new instance of ANSICodes.
34 35 36 37 38 |
# File 'lib/charming/presentation/ui/ansi_codes.rb', line 34 def initialize(attributes:, foreground:, background:) @attributes = attributes @foreground = foreground @background = background end |
Instance Method Details
#apply(value) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/charming/presentation/ui/ansi_codes.rb', line 46 def apply(value) return value if codes.empty? start = "\e[#{codes.join(";")}m" value.split("\n", -1).map { |line| "#{start}#{line.gsub("\e[0m", "\e[0m#{start}")}\e[0m" }.join("\n") end |
#codes ⇒ Object
40 41 42 43 44 |
# File 'lib/charming/presentation/ui/ansi_codes.rb', line 40 def codes @codes ||= attribute_codes + color_codes(@foreground, foreground: true) + color_codes(@background, foreground: false) end |