Class: TermColor::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/term_color/rule_set.rb

Overview

TermColor style rule setc class

Author:

Defined Under Namespace

Classes: SymbolOptions

Constant Summary collapse

DBG =
false
DEFAULT_SYMBOLS =

Default rule symbols

{
  open: '{%',
  close: '%}',
  reset: '%@'
}.freeze
DEFAULT_RESET_RULE =

Default reset rule

{after: {reset: :all} }.freeze
AFTER_PRESETS =

After preset options

{
  # Full reset
  reset: {reset: :all},
  # Automatically determine what to toggle off
  auto: :auto,
  # No reset
  keep: {keep: :all}
}
DEFAULT_AFTER =

Default after preset choice

:auto

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules = {}, **opts) ⇒ RuleSet

Construct new rule set

Examples:

rules = RuleSet.new({
    # Green underlined text; will auto reset fg and disable underline
    # for close, since no z: is provided
    name: {fg: :green, enable: :underline},
    # Italic text; will auto generate z: that disables italic
    quote: { enable: :italic },
    # A weird rule that will make fg red inside rule,
    # and change fg to blue after rule block ends
    weird: { inside: { fg: :red }, after: { fg: :blue }}
})

print rules.colorize("{%nameJohn%}: '{%quoteRoses are {%weirdRed%} (blue)%}.\n")
# Result will be:
#   fg green+underline "John"
#   regular ":  "
#   italic "Roses are "
#   fg red (still italic) "Red"
#   (fg blue)(still italic) "(blue)"
#   (regular) "."

Parameters:

  • rules (Hash) (defaults to: {})

    Hash of rule names mapping to rule hashes, which can define before rules (‘inside:`), after rules (`after:`) or both.

    • If neither are given, content is treated as though it was inside a ‘inside:` key.

    • If ‘inside:` only is given, Rule evaluate method attempts to

      auto guess `after:`, resetting any used color or style rules from `inside:`
      
  • opts (Hash)

    Optional arguments

Options Hash (**opts):

  • :after (Hash|Symbol)

    Override default ‘:after` rule behavior when rule has no `after:` Options:

    • ‘:reset` - Reset all color and styles

    • ‘:auto` (default) - Try to automatically determine what to reset based on applied colors/styles

    • ‘:keep` - Keel all rule styles intact

    • (‘Hash`) - Custom rule (formatted as Rule `after` prop, e.g. `{ reset: :fg, keep: :style }`)

  • :symbols (Hash)

    Override styling symbols Options:

    • ‘:open` - Rule open symbol (used as symbolRulename) (default `

    • ‘:close` - Rule close symbol (default `%`)

    • ‘:reset` - Symbol that can be used between rule blocks to fully reset everything (default `%@`)

See Also:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/term_color/rule_set.rb', line 83

def initialize(rules={}, **opts)
    if rules.nil?
      rules = opts
      opts = {}
    end
    @base_rules = rules
    @base_rules[:reset] = @base_rules.fetch(:reset, DEFAULT_RESET_RULE)
    # binding.pry
    after = opts.fetch(:after, nil)
    after = DEFAULT_AFTER if after.nil? || (after.is_a?(Symbol) && !AFTER_PRESETS.has_key?(after))
    @default_after = (after.is_a?(Hash))? after : AFTER_PRESETS[after]
    sym_opts = opts.fetch(:symbols,{})
    @symbols = SymbolOptions.new(
      sym_opts.fetch(:open, DEFAULT_SYMBOLS[:open]),
      sym_opts.fetch(:close, DEFAULT_SYMBOLS[:close]),
      sym_opts.fetch(:reset, DEFAULT_SYMBOLS[:reset])
    )
    evaluate_rules
    build_regexs
end

Instance Attribute Details

#default_afterObject (readonly)

Returns the value of attribute default_after.



41
42
43
# File 'lib/term_color/rule_set.rb', line 41

def default_after
  @default_after
end

#regexsObject (readonly)

Returns the value of attribute regexs.



41
42
43
# File 'lib/term_color/rule_set.rb', line 41

def regexs
  @regexs
end

#rulesObject (readonly)

Returns the value of attribute rules.



41
42
43
# File 'lib/term_color/rule_set.rb', line 41

