Class: Dex::UI::Formatter
- Inherits:
-
Object
- Object
- Dex::UI::Formatter
- Defined in:
- lib/dex/ui/formatter.rb
Defined Under Namespace
Classes: FormatError
Constant Summary collapse
- SGR_MAP =
{ # presentational 'red' => '31', 'green' => '32', 'yellow' => '33', 'blue' => '34', 'magenta' => '35', 'cyan' => '36', 'bold' => '1', 'reset' => '0', # semantic 'error' => '31', # red 'success' => '32', # success 'warning' => '33', # yellow 'info' => '34', # blue 'command' => '36', # cyan }.freeze
- BEGIN_EXPR =
'{{'
- END_EXPR =
'}}'
- SCAN_FUNCNAME =
/\w+:/
- SCAN_GLYPH =
/.}}/
- SCAN_BODY =
/ .*? ( #{BEGIN_EXPR} | #{END_EXPR} | \z ) /mx
- DISCARD_BRACES =
0..-3
- LITERAL_BRACES =
:__literal_braces__
Instance Method Summary collapse
- #format(sgr_map = SGR_MAP, enable_color: true) ⇒ Object
-
#initialize(text) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(text) ⇒ Formatter
Returns a new instance of Formatter.
56 57 58 |
# File 'lib/dex/ui/formatter.rb', line 56 def initialize(text) @text = text end |
Instance Method Details
#format(sgr_map = SGR_MAP, enable_color: true) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dex/ui/formatter.rb', line 60 def format(sgr_map = SGR_MAP, enable_color: true) @nodes = [] stack = parse_body(StringScanner.new(@text)) prev_fmt = nil content = @nodes.each_with_object(String.new) do |(text, fmt), str| if prev_fmt != fmt && enable_color text = apply_format(text, fmt, sgr_map) end str << text prev_fmt = fmt end stack.reject! { |e| e == LITERAL_BRACES } return content unless enable_color return content if stack == prev_fmt unless stack.empty? && (@nodes.size.zero? || @nodes.last[1].empty?) content << apply_format('', stack, sgr_map) end content end |