Class: KLog::LogHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/k_log/log_helper.rb

Overview

Simple console log helpers

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.progress_positionObject

Returns the value of attribute progress_position.



14
15
16
# File 'lib/k_log/log_helper.rb', line 14

def progress_position
  @progress_position
end

.progress_sectionObject

Returns the value of attribute progress_section.



15
16
17
# File 'lib/k_log/log_helper.rb', line 15

def progress_section
  @progress_section
end

Class Method Details

.bg_blue(value) ⇒ Object



158
159
160
# File 'lib/k_log/log_helper.rb', line 158

def self.bg_blue(value)
  "\033[44m#{value}\033[0m"
end

.bg_cyan(value) ⇒ Object



166
167
168
# File 'lib/k_log/log_helper.rb', line 166

def self.bg_cyan(value)
  "\033[46m#{value}\033[0m"
end

.bg_green(value) ⇒ Object



150
151
152
# File 'lib/k_log/log_helper.rb', line 150

def self.bg_green(value)
  "\033[42m#{value}\033[0m"
end

.bg_grey(value) ⇒ Object



170
171
172
# File 'lib/k_log/log_helper.rb', line 170

def self.bg_grey(value)
  "\033[47m#{value}\033[0m"
end

.bg_purple(value) ⇒ Object



162
163
164
# File 'lib/k_log/log_helper.rb', line 162

def self.bg_purple(value)
  "\033[45m#{value}\033[0m"
end

.bg_red(value) ⇒ Object



146
147
148
# File 'lib/k_log/log_helper.rb', line 146

def self.bg_red(value)
  "\033[41m#{value}\033[0m"
end

.bg_yellow(value) ⇒ Object



154
155
156
# File 'lib/k_log/log_helper.rb', line 154

def self.bg_yellow(value)
  "\033[43m#{value}\033[0m"
end

.block(messages, include_line: true, title: nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/k_log/log_helper.rb', line 96

def self.block(messages, include_line: true, title: nil)
  result = include_line ? [line] : []

  unless title.nil?
    result.push(title)
    result.push(line(70, '-'))
  end

  result.push messages if messages.is_a?(String) || messages.is_a?(Integer)

  if messages.is_a? Array
    messages.each do |message|
      result.push message
    end
  end

  result.push line if include_line

  result
end

.blue(value) ⇒ Object



130
131
132
# File 'lib/k_log/log_helper.rb', line 130

def self.blue(value)
  "\033[34m#{value}\033[0m"
end

.cyan(value) ⇒ Object



138
139
140
# File 'lib/k_log/log_helper.rb', line 138

def self.cyan(value)
  "\033[36m#{value}\033[0m"
end

.dynamic_heading(heading, size: 70, type: :heading) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/k_log/log_helper.rb', line 46

def self.dynamic_heading(heading, size: 70, type: :heading)
  return heading(heading, size)           if type == :heading
  return subheading(heading, size)        if type == :subheading
  return [section_heading(heading, size)] if %i[section_heading section].include?(type)

  []
end

.green(value) ⇒ Object



122
123
124
# File 'lib/k_log/log_helper.rb', line 122

def self.green(value)
  "\033[32m#{value}\033[0m"
end

.grey(value) ⇒ Object



142
143
144
# File 'lib/k_log/log_helper.rb', line 142

def self.grey(value)
  "\033[37m#{value}\033[0m"
end

.heading(heading, size = 70) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/k_log/log_helper.rb', line 54

def self.heading(heading, size = 70)
  line = line(size)

  [
    line,
    heading,
    line
  ]
end

.kv(key, value, key_width = 30) ⇒ Object



18
19
20
# File 'lib/k_log/log_helper.rb', line 18

def self.kv(key, value, key_width = 30)
  "#{green(key.to_s.ljust(key_width))}: #{value}"
end

.line(size = 70, character = '=') ⇒ Object



42
43
44
# File 'lib/k_log/log_helper.rb', line 42

def self.line(size = 70, character = '=')
  green(character * size)
end

.progress(pos = nil, section = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/k_log/log_helper.rb', line 22

def self.progress(pos = nil, section = nil)
  @progress_position = pos.nil? ? @progress_position : pos

  @progress_section = section unless section.nil?

  section_length = 28

  section = if @progress_section.nil?
              ' ' * section_length
            else
              " #{@progress_section.ljust(section_length - 1, ' ')}"
            end

  result = "..#{section}:#{@progress_position.to_s.rjust(4)}"

  @progress_position += 1

  result
end

.purple(value) ⇒ Object



134
135
136
# File 'lib/k_log/log_helper.rb', line 134

def self.purple(value)
  "\033[35m#{value}\033[0m"
end

.red(value) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity



118
119
120
# File 'lib/k_log/log_helper.rb', line 118

def self.red(value)
  "\033[31m#{value}\033[0m"
end

.section_heading(heading, size = 70) ⇒ Object

A section heading

example: [ I am a heading ]—————————————————-



77
78
79
80
81
82
83
84
85
# File 'lib/k_log/log_helper.rb', line 77

def self.section_heading(heading, size = 70)
  brace_open = green('[ ')
  brace_close = green(' ]')
  line_length = size - heading.length - 4
  line = line_length.positive? ? line(line_length, '-') : ''

  # It is important that you set the colour after you have calculated the size
  "#{brace_open}#{heading}#{brace_close}#{green(line)}"
end

.subheading(heading, size = 70) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/k_log/log_helper.rb', line 64

def self.subheading(heading, size = 70)
  line = line(size, '-')
  [
    line,
    heading,
    line
  ]
end

.yellow(value) ⇒ Object



126
127
128
# File 'lib/k_log/log_helper.rb', line 126

def self.yellow(value)
  "\033[33m#{value}\033[0m"
end