Class: CMDB::Shell::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/shell/text.rb

Overview

Adapted from pry: github.com/pry/pry

Constant Summary collapse

COLORS =
{
  'black'   => 0,
  'red'     => 1,
  'green'   => 2,
  'yellow'  => 3,
  'blue'    => 4,
  'purple'  => 5,
  'magenta' => 5,
  'cyan'    => 6,
  'white'   => 7
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(plain) ⇒ Text

Returns a new instance of Text.



16
17
18
19
# File 'lib/cmdb/shell/text.rb', line 16

def initialize(plain)
  @plain = plain
  trap('SIGWINCH') { @width = nil } unless @plain
end

Instance Method Details

#bold(text) ⇒ String Also known as: bright_default

Returns text as bold text for use on a terminal.

Parameters:

  • text (String, #to_s)

Returns:

  • (String)

    text



43
44
45
# File 'lib/cmdb/shell/text.rb', line 43

def bold(text)
  @plain && text || "\e[1m#{text}\e[0m"
end

#default(text) ⇒ String

Returns ‘text` in the default foreground colour. Use this instead of “black” or “white” when you mean absence of colour.

Parameters:

  • text (String, #to_s)

Returns:

  • (String)


52
53
54
# File 'lib/cmdb/shell/text.rb', line 52

def default(text)
  text.to_s
end

#strip_color(text) ⇒ String

Remove any color codes from text.

Parameters:

  • text (String, #to_s)

Returns:

  • (String)

    text stripped of any color codes.



35
36
37
# File 'lib/cmdb/shell/text.rb', line 35

def strip_color(text)
  text.to_s.gsub(/\e\[.*?(\d)+m/ , '')
end

#widthInteger

Returns screen width (number of columns).

Returns:

  • (Integer)

    screen width (number of columns)



58
59
60
61
62
63
64
# File 'lib/cmdb/shell/text.rb', line 58

def width
  if @plain
    65_535
  else
    @width ||= Integer(`stty size`.chomp.split(/ +/)[1]) rescue 80
  end
end