Module: Console::Windows

Extended by:
FFI::Library, Fiddle::Importer
Defined in:
lib/color_console/platform/windows.rb,
lib/color_console/platform/windows_ffi.rb,
lib/color_console/platform/windows_fiddle.rb

Overview

Implements Windows-specific platform functionality when running under MRI. Uses Fiddle, since MRI does not provide FFI functionality as standard, and furthermore requires a Ruby DevKit installation to install the ffi gem. This is a royal pain, so we instead use Fiddle to invoke the Windows console API.

Defined Under Namespace

Classes: BufferInfo, Coord

Constant Summary collapse

STD_OUTPUT_HANDLE =

Constant representing STDOUT (0xFFFFFFF5) We can’t use this however, as it overflows to a Bignum!

-11
STD_INPUT_HANDLE =
0xFFFFFFF6
STD_ERROR_HANDLE =
0xFFFFFFF7

Class Method Summary collapse

Class Method Details

.attach_function(name, sig) ⇒ Object

Wrap the need to call #call on a Fiddle::Function



25
26
27
28
# File 'lib/color_console/platform/windows_fiddle.rb', line 25

def self.attach_function(name, sig)
    func = extern(sig, :stdcall)
    define_singleton_method(name){ |*args| func.call(*args) }
end

.buffer_infoObject

Retrieve a BufferInfo object



58
59
60
# File 'lib/color_console/platform/windows_ffi.rb', line 58

def buffer_info
    @buffer_info ||= BufferInfo.new
end

.get_buffer_infoBufferInfo

Populate a BufferInfo structure with details about the current buffer state.

Returns:

  • (BufferInfo)

    A BufferInfo structure containing fields for various bits of console state.



27
28
29
30
31
32
# File 'lib/color_console/platform/windows.rb', line 27

def get_buffer_info
    if stdout
        self.get_console_screen_buffer_info(stdout, buffer_info)
        @buffer_info
    end
end

.set_color(color) ⇒ Object

Sets the console foreground and background colors.



37
38
39
40
41
# File 'lib/color_console/platform/windows.rb', line 37

def set_color(color)
    if stdout && color
        self.set_console_text_attribute(stdout, color)
    end
end

.set_cursor_position(x, y) ⇒ Object

Sets the cursor position to the specified x and y locations in the console output buffer. If y is nil, the cursor is positioned at x on the current line.



68
69
70
71
72
73
# File 'lib/color_console/platform/windows_ffi.rb', line 68

def set_cursor_position(x, y)
    if stdout && x && y
        coord = Coord.new(x, y)
        self.set_console_cursor_position(stdout, coord)
    end
end

.stdoutObject

Retrieve a handle to STDOUT



15
16
17
# File 'lib/color_console/platform/windows.rb', line 15

def stdout
    @stdout ||= self.get_std_handle(STD_OUTPUT_HANDLE)
end