Class: Yummi::Colorizers::PatternColorizer
- Inherits:
-
Object
- Object
- Yummi::Colorizers::PatternColorizer
- Includes:
- Yummi::Colorizer
- Defined in:
- lib/yummi/colorizers.rb
Overview
A colorizer for strings that follows a pattern. This colorizer is usefull for log files.
Instance Attribute Summary collapse
-
#level ⇒ Object
writeonly
Sets the attribute level.
Instance Method Summary collapse
- #call(ctx) ⇒ Object (also: #colorize)
-
#initialize(mappings = nil) ⇒ PatternColorizer
constructor
A new instance of PatternColorizer.
-
#map(params) ⇒ Object
Maps a set of patterns to colors.
Methods included from Yummi::Colorizer
Constructor Details
#initialize(mappings = nil) ⇒ PatternColorizer
Returns a new instance of PatternColorizer.
239 240 241 242 |
# File 'lib/yummi/colorizers.rb', line 239 def initialize mappings = nil @patterns = [] map mappings if mappings end |
Instance Attribute Details
#level=(value) ⇒ Object (writeonly)
Sets the attribute level
237 238 239 |
# File 'lib/yummi/colorizers.rb', line 237 def level=(value) @level = value end |
Instance Method Details
#call(ctx) ⇒ Object Also known as: colorize
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/yummi/colorizers.rb', line 276 def call ctx ctx = Yummi::Context::new(ctx) unless ctx.is_a? Context text = ctx.value.to_s @patterns.each do |config| level = -1 config[:patterns].each do |regex, color| level += 1 return if @level and level > @level and not @last_color if regex.match(text) return match(text, config) end end end return text.colorize(@last_color) if @last_color text end |
#map(params) ⇒ Object
Maps a set of patterns to colors.
Args
A hash containing the following keys:
* prefix: a pattern prefix
* suffix: a pattern suffix
* options: option flags to use
* mode: the mode to use:
- all : colorize the entire text
- grep : colorize only the matched text
* patterns: a pattern => color hash
If a string is passed instead of a hash, a file containing a YAML configuration will be loaded. If the string doesn’t represent a file, the following patterns will be used to find it:
* $HOME/.yummi/patterns/PATTERN.yaml
* $YUMMI_GEM/yummy/patterns/PATTERN.yaml
266 267 268 269 270 271 272 273 274 |
# File 'lib/yummi/colorizers.rb', line 266 def map params if params.is_a? Array params.each { |p| map p } elsif params.is_a? String or params.is_a? Symbol map Yummi::Helpers::load_resource(params, :from => :patterns) else config params end end |