Module: Console

Extended by:
Console
Included in:
Console
Defined in:
lib/drydock/console.rb

Overview

:nodoc:all

Constant Summary collapse

ATTRIBUTES =

ANSI escape sequence numbers for text attributes

{
  :normal     => 0,
  :bright     => 1,
  :dim        => 2,
  :underline  => 4,
  :blink      => 5,
  :reverse    => 7,
  :hidden     => 8,
  :default    => 0,
}.freeze
COLOURS =

ANSI escape sequence numbers for text colours

{
  :black   => 30,
  :red     => 31,
  :green   => 32,
  :yellow  => 33,
  :blue    => 34,
  :magenta => 35,
  :cyan    => 36,
  :white   => 37,
  :default => 39,
  :random  => 30 + rand(10).to_i
}.freeze
BGCOLOURS =

ANSI escape sequence numbers for background colours

{
  :black   => 40,
  :red     => 41,
  :green   => 42,
  :yellow  => 43,
  :blue    => 44,
  :magenta => 45,
  :cyan    => 46,
  :white   => 47,
  :default => 49,
  :random  => 40 + rand(10).to_i
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



110
111
112
# File 'lib/drydock/console.rb', line 110

def self.clear
  tput :clear
end

.style(col, bgcol = nil, att = nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/drydock/console.rb', line 102

def self.style(col, bgcol=nil, att=nil)
  valdor = []
  valdor << COLOURS[col] if COLOURS.has_key?(col)
  valdor << BGCOLOURS[bgcol] if BGCOLOURS.has_key?(bgcol)
  valdor << ATTRIBUTES[att] if ATTRIBUTES.has_key?(att)
  "\e[#{valdor.join(";")}m"   # => \e[8;34;42m  
end

Instance Method Details

#heightObject



122
123
124
# File 'lib/drydock/console.rb', line 122

def height
  tput_val(:lines).to_i
end


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/drydock/console.rb', line 88

def print_at(str, props={})
  print_at_lamb = lambda {
    props[:x] ||= 0
    props[:y] ||= 0
    props[:minus] = false unless props.has_key?(:minus)
    props[:x] = props[:x]-str.noatt.size if props[:x] && props[:minus] # Subtract the str length from the position
    Cursor.save
    Cursor.move = [props[:x], props[:y]]
    print str
    Cursor.restore
  }
  RUBY_VERSION =~ /1.9/ ? Thread.exclusive(&print_at_lamb) : print_at_lamb.call
end


83
84
85
86
87
# File 'lib/drydock/console.rb', line 83

def print_center(str, props={})
  props[:x] = ((width - str.noatt.length) / 2).to_i-1
  props[:y] ||= height
  print_at(str, props)
end


59
60
61
62
63
64
# File 'lib/drydock/console.rb', line 59

def print_left(str, props={})
  props[:x] ||= 0
  props[:y] ||= Cursor.y
#    print_at("x:#{props[:x]} y:#{props[:y]}", {:x => 0, :y => 10})
  print_at(str, props)
end


65
66
67
68
69
70
# File 'lib/drydock/console.rb', line 65

def print_right(str, props={})
  props[:x] ||= width
  props[:y] ||= Cursor.y
  props[:minus] = true unless props.has_key?(:minus)
  print_at(str, props)  
end


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/drydock/console.rb', line 71

def print_spaced(*args)
  props = (args.last.is_a? Hash) ? args.pop : {}
  props[:y] = Cursor.y
  chunk_width = (width / args.flatten.size).to_i
  chunk_at = 0
  args.each do |chunk|
    props[:x] = chunk_at
    print_at(chunk.to_s[0, chunk_width], props)
    chunk_at += chunk_width
  end
  puts
end

#resetObject



114
115
116
# File 'lib/drydock/console.rb', line 114

def reset
  tput :reset
end

#valid_colour?(colour) ⇒ Boolean Also known as: valid_color?

Returns:

  • (Boolean)


54
55
56
# File 'lib/drydock/console.rb', line 54

def valid_colour?(colour)
  COLOURS.has_key? colour
end

#widthObject



118
119
120
# File 'lib/drydock/console.rb', line 118

def width
  tput_val(:cols).to_i
end