lib/dev/ui/formatter.rb in dev-ui-0.1.0 vs lib/dev/ui/formatter.rb in dev-ui-0.1.1
- old
+ new
@@ -4,10 +4,15 @@
require 'strscan'
module Dev
module UI
class Formatter
+ # Available mappings of formattings
+ # To use any of them, you can use {{<key>:<string>}}
+ # There are presentational (colours and formatters)
+ # and semantic (error, info, command) formatters available
+ #
SGR_MAP = {
# presentational
'red' => '31',
'green' => '32',
'yellow' => '33',
@@ -30,18 +35,18 @@
BEGIN_EXPR = '{{'
END_EXPR = '}}'
SCAN_FUNCNAME = /\w+:/
SCAN_GLYPH = /.}}/
- SCAN_BODY = /
+ SCAN_BODY = %r{
.*?
(
#{BEGIN_EXPR} |
#{END_EXPR} |
\z
)
- /mx
+ }mx
DISCARD_BRACES = 0..-3
LITERAL_BRACES = :__literal_braces__
@@ -53,13 +58,29 @@
@input = input
@index = index
end
end
+ # Initialize a formatter with text.
+ #
+ # ===== Attributes
+ #
+ # * +text+ - the text to format
+ #
def initialize(text)
@text = text
end
+ # Format the text using a map.
+ #
+ # ===== Attributes
+ #
+ # * +sgr_map+ - the mapping of the formattings. Defaults to +SGR_MAP+
+ #
+ # ===== Options
+ #
+ # * +:enable_color+ - enable color output? Default is true
+ #
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|