Class: TermColor::RuleSet
- Inherits:
-
Object
- Object
- TermColor::RuleSet
- Defined in:
- lib/term_color/rule_set.rb
Overview
TermColor style rule setc class
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
-
#default_after ⇒ Object
readonly
Returns the value of attribute default_after.
-
#regexs ⇒ Object
readonly
Returns the value of attribute regexs.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#symbols ⇒ Object
readonly
Returns the value of attribute symbols.
Instance Method Summary collapse
-
#apply(text) ⇒ String
Apply styling to string using rule set.
-
#initialize(rules = {}, **opts) ⇒ RuleSet
constructor
Construct new rule set.
-
#print(*args, **opts) ⇒ Object
Wraps STDOUT print method, passing output of ‘apply` to `print`.
-
#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`.
Constructor Details
#initialize(rules = {}, **opts) ⇒ RuleSet
Construct new rule set
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_after ⇒ Object (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 |
#regexs ⇒ Object (readonly)
Returns the value of attribute regexs.
41 42 43 |
# File 'lib/term_color/rule_set.rb', line 41 def regexs @regexs end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
41 42 43 |
# File 'lib/term_color/rule_set.rb', line 41 def rules @rules end |
#symbols ⇒ Object (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
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 |
#print(*args, **opts) ⇒ Object
Wraps STDOUT print method, passing output of ‘apply` to `print`
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`.
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 |