class Printer

Public Class Methods

log(msg) click to toggle source
# File lib/helpers/printer.rb, line 35
def log(msg)
  puts "\x1b[36m┃\x1b[0m " + msg
end
printing_width(str) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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
put_header(text, color = "\x1b[36m") click to toggle source
# File lib/helpers/printer.rb, line 12
def put_header(text, color = "\x1b[36m")
  put_edge(color, "┏━━ ", text)
end
puts_blue(a) click to toggle source
# File lib/helpers/printer.rb, line 116
def puts_blue(a)
  puts_raw("\x1b[34m┃\x1b[0m " + a)
end
puts_coloured(a, error: false) click to toggle source
# 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) click to toggle source
# File lib/helpers/printer.rb, line 112
def puts_failure(a)
  puts_red("\x1b[31m✗\x1b[0m " + a)
end
puts_green(a) click to toggle source
# File lib/helpers/printer.rb, line 120
def puts_green(a)
  puts_raw("\x1b[32m┃\x1b[0m " + a)
end
puts_info(a, mark = '?') click to toggle source
# File lib/helpers/printer.rb, line 104
def puts_info(a, mark = '?')
  puts_blue("\x1b[34m#{mark} \x1b[0m" + a)
end
puts_raw(a) click to toggle source
# File lib/helpers/printer.rb, line 128
def puts_raw(a)
  STDOUT.puts(a)
end
puts_red(a) click to toggle source
# File lib/helpers/printer.rb, line 124
def puts_red(a)
  puts_raw("\x1b[31m┃\x1b[0m " + a)
end
puts_success(a) click to toggle source
# File lib/helpers/printer.rb, line 108
def puts_success(a)
  puts_green("\x1b[32m✓\x1b[0m " + a)
end