Module: Wizard::Helpers

Included in:
Wizard, Formula, Spells::Base
Defined in:
lib/wizard/helpers.rb

Constant Summary collapse

COLORS =
{
  :black  => 30,
  :red    => 31,
  :green  => 32,
  :yellow => 33,
  :blue   => 34,
  :purple => 35,
  :cyan   => 36,
  :white  => 37,
}

Instance Method Summary collapse

Instance Method Details

#adjust(text, size = nil, delim = ".") ⇒ Object Also known as: a

Display given text adjusted to the desired length.

adjust("This", 30, "-")
adjust("is", 30, "-")
adjust("SPARTA!", 30, "-")

will produce:

This -------------------------
is ---------------------------
SPARTA! ----------------------


50
51
52
53
54
# File 'lib/wizard/helpers.rb', line 50

def adjust(text, size=nil, delim=".")
  size ||= console_width
  delims = size-text.size
  delims > 0 ? text+" "+(c(delim*(delims-1), :black)) : text 
end

#colorize(text, color = :white, bold = false) ⇒ Object Also known as: c

Colorize specified text with given color.



33
34
35
36
# File 'lib/wizard/helpers.rb', line 33

def colorize(text, color=:white, bold=false)
  color = COLORS[color] || COLORS[:white]
  return "\e[#{bold ? 1 : 0};#{color}m#{text}\e[0m"
end

#console_widthObject

Returns actual width of console line.



58
59
60
# File 'lib/wizard/helpers.rb', line 58

def console_width
  `stty size`.split.last.to_i
end

Writes given text to $stdout.



21
22
23
24
25
# File 'lib/wizard/helpers.rb', line 21

def print(*args)
  res = $stdout.write(*args)
  $stdout.flush
  return res
end

#say(text, color = nil, bold = false) ⇒ Object

Display colorized output (no new line at the end).



16
17
18
# File 'lib/wizard/helpers.rb', line 16

def say(text, color=nil, bold=false)
  print(color ? colorize(text, color, bold) : text)
end

#say!(text, color = nil, bold = false) ⇒ Object

Display colorized output.



28
29
30
# File 'lib/wizard/helpers.rb', line 28

def say!(text, color=nil, bold=false)
  say(text+"\n", color, bold)
end