keybox 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,125 +0,0 @@
1
- #!/usr/local/bin/ruby -w
2
-
3
- # system_extensions.rb
4
- #
5
- # Created by James Edward Gray II on 2006-06-14.
6
- # Copyright 2006 Gray Productions. All rights reserved.
7
- #
8
- # This is Free Software. See LICENSE and COPYING for details.
9
-
10
- class HighLine
11
- module SystemExtensions
12
- module_function
13
-
14
- #
15
- # This section builds character reading and terminal size functions
16
- # to suit the proper platform we're running on. Be warned: Here be
17
- # dragons!
18
- #
19
- begin
20
- # Cygwin will look like Windows, but we want to treat it like a Posix OS:
21
- raise LoadError, "Cygwin is a Posix OS." if RUBY_PLATFORM =~ /\bcygwin\b/i
22
-
23
- require "Win32API" # See if we're on Windows.
24
-
25
- CHARACTER_MODE = "Win32API" # For Debugging purposes only.
26
-
27
- #
28
- # Windows savvy getc().
29
- #
30
- # *WARNING*: This method ignores <tt>input</tt> and reads one
31
- # character from +STDIN+!
32
- #
33
- def get_character( input = STDIN )
34
- Win32API.new("crtdll", "_getch", [ ], "L").Call
35
- end
36
-
37
- # A Windows savvy method to fetch the console columns, and rows.
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
56
- rescue LoadError # If we're not on Windows try...
57
- begin
58
- require "termios" # Unix, first choice.
59
-
60
- CHARACTER_MODE = "termios" # For Debugging purposes only.
61
-
62
- #
63
- # Unix savvy getc(). (First choice.)
64
- #
65
- # *WARNING*: This method requires the "termios" library!
66
- #
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
81
- rescue LoadError # If our first choice fails, default.
82
- CHARACTER_MODE = "stty" # For Debugging purposes only.
83
-
84
- #
85
- # Unix savvy getc(). (Second choice.)
86
- #
87
- # *WARNING*: This method requires the external "stty" program!
88
- #
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
98
-
99
- #
100
- # Switched the input mode to raw and disables echo.
101
- #
102
- # *WARNING*: This method requires the external "stty" program!
103
- #
104
- def raw_no_echo_mode
105
- @state = `stty -g`
106
- system "stty raw -echo cbreak isig"
107
- end
108
-
109
- #
110
- # Restores a previously saved input mode.
111
- #
112
- # *WARNING*: This method requires the external "stty" program!
113
- #
114
- def restore_mode
115
- system "stty #{@state}"
116
- end
117
- end
118
-
119
- # A Unix savvy method to fetch the console columns, and rows.
120
- def terminal_size
121
- `stty size`.split.map { |x| x.to_i }.reverse
122
- end
123
- end
124
- end
125
- end