Class: CMDB::Shell::Text
- Inherits:
-
Object
- Object
- CMDB::Shell::Text
- 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
-
#bold(text) ⇒ String
(also: #bright_default)
Returns text as bold text for use on a terminal.
-
#default(text) ⇒ String
Returns ‘text` in the default foreground colour.
-
#initialize(plain) ⇒ Text
constructor
A new instance of Text.
-
#strip_color(text) ⇒ String
Remove any color codes from text.
-
#width ⇒ Integer
Screen width (number of columns).
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.
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.
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.
35 36 37 |
# File 'lib/cmdb/shell/text.rb', line 35 def strip_color(text) text.to_s.gsub(/\e\[.*?(\d)+m/ , '') end |
#width ⇒ Integer
Returns 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 |