Class: RSpec::CapturingFormatter::WindowsTerminal::NativeApi

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/capturing_formatter/windows_terminal.rb

Overview

Isolates the Fiddle bindings for the Windows console API.

Instance Method Summary collapse

Constructor Details

#initializeNativeApi

Returns a new instance of NativeApi.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rspec/capturing_formatter/windows_terminal.rb', line 43

def initialize
  kernel32 = Fiddle.dlopen("kernel32.dll")
  @get_std_handle = function(kernel32, "GetStdHandle", [Fiddle::TYPE_LONG], Fiddle::TYPE_VOIDP)
  @get_console_mode = function(
    kernel32,
    "GetConsoleMode",
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],
    Fiddle::TYPE_INT
  )
  @set_console_mode = function(
    kernel32,
    "SetConsoleMode",
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG],
    Fiddle::TYPE_INT
  )
end

Instance Method Details

#enable_virtual_terminal_processing(output) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec/capturing_formatter/windows_terminal.rb', line 60

def enable_virtual_terminal_processing(output)
  handle = @get_std_handle.call(std_handle(output))
  return false if handle.to_i.zero? || handle.to_i == -1

  # Console modes are Windows DWORDs: four little-endian bytes.
  mode = Fiddle::Pointer.malloc(4)
  return false if @get_console_mode.call(handle, mode).zero?

  current_mode = mode[0, 4].unpack1("V")
  return true if (current_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING).positive?

  !@set_console_mode.call(handle, current_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING).zero?
end