Class: AnsiPrinter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#inputObject (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_codeObject



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_outputObject



111
112
113
# File 'lib/log-pretty/ansi_printer.rb', line 111

def background_output
  escape(background_code) + colored_output
end

#color_codeObject



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_outputObject



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_codesObject



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_outputObject



107
108
109
# File 'lib/log-pretty/ansi_printer.rb', line 107

def formatted_output
  escapes(format_codes) + background_output
end

#outputObject



91
92
93
# File 'lib/log-pretty/ansi_printer.rb', line 91

def output
  formatted_output + reset
end

#resetObject



103
104
105
# File 'lib/log-pretty/ansi_printer.rb', line 103

def reset
  "\e[0m"
end