Class: TermColor::RuleSet
- Inherits:
-
Object
- Object
- TermColor::RuleSet
- Defined in:
- lib/term_color/rule_set.rb
Overview
TermColor style rule setc class
Constant Summary collapse
- RULE_SYMBOL =
Symbol used as prefix for rule name to denote rule start
'%'- RESET_SYMBOL =
String used to denote rule close / reset
'%%'- DEFAULT_RESET_RULE =
{z: {reset: :all} }
Instance Attribute Summary collapse
-
#regexs ⇒ Object
readonly
Returns the value of attribute regexs.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
-
#apply(text) ⇒ String
Apply styling to string using rule set.
-
#initialize(rules) ⇒ 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) ⇒ RuleSet
Construct new rule set
47 48 49 50 51 52 |
# File 'lib/term_color/rule_set.rb', line 47 def initialize(rules) @base_rules = rules @base_rules[:default] = @base_rules.fetch(:default, DEFAULT_RESET_RULE) evaluate_rules build_regexs end |
Instance Attribute Details
#regexs ⇒ Object (readonly)
Returns the value of attribute regexs.
17 18 19 |
# File 'lib/term_color/rule_set.rb', line 17 def regexs @regexs end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
17 18 19 |
# File 'lib/term_color/rule_set.rb', line 17 def rules @rules end |
Instance Method Details
#apply(text) ⇒ String
Apply styling to string using rule set
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/term_color/rule_set.rb', line 58 def apply(text) raw = process_text(text) last_rule = nil str = '' raw.each do |r| if r.is_a?(Symbol) # if (r == :close_rule && !last_rule.nil?) # str.concat(Rule.codes(@rules[last_rule][:z])) # last_rule = nil # elsif r == :default # str.concat(Rule.codes(@rules[r][:z])) # last_rule = nil # else # last_rule = r # str.concat(Rule.codes(@rules[r][:a])) # end if (r == :default) && !last_rule.nil? str.concat(@rules[last_rule].codes(Rule::Parts[:after])) last_rule = nil elsif r == :default str.concat(@rules[r].codes(Rule::Parts[:after])) last_rule = nil else last_rule = r str.concat(@rules[r].codes(Rule::Parts[:inside])) end else str.concat(r) end end str end |
#print(*args, **opts) ⇒ Object
Wraps STDOUT print method, passing output of ‘apply` to `print`
97 98 99 100 101 |
# File 'lib/term_color/rule_set.rb', line 97 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`.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/term_color/rule_set.rb', line 113 def printf(format_string,*args,**opts) stdout = opts.fetch(:out, $stdout) # Sanitize rule symbols sanitized = format_string.dup @rules.keys.each { |k| sanitized.gsub!("#{RULE_SYMBOL}#{k.to_s}","#{255.chr}#{k.to_s}") } sanitized.gsub!(RESET_SYMBOL, 255.chr*2) t = sanitized % args # Reinstate rule symbols @rules.keys.each { |k| t.gsub!("#{255.chr}#{k.to_s}","#{RULE_SYMBOL}#{k.to_s}") } t.gsub!(255.chr*2,RESET_SYMBOL) stdout.print apply(t) end |