Module | HighLine::SystemExtensions |
In: |
lib/highline/system_extensions.rb
|
JRUBY | = | defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' | ||
CHARACTER_MODE | = | "Win32API" | ||
STD_INPUT_HANDLE | = | -10 | win32 console APIs | |
STD_OUTPUT_HANDLE | = | -11 | ||
STD_ERROR_HANDLE | = | -12 | ||
ENABLE_PROCESSED_INPUT | = | 0x0001 | ||
ENABLE_LINE_INPUT | = | 0x0002 | ||
ENABLE_WRAP_AT_EOL_OUTPUT | = | 0x0002 | ||
ENABLE_ECHO_INPUT | = | 0x0004 | ||
ENABLE_WINDOW_INPUT | = | 0x0008 | ||
ENABLE_MOUSE_INPUT | = | 0x0010 | ||
ENABLE_INSERT_MODE | = | 0x0020 | ||
ENABLE_QUICK_EDIT_MODE | = | 0x0040 | ||
CHARACTER_MODE | = | "termios" | ||
CHARACTER_MODE | = | "ncurses" | work correctly in JRuby manually installing the ffi-ncurses gem is the only way to get highline to operate correctly in JRuby. The ncurses library is only present on unix platforms so this is not a solution for using highline in JRuby on windows. | |
CHARACTER_MODE | = | "stty" |
# File lib/highline/system_extensions.rb, line 94 94: def GetConsoleMode( console_handle ) 95: @@apiGetConsoleMode ||= Win32API.new( "kernel32", "GetConsoleMode", 96: ['L', 'P'], 'I' ) 97: 98: mode = ' ' * 4 99: @@apiGetConsoleMode.call(console_handle, mode) 100: mode.unpack('L')[0] 101: end
# File lib/highline/system_extensions.rb, line 110 110: def GetConsoleScreenBufferInfo( console_handle ) 111: @@apiGetConsoleScreenBufferInfo ||= 112: Win32API.new( "kernel32", "GetConsoleScreenBufferInfo", 113: ['L', 'P'], 'L' ) 114: 115: format = 'SSSSSssssSS' 116: buf = ([0] * format.size).pack(format) 117: @@apiGetConsoleScreenBufferInfo.call(console_handle, buf) 118: buf.unpack(format) 119: end
# File lib/highline/system_extensions.rb, line 87 87: def GetStdHandle( handle_type ) 88: @@apiGetStdHandle ||= Win32API.new( "kernel32", "GetStdHandle", 89: ['L'], 'L' ) 90: 91: @@apiGetStdHandle.call( handle_type ) 92: end
windows savvy console echo toggler
# File lib/highline/system_extensions.rb, line 54 54: def SetConsoleEcho( console_handle, on ) 55: mode = GetConsoleMode(console_handle) 56: 57: # toggle the console echo bit 58: if on 59: mode |= ENABLE_ECHO_INPUT 60: else 61: mode &= ~ENABLE_ECHO_INPUT 62: end 63: 64: ok = SetConsoleMode(console_handle, mode) 65: end
# File lib/highline/system_extensions.rb, line 103 103: def SetConsoleMode( console_handle, mode ) 104: @@apiSetConsoleMode ||= Win32API.new( "kernel32", "SetConsoleMode", 105: ['L', 'L'], 'I' ) 106: 107: @@apiSetConsoleMode.call(console_handle, mode) != 0 108: end
Windows savvy getc().
# File lib/highline/system_extensions.rb, line 33 33: def get_character( input = STDIN ) 34: @stdin_handle ||= GetStdHandle(STD_INPUT_HANDLE) 35: 36: begin 37: SetConsoleEcho(@stdin_handle, false) 38: input.getbyte 39: ensure 40: SetConsoleEcho(@stdin_handle, true) 41: end 42: end
Unix savvy getc(). (Second choice.)
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 182 182: def get_character( input = STDIN ) 183: raw_no_echo_mode 184: 185: begin 186: input.getbyte 187: ensure 188: restore_mode 189: end 190: end
ncurses savvy getc(). (JRuby choice.)
# File lib/highline/system_extensions.rb, line 160 160: def get_character( input = STDIN ) 161: FFI::NCurses.initscr 162: FFI::NCurses.cbreak 163: begin 164: FFI::NCurses.curs_set 0 165: input.getc 166: ensure 167: FFI::NCurses.endwin 168: end 169: end
Unix savvy getc(). (First choice.)
WARNING: This method requires the "termios" library!
# File lib/highline/system_extensions.rb, line 132 132: def get_character( input = STDIN ) 133: old_settings = Termios.getattr(input) 134: 135: new_settings = old_settings.dup 136: new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON) 137: new_settings.c_cc[Termios::VMIN] = 1 138: 139: begin 140: Termios.setattr(input, Termios::TCSANOW, new_settings) 141: input.getbyte 142: ensure 143: Termios.setattr(input, Termios::TCSANOW, old_settings) 144: end 145: 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 197 197: def raw_no_echo_mode 198: @state = `stty -g` 199: system "stty raw -echo cbreak isig" 200: end
Restores a previously saved input mode.
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 207 207: def restore_mode 208: system "stty #{@state}" 209: end
A Windows savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 45 45: def terminal_size 46: stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE) 47: 48: bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy = 49: GetConsoleScreenBufferInfo(stdout_handle) 50: return right - left + 1, bottom - top + 1 51: end
A ncurses savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 216 216: def terminal_size 217: size = [80, 40] 218: FFI::NCurses.initscr 219: begin 220: size = FFI::NCurses.getmaxyx(stdscr).reverse 221: ensure 222: FFI::NCurses.endwin 223: end 224: size 225: end
A Unix savvy method using stty that to fetch the console columns, and rows. … stty does not work in JRuby
# File lib/highline/system_extensions.rb, line 229 229: def terminal_size 230: if /solaris/ =~ RUBY_PLATFORM and 231: `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/ 232: [$2, $1].map { |c| x.to_i } 233: else 234: `stty size`.split.map { |x| x.to_i }.reverse 235: end 236: end