Module: Bovem::ConsoleMethods::StyleHandling::ClassMethods
- Defined in:
- lib/bovem/console.rb
Overview
Class methods for handling styles in the terminal.
Instance Method Summary (collapse)
-
- (String) parse_style(style)
Parse a style and returns terminal codes.
-
- (String) parse_styles(styles)
Parses a set of styles and returns terminals codes.
-
- (String) replace_markers(message, plain = false)
Replaces colors markers in a string.
-
- (String|nil) replace_term_code(codes, code, modifier = 0)
Replaces a terminal code.
Instance Method Details
- (String) parse_style(style)
Parse a style and returns terminal codes.
Supported styles and colors are those in TERM_COLORS and TERM_EFFECTS.
You can also prefix colors with bg_
(like bg_red
) for background colors.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bovem/console.rb', line 29 def parse_style(style) style = style.ensure_string.strip.parameterize if style.present? Bovem::Console.replace_term_code(Bovem::TERM_EFFECTS, style, 0) || Bovem::Console.replace_term_code(Bovem::TERM_COLORS, style, 30) || Bovem::Console.replace_term_code(Bovem::TERM_COLORS, style.gsub(/^bg_/, ""), 40) || "" else "" end end |
- (String) parse_styles(styles)
Parses a set of styles and returns terminals codes.
Supported styles and colors are those in TERM_COLORS and TERM_EFFECTS.
You can also prefix colors with bg_
(like bg_red
) for background colors.
48 49 50 |
# File 'lib/bovem/console.rb', line 48 def parse_styles(styles) styles.split(/\s*[\s,-]\s*/).map { |s| parse_style(s) }.join("") end |
- (String) replace_markers(message, plain = false)
Replaces colors markers in a string.
You can specify markers by enclosing in {mark=[style]}
and {/mark}
tags.
Separate styles with spaces, dashes or commas. Nesting markers is supported.
Example:
ruby
Bovem::Console.new.replace_markers("{mark=bright bg_red}{mark=green}Hello world!{/mark}{/mark}")
# => "\e[1m\e[41m\e[32mHello world!\e[1m\e[41m\e[0m"
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/bovem/console.rb', line 80 def replace_markers(, plain = false) stack = [] .ensure_string.gsub(/((\{mark=([a-z\-_\s,]+)\})|(\{\/mark\}))/mi) do if $LAST_MATCH_INFO[1] == "{/mark}" # If it is a tag, pop from the latest opened. stack.pop plain || stack.blank? ? "" : Bovem::Console.parse_styles(stack.last) else add_style($LAST_MATCH_INFO[3], plain, stack) end end end |
- (String|nil) replace_term_code(codes, code, modifier = 0)
Replaces a terminal code.
59 60 61 62 |
# File 'lib/bovem/console.rb', line 59 def replace_term_code(codes, code, modifier = 0) sym = code.to_sym codes.include?(sym) ? "\e[#{modifier + codes[sym]}m" : nil end |