Class: Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/printer.rb

Class Method Summary collapse

Class Method Details

.log(msg) ⇒ Object



35
36
37
# File 'lib/helpers/printer.rb', line 35

def log(msg)
  puts "\x1b[36m┃\x1b[0m " + msg
end

.printing_width(str) ⇒ Object

ANSI escape sequences (like x1b[31m) have zero width. when calculating the padding width, we must exclude them.



41
42
43
# File 'lib/helpers/printer.rb', line 41

def printing_width(str)
  str.gsub(/\x1b\[[\d;]+[A-z]/, '').size
end

.prompt_user_with_options(question, options) ⇒ Object



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
83
84
85
86
87
88
# File 'lib/helpers/printer.rb', line 55

def prompt_user_with_options(question, options)
  require 'readline'

  log(question)
  log("Your options are:")
  options.each_with_index do |v, idx|
    log("#{idx + 1}) #{v}")
  end
  log("Choose a number between 1 and #{options.length}")

  Readline.completion_append_character = " "
  Readline.completion_proc = nil

  buf = -1
  available = (1..options.length).to_a
  until available.include?(buf.to_i)
    begin
      buf = Readline.readline("\x1b[34m┃ > \x1b[33m", true)
    rescue Interrupt
      nil
    end

    if buf.nil?
      STDERR.puts
      next
    end

    buf = buf.chomp
    buf = -1 if buf.empty?
    buf = -1 if buf.to_i.to_s != buf
  end

  options[buf.to_i - 1]
end

.put_edge(color, prefix, text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/helpers/printer.rb', line 16

def put_edge(color, prefix, text)
  ptext = "#{color}#{prefix}#{text}"
  textwidth = printing_width(ptext)

  console = IO.console
  termwidth = console ? console.winsize[1] : 80
  termwidth = 30 if termwidth < 30

  if textwidth > termwidth
    ptext = ptext[0...termwidth]
    textwidth = termwidth
  end
  padwidth = termwidth - textwidth
  pad = "" * padwidth
  formatted = "#{ptext}#{color}#{pad}\x1b[0m\n"

  puts formatted
end


45
46
47
48
49
50
51
52
53
# File 'lib/helpers/printer.rb', line 45

def put_footer(success = true, success_color = "\x1b[36m")
  if success
    put_edge(success_color, "", "")
  else
    text = "💥  Failed! Aborting! "
    put_edge("\x1b[31m", "┗━━ ", text)
    exit
  end
end

.put_header(text, color = "\x1b[36m") ⇒ Object



12
13
14
# File 'lib/helpers/printer.rb', line 12

def put_header(text, color = "\x1b[36m")
  put_edge(color, "┏━━ ", text)
end

.puts_blue(a) ⇒ Object



116
117
118
# File 'lib/helpers/printer.rb', line 116

def puts_blue(a)
  puts_raw("\x1b[34m┃\x1b[0m " + a)
end

.puts_coloured(a, error: false) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/helpers/printer.rb', line 90

def puts_coloured(a, error: false)
  text = a
    .gsub(/{{green:(.*?)}}/, "\x1b[32m\\1\x1b[0m")
    .gsub(/{{bold:(.*?)}}/,  "\x1b[1m\\1\x1b[0m")
    .gsub(/{{cyan:(.*?)}}/,  "\x1b[36m\\1\x1b[0m")
    .gsub(/{{red:(.*?)}}/, "\x1b[31m\\1\x1b[0m")

  if error
    puts_red text
  else
    puts text
  end
end

.puts_failure(a) ⇒ Object



112
113
114
# File 'lib/helpers/printer.rb', line 112

def puts_failure(a)
  puts_red("\x1b[31m✗\x1b[0m " + a)
end

.puts_green(a) ⇒ Object



120
121
122
# File 'lib/helpers/printer.rb', line 120

def puts_green(a)
  puts_raw("\x1b[32m┃\x1b[0m " + a)
end

.puts_info(a, mark = '?') ⇒ Object



104
105
106
# File 'lib/helpers/printer.rb', line 104

def puts_info(a, mark = '?')
  puts_blue("\x1b[34m#{mark} \x1b[0m" + a)
end

.puts_raw(a) ⇒ Object



128
129
130
# File 'lib/helpers/printer.rb', line 128

def puts_raw(a)
  STDOUT.puts(a)
end

.puts_red(a) ⇒ Object



124
125
126
# File 'lib/helpers/printer.rb', line 124

def puts_red(a)
  puts_raw("\x1b[31m┃\x1b[0m " + a)
end

.puts_success(a) ⇒ Object



108
109
110
# File 'lib/helpers/printer.rb', line 108

def puts_success(a)
  puts_green("\x1b[32m✓\x1b[0m " + a)
end