Module: Origami::Console

Defined in:
lib/origami/parser.rb

Defined Under Namespace

Modules: Colors

Constant Summary collapse

@@getConsoleScreenBufferInfo =
Win32API.new("kernel32", "GetConsoleScreenBufferInfo", 'LP', 'L')
@@setConsoleTextAttribute =
Win32API.new("kernel32", "SetConsoleTextAttribute", 'LN', 'I')
@@hOut =
getStdHandle.call(-11)

Class Method Summary collapse

Class Method Details

.colorize(text, color, bright = false) ⇒ Object



95
96
97
98
# File 'lib/origami/parser.rb', line 95

def self.colorize(text, color, bright = false)
  col, nocol = [color, Colors::GREY].map! { |key| "\033[#{key}m" }
  "#{col}#{text}#{nocol}"
end

.colorprint(text, color, bright = false, fd = STDOUT) ⇒ Object

:nodoc:



101
102
103
104
105
# File 'lib/origami/parser.rb', line 101

def self.colorprint(text, color, bright = false, fd = STDOUT) #:nodoc:
  set_fg_color(color, bright, fd) {
    fd << text
  }    
end

.set_fg_color(color, bright = false, fd = STDOUT) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/origami/parser.rb', line 73

def self.set_fg_color(color, bright = false, fd = STDOUT) #:nodoc:
  if RUBY_PLATFORM =~ /win32/ or RUBY_PLATFORM =~ /mingw32/
    screen_info = "\x00" * 30
    current = 
      if @@getConsoleScreenBufferInfo.call(@@hOut, screen_info) == 1
        screen_info[8,2].unpack('v')[0]
      else 
        Colors::GREY
      end
    color |= Colors::WHITE if bright
    @@setConsoleTextAttribute.call(@@hOut, color)
    yield
    @@setConsoleTextAttribute.call(@@hOut, current)
  else
    col, nocol = [color, Colors::GREY].map! { |key| "\033[#{key}m" }
    fd << col
    yield
    fd << nocol
  end
end