Class: Yay::Colourizer
- Inherits:
-
Object
- Object
- Yay::Colourizer
- Defined in:
- lib/yay/colourizer.rb
Overview
this class adds colour to a stream based on rules that have been generated by a parser
Instance Method Summary collapse
-
#colourize_pipe ⇒ Object
create a pipe between the input and output streams, applying colour rules to every line that appears.
-
#colourize_rules(rules) ⇒ Object
process the rules created by a parser and determine the actual strings we need to emit when we use this colour.
-
#initialize(rules, input, output) ⇒ Colourizer
constructor
create a colourizer using specified parser rules and input and output streams (usually stdin/stdout).
-
#line_rules ⇒ Object
get the rules that are applied to a whole line if it contains matching text of the form [[regex, colour_string],..].
-
#part_rules ⇒ Object
get the rules that are applied to matching text of the form [[regex, colour_string],..].
Constructor Details
#initialize(rules, input, output) ⇒ Colourizer
create a colourizer using specified parser rules and input and output streams (usually stdin/stdout)
9 10 11 12 13 |
# File 'lib/yay/colourizer.rb', line 9 def initialize rules, input, output colourize_rules rules @input = input @output = output end |
Instance Method Details
#colourize_pipe ⇒ Object
create a pipe between the input and output streams, applying colour rules to every line that appears. only an interrupt, end of file or exception will end this process
53 54 55 56 57 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 |
# File 'lib/yay/colourizer.rb', line 53 def colourize_pipe # this is the colour we'll use when we haven't already applied a colour # we remember the previous colour as we can colourize words within a line # that's already been coloured default_end_colour = ColourWheel::end_colour @input.each_line { |line| # track the line_rules end colour so we can return to this after each # match end_colour = default_end_colour # apply all line rules @line_rules.each { |rule| if line.match(rule[0]) line = "#{rule[1]}#{line.rstrip}#{default_end_colour}" end_colour = rule[1] # leave loop; only allow one line match per line break end } # apply all partial rules @part_rules.each { |rule| line.gsub!(rule[0], "#{rule[1]}\\0#{end_colour}") } @output.puts line } end |
#colourize_rules(rules) ⇒ Object
process the rules created by a parser and determine the actual strings we need to emit when we use this colour. this breaks parser rules up in to two categories - line rules and part rules
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/yay/colourizer.rb', line 30 def colourize_rules rules @line_rules = [] @part_rules = [] rules.each { |rule| regex = rule[0] colours = rule[1] is_line = rule[2] colour_string = ColourWheel::begin_colours(colours) if is_line @line_rules.unshift [regex, colour_string] else @part_rules.unshift [regex, colour_string] end } end |
#line_rules ⇒ Object
get the rules that are applied to a whole line if it contains matching text of the form [[regex, colour_string],..]
17 18 19 |
# File 'lib/yay/colourizer.rb', line 17 def line_rules @line_rules end |
#part_rules ⇒ Object
get the rules that are applied to matching text of the form [[regex, colour_string],..]
23 24 25 |
# File 'lib/yay/colourizer.rb', line 23 def part_rules @part_rules end |