Class: String

Inherits:
Object show all
Defined in:
lib/drydock/mixins/string.rb

Constant Summary collapse

true

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disable_colorObject



5
# File 'lib/drydock/mixins/string.rb', line 5

def String.disable_color;  @@print_with_attributes = false; end

.disable_colourObject



4
# File 'lib/drydock/mixins/string.rb', line 4

def String.disable_colour; @@print_with_attributes = false; end

.enable_colorObject



7
# File 'lib/drydock/mixins/string.rb', line 7

def String.enable_color;   @@print_with_attributes = true;  end

.enable_colourObject



6
# File 'lib/drydock/mixins/string.rb', line 6

def String.enable_colour;  @@print_with_attributes = true;  end

Instance Method Details

#att(a = :default) ⇒ Object

See colour



33
34
35
36
37
38
# File 'lib/drydock/mixins/string.rb', line 33

def att(a = :default)
  return self unless @@print_with_attributes
  Console.style(nil, nil, a) +
  self +
  Console.style(nil, nil, :default)
end

#bgcolour(bgcol = :default) ⇒ Object Also known as: bgcolor

See colour



24
25
26
27
28
29
# File 'lib/drydock/mixins/string.rb', line 24

def bgcolour(bgcol = :default)
  return self unless @@print_with_attributes
  Console.style(nil, bgcol, nil) +
  self +
  Console.style(nil, :default, nil)
end

#brightObject

Shortcut for att(:bright)



41
# File 'lib/drydock/mixins/string.rb', line 41

def bright; att(:bright); end

#colour(col, bgcol = nil, attribute = nil) ⇒ Object Also known as: color

col, bgcol, and attribute are symbols corresponding to Console::COLOURS, Console::BGCOLOURS, and Console::ATTRIBUTES. Returns the string in the format attributes + string + defaults.

"MONKEY_JUNK".colour(:blue, :white, :blink)  # => "\e[34;47;5mMONKEY_JUNK\e[39;49;0m"


15
16
17
18
19
20
# File 'lib/drydock/mixins/string.rb', line 15

def colour(col, bgcol = nil, attribute = nil)
  return self unless @@print_with_attributes
  Console.style(col, bgcol, attribute) +
  self +
  Console.style(:default, :default, :default)
end

#noattObject Also known as: noansi

Returns the string with ANSI escape codes removed.

NOTE: The non-printable attributes count towards the string size. You can use this method to get the “visible” size:

"\e[34;47;5mMONKEY_JUNK\e[39;49;0m".noatt.size      # => 11
"\e[34;47;5mMONKEY_JUNK\e[39;49;0m".size            # => 31


61
62
63
# File 'lib/drydock/mixins/string.rb', line 61

def noatt
  gsub(/\e\[?[0-9;]*[mc]?/, '')
end

Print the string at x y. When minus is any true value the length of the string is subtracted from the value of x before printing.



46
47
48
49
50
51
# File 'lib/drydock/mixins/string.rb', line 46

def print_at(x=nil, y=nil, minus=false)
  args = {:minus=>minus}
  args[:x] &&= x
  args[:y] &&= y
  Console.print_at(self, args)
end