Module | HighLine::SystemExtensions |
In: |
lib/highline/system_extensions.rb
|
CHARACTER_MODE | = | "Win32API" |
CHARACTER_MODE | = | "termios" |
CHARACTER_MODE | = | "stty" |
Windows savvy getc().
WARNING: This method ignores input and reads one character from STDIN!
# File lib/highline/system_extensions.rb, line 30 30: def get_character( input = STDIN ) 31: Win32API.new("crtdll", "_getch", [ ], "L").Call 32: end
Unix savvy getc(). (First choice.)
WARNING: This method requires the "termios" library!
# File lib/highline/system_extensions.rb, line 64 64: def get_character( input = STDIN ) 65: old_settings = Termios.getattr(input) 66: 67: new_settings = old_settings.dup 68: new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON) 69: new_settings.c_cc[Termios::VMIN] = 1 70: 71: begin 72: Termios.setattr(input, Termios::TCSANOW, new_settings) 73: input.getc 74: ensure 75: Termios.setattr(input, Termios::TCSANOW, old_settings) 76: end 77: end
Unix savvy getc(). (Second choice.)
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 86 86: def get_character( input = STDIN ) 87: raw_no_echo_mode 88: 89: begin 90: input.getc 91: ensure 92: restore_mode 93: end 94: end
Switched the input mode to raw and disables echo.
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 101 101: def raw_no_echo_mode 102: @state = `stty -g` 103: system "stty raw -echo cbreak" 104: end
Restores a previously saved input mode.
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 111 111: def restore_mode 112: system "stty #{@state}" 113: end
A Windows savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 35 35: def terminal_size 36: m_GetStdHandle = Win32API.new( 'kernel32', 37: 'GetStdHandle', 38: ['L'], 39: 'L' ) 40: m_GetConsoleScreenBufferInfo = Win32API.new( 41: 'kernel32', 'GetConsoleScreenBufferInfo', ['L', 'P'], 'L' 42: ) 43: 44: format = 'SSSSSssssSS' 45: buf = ([0] * format.size).pack(format) 46: stdout_handle = m_GetStdHandle.call(0xFFFFFFF5) 47: 48: m_GetConsoleScreenBufferInfo.call(stdout_handle, buf) 49: bufx, bufy, curx, cury, wattr, 50: left, top, right, bottom, maxx, maxy = buf.unpack(format) 51: return right - left + 1, bottom - top + 1 52: end