| 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 33
33: def get_character( input = STDIN )
34: Win32API.new("crtdll", "_getch", [ ], "L").Call
35: end
Unix savvy getc(). (First choice.)
WARNING: This method requires the "termios" library!
# File lib/highline/system_extensions.rb, line 67
67: def get_character( input = STDIN )
68: old_settings = Termios.getattr(input)
69:
70: new_settings = old_settings.dup
71: new_settings.c_lflag &= ~(Termios::ECHO | Termios::ICANON)
72: new_settings.c_cc[Termios::VMIN] = 1
73:
74: begin
75: Termios.setattr(input, Termios::TCSANOW, new_settings)
76: input.getc
77: ensure
78: Termios.setattr(input, Termios::TCSANOW, old_settings)
79: end
80: end
Unix savvy getc(). (Second choice.)
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 89
89: def get_character( input = STDIN )
90: raw_no_echo_mode
91:
92: begin
93: input.getc
94: ensure
95: restore_mode
96: end
97: 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 104
104: def raw_no_echo_mode
105: @state = `stty -g`
106: system "stty raw -echo cbreak isig"
107: end
Restores a previously saved input mode.
WARNING: This method requires the external "stty" program!
# File lib/highline/system_extensions.rb, line 114
114: def restore_mode
115: system "stty #{@state}"
116: end
A Windows savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 38
38: def terminal_size
39: m_GetStdHandle = Win32API.new( 'kernel32',
40: 'GetStdHandle',
41: ['L'],
42: 'L' )
43: m_GetConsoleScreenBufferInfo = Win32API.new(
44: 'kernel32', 'GetConsoleScreenBufferInfo', ['L', 'P'], 'L'
45: )
46:
47: format = 'SSSSSssssSS'
48: buf = ([0] * format.size).pack(format)
49: stdout_handle = m_GetStdHandle.call(0xFFFFFFF5)
50:
51: m_GetConsoleScreenBufferInfo.call(stdout_handle, buf)
52: bufx, bufy, curx, cury, wattr,
53: left, top, right, bottom, maxx, maxy = buf.unpack(format)
54: return right - left + 1, bottom - top + 1
55: end
A Unix savvy method to fetch the console columns, and rows.
# File lib/highline/system_extensions.rb, line 120
120: def terminal_size
121: if /solaris/ =~ RUBY_PLATFORM and
122: `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
123: [$2, $1].map { |c| x.to_i }
124: else
125: `stty size`.split.map { |x| x.to_i }.reverse
126: end
127: end