def rules
  @rules
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



41
42
43
# File 'lib/term_color/rule_set.rb', line 41

def symbols
  @symbols
end

Instance Method Details

#apply(text) ⇒ String

Apply styling to string using rule set

Parameters:

  • text (String)

    Text to parse for stylization

Returns:

  • (String)

    Text with ANSI style codes injected



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/term_color/rule_set.rb', line 108

def apply(text)
    raw = process_text(text)
    rule_stack = []
    str = ''
    rule_names = @rules.keys
    raw.each do |r|
      if r.is_a?(Symbol)
        # Part is a rule
        dprint "\tRule Symbol #{r}\n"
        if r == :close && rule_stack.length >= 1
          # Rule close with 1+ opened rules
          opened = rule_stack.pop
          opened_after = @rules[opened].codes(Rule::Parts[:after])
          dprint "\t\tClose, opened rule '#{opened}'\n"
          dprint "\t\t\tClosing rule '#{opened}' with After\n"
          dprint 4,"After: #{opened_after.inspect}\n"
          str.concat(opened_after)
          unless rule_stack.length == 0
            rule_stack.each do |outer|
              outer_inside = @rules[outer].codes(Rule::Parts[:inside])
              # Closed rule was nested in another open rule
              dprint 3, "Outer rule '#{outer}' still open. Restoring Inside\n"
              dprint 4, "Inside: #{outer_inside.inspect}\n}"
              str.concat(outer_inside)
            end
          end
            # binding.pry
            # outer = rule_stack[-1]
            # outer_inside = @rules[outer].codes(Rule::Parts[:inside])
            # # Closed rule was nested in another open rule
            # dprint 3, "Outer rule '#{outer}' still open. Restoring Inside\n"
            # dprint 4, "Inside: #{outer_inside.inspect}\n}"
            # str.concat(outer_inside)
            # # binding.pry
          # end
        elsif r == :reset && rule_stack.length == 0
          # no opened outer rules, reset symbol given
          dprint "\t\tReset, no opened rule\n"
          str.concat(@rules[r].codes(Rule::Parts[:after]))
        elsif rule_names.include?(r)
          # New rule to apply
          dprint "\t\tApplying new rule '#{r}'\n"
          dprint 3, "Previous active rule `#{rule_stack[-1]}`\n"
          rule_stack.push r
          str.concat(@rules[r].codes(Rule::Parts[:inside]))
        end
      else
        # Part is text
        str.concat(r)
      end
    end
    str
end

Wraps STDOUT print method, passing output of ‘apply` to `print`

Parameters:

  • args (Array)

    Print arguments, including TermColor style tags

  • opts (Hash)

    Optional params

Options Hash (**opts):

  • :out (IO)

    Optional override for IO class to call ‘print` on (default `$stdout`)



168
169
170
171
172
# File 'lib/term_color/rule_set.rb', line 168

def print(*args,**opts)
    stdout = opts.fetch(:out, $stdout)
    t = args.map{|a|apply(a)}
    stdout.print *t
end

#printf(format_string, *args, **opts) ⇒ Object

Wraps STDOUT printf method, passing output of ‘apply` to `print` Doesn’t actually use ‘printf`, instead passes result of `format_string % args` to `print`.

Parameters:

  • format_string (String)

    printf format string, including TermColor style tags

  • args (Array)

    printf values to use with format string

  • opts (Hash)

    Optional params

Options Hash (**opts):

  • :out (IO)

    Optional override for IO class to call ‘print` on (default `$stdout`)



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/term_color/rule_set.rb', line 184

def printf(format_string,*args,**opts)
    stdout = opts.fetch(:out, $stdout)

    # Sanitize rule symbols
    sanitized = format_string.dup
    @rules.keys.each { |k| sanitized.gsub!("#{@symbols.rule}#{k.to_s}","#{255.chr}#{k.to_s}") }
    sanitized.gsub!(@symbols.reset, 255.chr*2)

    t = sanitized % args
    # Reinstate rule symbols
    @rules.keys.each { |k| t.gsub!("#{255.chr}#{k.to_s}","#{@symbols.rule}#{k.to_s}") }
    t.gsub!(255.chr*2,@symbols.reset)

    stdout.print apply(t)
end