module ANSI::Terminal

Terminal

This library is based of HighLine's SystemExtensions by James Edward Gray II.

Copyright 2006 Gray Productions

Distributed under the tems of the Ruby software license.

Constants

CHARACTER_MODE
ENABLE_ECHO_INPUT
ENABLE_INSERT_MODE
ENABLE_LINE_INPUT
ENABLE_MOUSE_INPUT
ENABLE_PROCESSED_INPUT
ENABLE_QUICK_EDIT_MODE
ENABLE_WINDOW_INPUT
ENABLE_WRAP_AT_EOL_OUTPUT
STD_ERROR_HANDLE
STD_INPUT_HANDLE

win32 console APIs

STD_OUTPUT_HANDLE

Public Instance Methods

GetConsoleMode( console_handle ) click to toggle source
# File lib/ansi/terminal/win32.rb, line 78
def GetConsoleMode( console_handle )
  @@apiGetConsoleMode ||= Win32API.new( "kernel32", "GetConsoleMode",
                                        ['L', 'P'], 'I' )

  mode = ' ' * 4
  @@apiGetConsoleMode.call(console_handle, mode)
  mode.unpack('L')[0]
end
GetConsoleScreenBufferInfo( console_handle ) click to toggle source
# File lib/ansi/terminal/win32.rb, line 94
def GetConsoleScreenBufferInfo( console_handle )
  @@apiGetConsoleScreenBufferInfo ||=
    Win32API.new( "kernel32", "GetConsoleScreenBufferInfo",
                  ['L', 'P'], 'L' )

  format = 'SSSSSssssSS'
  buf    = ([0] * format.size).pack(format)
  @@apiGetConsoleScreenBufferInfo.call(console_handle, buf)
  buf.unpack(format)
end
GetStdHandle( handle_type ) click to toggle source
# File lib/ansi/terminal/win32.rb, line 71
def GetStdHandle( handle_type )
  @@apiGetStdHandle ||= Win32API.new( "kernel32", "GetStdHandle",
                                      ['L'], 'L' )

  @@apiGetStdHandle.call( handle_type )
end
SetConsoleEcho( console_handle, on ) click to toggle source

windows savvy console echo toggler

# File lib/ansi/terminal/win32.rb, line 38
def SetConsoleEcho( console_handle, on )
  mode = GetConsoleMode(console_handle)

  # toggle the console echo bit
  if on
      mode |=  ENABLE_ECHO_INPUT
  else
      mode &= ~ENABLE_ECHO_INPUT
  end

  ok = SetConsoleMode(console_handle, mode)
end
SetConsoleMode( console_handle, mode ) click to toggle source
# File lib/ansi/terminal/win32.rb, line 87
def SetConsoleMode( console_handle, mode )
  @@apiSetConsoleMode ||= Win32API.new( "kernel32", "SetConsoleMode",
                                        ['L', 'L'], 'I' )

  @@apiSetConsoleMode.call(console_handle, mode) != 0
end
get_character(input = STDIN) click to toggle source

Curses savvy getc().

# File lib/ansi/terminal/curses.rb, line 13
def get_character(input = STDIN)
  Curses.getch()
end
raw_no_echo_mode() click to toggle source

Switched the input mode to raw and disables echo.

WARNING: This method requires the external “stty” program!

# File lib/ansi/terminal/stty.rb, line 31
def raw_no_echo_mode
  @state = %x`stty -g`
  system "stty raw -echo cbreak isig"
end
restore_mode() click to toggle source

Restores a previously saved input mode.

WARNING: This method requires the external “stty” program!

# File lib/ansi/terminal/stty.rb, line 41
def restore_mode
  system "stty #{@state}"
end
screen_width(out=STDERR) click to toggle source

Console screen width (taken from progress bar)

NOTE: Don't know how portable screen_width is. TODO: How to fit into system?

# File lib/ansi/terminal/termios.rb, line 50
def screen_width(out=STDERR)
  default_width = ENV['COLUMNS'] || 76
  begin
    tiocgwinsz = 0x5413
    data = [0, 0, 0, 0].pack("SSSS")
    if out.ioctl(tiocgwinsz, data) >= 0 then
      rows, cols, xpixels, ypixels = data.unpack("SSSS")
      if cols >= 0 then cols else default_width end
    else
      default_width
    end
  rescue Exception
    default_width
  end
end
terminal_height() click to toggle source

Get the height of the terminal window.

# File lib/ansi/terminal.rb, line 37
def terminal_height
  terminal_size.last
end
terminal_size() click to toggle source
# File lib/ansi/terminal/curses.rb, line 17
def terminal_size
  Curses.init_screen
  w, r = Curses.cols, Curses.lines
  Curses.close_screen
  return w, r
end
terminal_width() click to toggle source

Get the width of the terminal window.

# File lib/ansi/terminal.rb, line 32
def terminal_width
  terminal_size.first
end