Class: AnsiPrinter
- Inherits:
- 
      Object
      
        - Object
- AnsiPrinter
 
- Defined in:
- lib/log-pretty/ansi_printer.rb
Overview
Receives a Sting of input and options that will translate to ANSI escape codes
Instance Attribute Summary collapse
- 
  
    
      #input  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute input. 
Instance Method Summary collapse
- #background_code ⇒ Object
- #background_output ⇒ Object
- #color_code ⇒ Object
- #colored_output ⇒ Object
- #escape(code) ⇒ Object
- #escapes(codes) ⇒ Object
- #format_code(fmt) ⇒ Object
- #format_codes ⇒ Object
- #formatted_output ⇒ Object
- 
  
    
      #initialize(input:, **opts)  ⇒ AnsiPrinter 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of AnsiPrinter. 
- #output ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(input:, **opts) ⇒ AnsiPrinter
Returns a new instance of AnsiPrinter.
| 7 8 9 10 11 12 13 14 | # File 'lib/log-pretty/ansi_printer.rb', line 7 def initialize(input:, **opts) opts.transform_keys!(&:to_sym) @input = input @color = opts[:color] @background = opts[:background] @formats = opts[:format] end | 
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
| 5 6 7 | # File 'lib/log-pretty/ansi_printer.rb', line 5 def input @input end | 
Instance Method Details
#background_code ⇒ Object
| 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # File 'lib/log-pretty/ansi_printer.rb', line 39 def background_code case @background when :black, 'black' 40 when :red, 'red' 41 when :green, 'green' 42 when :yellow, 'yellow' 43 when :blue, 'blue' 44 when :magenta, :purple, 'magenta', 'purple' 45 when :cyan, :teal, 'cyan', 'teal' 46 when :white, 'white' 47 else 49 end end | 
#background_output ⇒ Object
| 111 112 113 | # File 'lib/log-pretty/ansi_printer.rb', line 111 def background_output escape(background_code) + colored_output end | 
#color_code ⇒ Object
| 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # File 'lib/log-pretty/ansi_printer.rb', line 16 def color_code case @color when :black, 'black' 30 when :red, 'red' 31 when :green, 'green' 32 when :yellow, 'yellow' 33 when :blue, 'blue' 34 when :magenta, :purple, 'magenta', 'purple' 35 when :cyan, :teal, 'cyan', 'teal' 36 when :white, 'white' 37 else 39 end end | 
#colored_output ⇒ Object
| 115 116 117 | # File 'lib/log-pretty/ansi_printer.rb', line 115 def colored_output escape(color_code) + input end | 
#escape(code) ⇒ Object
| 95 96 97 | # File 'lib/log-pretty/ansi_printer.rb', line 95 def escape(code) "\e[#{code}m" end | 
#escapes(codes) ⇒ Object
| 99 100 101 | # File 'lib/log-pretty/ansi_printer.rb', line 99 def escapes(codes) codes.map { |c| escape(c) }.join end | 
#format_code(fmt) ⇒ Object
| 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # File 'lib/log-pretty/ansi_printer.rb', line 68 def format_code(fmt) case fmt when :bold, 'bold' 1 when :dim, :faint, 'dim', 'faint' 2 when :italic, 'italic' 3 when :underline, :underscore, 'underline', 'underscore' 4 when :blink, :blinking, 'blink', 'blinking' 5 when :inverse, :reverse, 'inverse', 'reverse' 7 when :hidden, :invisible, 'hidden', 'invisible' 8 when :strikethrough, 'strikethrough' 9 else 6 end end | 
#format_codes ⇒ Object
| 62 63 64 65 66 | # File 'lib/log-pretty/ansi_printer.rb', line 62 def format_codes [@formats].flatten.map do |fmt| format_code(fmt) end end | 
#formatted_output ⇒ Object
| 107 108 109 | # File 'lib/log-pretty/ansi_printer.rb', line 107 def formatted_output escapes(format_codes) + background_output end | 
#output ⇒ Object
| 91 92 93 | # File 'lib/log-pretty/ansi_printer.rb', line 91 def output formatted_output + reset end | 
#reset ⇒ Object
| 103 104 105 | # File 'lib/log-pretty/ansi_printer.rb', line 103 def reset "\e[0m" end |