| Module | HighLine::SystemExtensions |
| In: |
lib/highline/system_extensions.rb
|
| 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 | = | "stty" |
# File lib/highline/system_extensions.rb, line 92
92: def GetConsoleMode( console_handle )
93: @@apiGetConsoleMode ||= Win32API.new( "kernel32", "GetConsoleMode",
94: ['L', 'P'], 'I' )
95:
96: mode = ' ' * 4
97: @@apiGetConsoleMode.call(console_handle, mode)
98: mode.unpack('L')[0]
99: end
# File lib/highline/system_extensions.rb, line 108
108: def GetConsoleScreenBufferInfo( console_handle )
109: @@apiGetConsoleScreenBufferInfo ||=
110: Win32API.new( "kernel32", "GetConsoleScreenBufferInfo",
111: ['L', 'P'], 'L' )
112:
113: format = 'SSSSSssssSS'
114: buf = ([0] * format.size).pack(format)
115: @@apiGetConsoleScreenBufferInfo.call(console_handle, buf)
116: buf.unpack(format)
117: end
# File lib/highline/system_extensions.rb, line 85
85: def GetStdHandle( handle_type )
86: @@apiGetStdHandle ||= Win32API.new( "kernel32", "GetStdHandle",
87: ['L'], 'L' )
88:
89: @@apiGetStdHandle.call( handle_type )
90: end
windows savvy console echo toggler
# File lib/highline/system_extensions.rb, line 52
52: def SetConsoleEcho( console_handle, on )
53: mode = GetConsoleMode(console_handle)
54:
55: # toggle the console echo bit
56: if on
57: mode |= ENABLE_ECHO_INPUT
58: else
59: mode &= ~ENABLE_ECHO_INPUT
60: end
61:
62: ok = SetConsoleMode(console_handle, mode)
63: end
# File lib/highline/system_extensions.rb, line 101
101: def SetConsoleMode( console_handle, mode )
102: @@apiSetConsoleMode ||= Win32API.new( "kernel32", "SetConsoleMode",
103: ['L', 'L'], 'I' )
104:
105: @@apiSetConsoleMode.call(console_handle, mode) != 0
106: end
Windows savvy getc().
# File lib/highline/system_extensions.rb, line 31
31: def get_character( input = STDIN )
32: @stdin_handle ||= GetStdHandle(STD_INPUT_HANDLE)
33:
34: begin
35: SetConsoleEcho(@stdin_handle, false)
36: input.getbyte
37: ensure
38: SetConsoleEcho(@stdin_handle, true)
39: end
40: end
Unix savvy getc(). (Second choice.)
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 152
152: def get_character( input = STDIN )
153: raw_no_echo_mode
154:
155: begin
156: input.getbyte
157: ensure
158: restore_mode
159: end
160: end
Unix savvy getc(). (First choice.)
WARNING: This method requires the "termios" library!
# File lib/highline/system_extensions.rb, line 130
130: def get_character( input = STDIN )
131: old_settings = Termios.getattr(input)
132:
133: new_settings = old_settings.dup
134: new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
135: new_settings.c_cc[Termios::VMIN] = 1
136:
137: begin
138: Termios.setattr(input, Termios::TCSANOW, new_settings)
139: input.getbyte
140: ensure
141: Termios.setattr(input, Termios::TCSANOW, old_settings)
142: end
143: 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 167
167: def raw_no_echo_mode
168: @state = `stty -g`
169: system "stty raw -echo cbreak isig"
170: end
Restores a previously saved input mode.
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 177
177: def restore_mode
178: system "stty #{@state}"
179: end
A Windows savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 43
43: def terminal_size
44: stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE)
45:
46: bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy =
47: GetConsoleScreenBufferInfo(stdout_handle)
48: return right - left + 1, bottom - top + 1
49: end
A Unix savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 183
183: def terminal_size
184: if /solaris/ =~ RUBY_PLATFORM and
185: `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
186: [$2, $1].map { |c| x.to_i }
187: else
188: `stty size`.split.map { |x| x.to_i }.reverse
189: end
190: end