Class: Win32::Console
- Inherits:
-
Object
show all
- Includes:
- Constants
- Defined in:
- lib/Win32/Console.rb,
lib/Win32/Console/api.rb,
lib/Win32/Console/ANSI.rb,
lib/Win32/Console/constants.rb,
ext/Console_ext/Console.c
Defined Under Namespace
Modules: ANSI, Constants
Classes: API
Constant Summary
collapse
- VERSION =
'1.0'
Constants included
from Constants
Constants::BACKGROUND_BLUE, Constants::BACKGROUND_GREEN, Constants::BACKGROUND_INTENSITY, Constants::BACKGROUND_RED, Constants::CAPSLOCK_ON, Constants::CONSOLE_TEXTMODE_BUFFER, Constants::CTRL_BREAK_EVENT, Constants::CTRL_CLOSE_EVENT, Constants::CTRL_C_EVENT, Constants::CTRL_LOGOFF_EVENT, Constants::CTRL_SHUTDOWN_EVENT, Constants::DOUBLE_CLICK, Constants::ENABLE_ECHO_INPUT, Constants::ENABLE_LINE_INPUT, Constants::ENABLE_MOUSE_INPUT, Constants::ENABLE_PROCESSED_INPUT, Constants::ENABLE_PROCESSED_OUTPUT, Constants::ENABLE_WINDOW_INPUT, Constants::ENABLE_WRAP_AT_EOL_OUTPUT, Constants::ENHANCED_KEY, Constants::FILE_SHARE_READ, Constants::FILE_SHARE_WRITE, Constants::FOCUS_EVENT, Constants::FOREGROUND_BLUE, Constants::FOREGROUND_GREEN, Constants::FOREGROUND_INTENSITY, Constants::FOREGROUND_RED, Constants::FROM_LEFT_1ST_BUTTON_PRESSED, Constants::FROM_LEFT_2ND_BUTTON_PRESSED, Constants::FROM_LEFT_3RD_BUTTON_PRESSED, Constants::FROM_LEFT_4TH_BUTTON_PRESSED, Constants::GENERIC_READ, Constants::GENERIC_WRITE, Constants::INVALID_HANDLE_VALUE, Constants::KEY_EVENT, Constants::LEFT_ALT_PRESSED, Constants::LEFT_CTRL_PRESSED, Constants::MENU_EVENT, Constants::MOUSE_EVENT, Constants::MOUSE_MOVED, Constants::MOUSE_WHEELED, Constants::NUMLOCK_ON, Constants::RIGHTMOST_BUTTON_PRESSED, Constants::RIGHT_ALT_PRESSED, Constants::RIGHT_CTRL_PRESSED, Constants::SCROLLLOCK_ON, Constants::SHIFT_PRESSED, Constants::STD_ERROR_HANDLE, Constants::STD_INPUT_HANDLE, Constants::STD_OUTPUT_HANDLE, Constants::WINDOW_BUFFER_SIZE_EVENT
Class Method Summary
collapse
Instance Method Summary
collapse
-
#Attr(*attr) ⇒ Object
-
#Cls ⇒ Object
-
#Cursor(*t) ⇒ Object
-
#DefaultBackground ⇒ Object
-
#DefaultBold ⇒ Object
-
#DefaultForeground ⇒ Object
-
#DefaultUnderline ⇒ Object
-
#Display ⇒ Object
-
#Echo(flag = nil) ⇒ Object
-
#FillAttr(attr, number = 1, col = -1,, row = -1)) ⇒ Object
-
#FillChar(char, number, col = -1,, row = -1)) ⇒ Object
-
#Flush ⇒ Object
-
#GetEvents ⇒ Object
-
#Info ⇒ Object
-
#initialize(t = nil) ⇒ Console
constructor
A new instance of Console.
-
#Input ⇒ Object
-
#InputChar(number = nil) ⇒ Object
-
#MaxWindow(flag = nil) ⇒ Object
-
#Mode(mode = nil) ⇒ Object
-
#PeekInput ⇒ Object
-
#ReadAttr(size, col, row) ⇒ Object
-
#ReadChar(size, col, row) ⇒ Object
-
#ReadRect(left, top, right, bottom) ⇒ Object
-
#redirected? ⇒ Boolean
Return true if console is redirected or piped and no longer is outputing to the normal console.
-
#Scroll(left1, top1, right1, bottom1, col, row, char, attr, left2, top2, right2, bottom2) ⇒ Object
-
#Select(type) ⇒ Object
-
#Size(*t) ⇒ Object
-
#Title(title = nil) ⇒ Object
-
#Window(*t) ⇒ Object
-
#Write(s) ⇒ Object
-
#WriteAttr(attr, col, row) ⇒ Object
-
#WriteChar(s, col, row) ⇒ Object
-
#WriteFile(s) ⇒ Object
-
#WriteInput(*t) ⇒ Object
-
#WriteRect(buffer, left, top, right, bottom) ⇒ Object
Constructor Details
#initialize(t = nil) ⇒ Console
Returns a new instance of Console.
Class Method Details
.Alloc ⇒ Object
301
302
303
|
# File 'lib/Win32/Console.rb', line 301
def Console.Alloc()
API.AllocConsole()
end
|
.Free ⇒ Object
297
298
299
|
# File 'lib/Win32/Console.rb', line 297
def Console.Free()
API.FreeConsole()
end
|
.GenerateCtrlEvent(type = nil, pid = nil) ⇒ Object
325
326
327
328
329
|
# File 'lib/Win32/Console.rb', line 325
def Console.GenerateCtrlEvent( type=nil, pid=nil )
type = API.constant('CTRL_C_EVENT') if type == nil
pid = 0 if pid == nil
API.GenerateConsoleCtrlEvent(type, pid)
end
|
309
310
311
312
313
314
315
|
# File 'lib/Win32/Console.rb', line 309
def Console.InputCP(codepage=nil)
if codepage
API.SetConsoleCP(codepage)
else
return API.GetConsoleCP()
end
end
|
305
306
307
|
# File 'lib/Win32/Console.rb', line 305
def Console.MouseButtons()
API.GetNumberOfConsoleMouseButtons()
end
|
.OutputCP(codepage = nil) ⇒ Object
317
318
319
320
321
322
323
|
# File 'lib/Win32/Console.rb', line 317
def Console.OutputCP(codepage=nil)
if codepage
API.SetConsoleOutputCP(codepage)
else
return API.GetConsoleOutputCP()
end
end
|
Instance Method Details
#Attr(*attr) ⇒ Object
206
207
208
209
210
211
212
213
|
# File 'lib/Win32/Console.rb', line 206
def Attr(*attr)
if attr.size > 0
API.SetConsoleTextAttribute( @handle, attr[0] )
else
info = API.GetConsoleScreenBufferInfo( @handle )
return info[4]
end
end
|
#Cls ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/Win32/Console.rb', line 276
def Cls()
attr = @attr_default || ATTR_NORMAL
x, y = Size()
left, top, right , bottom = Window()
vx = right - left
vy = bottom - top
FillChar(' ', x*y, 0, 0)
FillAttr(attr, x*y, 0, 0)
Cursor(0,0)
Window(1,0,0,vx,vy)
end
|
#Cursor(*t) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/Win32/Console.rb', line 77
def Cursor(*t)
col, row, size, visi = t
if col
row = -1 if !row
if col < 0 or row < 0
curr_col, curr_row = API.GetConsoleScreenBufferInfo(@handle)
col = curr_col if col < 0
row = curr_row if row < 0
end
API.SetConsoleCursorPosition( @handle, col, row )
if size and visi
curr_size, curr_visi = API.GetConsoleCursorInfo( @handle )
size = curr_size if size < 0
visi = curr_visi if visi < 0
size = 1 if size < 1
size = 99 if size > 99
API.SetConsoleCursorInfo( @handle, size, visi )
end
else
d, d, curr_col, curr_row = API.GetConsoleScreenBufferInfo(@handle)
curr_size, curr_visi = API.GetConsoleCursorInfo( @handle )
return [ curr_col, curr_row, curr_size, curr_visi ]
end
end
|
#DefaultBackground ⇒ Object
228
229
230
231
|
# File 'lib/Win32/Console.rb', line 228
def DefaultBackground
a = self.Attr
(4..6).map{|i| a[i] }.inject(0){|num, bit| (num << 1) + bit }
end
|
#DefaultBold ⇒ Object
215
216
217
|
# File 'lib/Win32/Console.rb', line 215
def DefaultBold self.Attr[3] == 1
end
|
#DefaultForeground ⇒ Object
223
224
225
226
|
# File 'lib/Win32/Console.rb', line 223
def DefaultForeground
a = self.Attr
(0..2).map{|i| a[i] }.inject(0){|num, bit| (num << 1) + bit }
end
|
#DefaultUnderline ⇒ Object
219
220
221
|
# File 'lib/Win32/Console.rb', line 219
def DefaultUnderline self.Attr[7] == 1
end
|
#Display ⇒ Object
39
40
41
|
# File 'lib/Win32/Console.rb', line 39
def Display
return API.SetConsoleActiveScreenBuffer(@handle)
end
|
#Echo(flag = nil) ⇒ Object
#FillAttr(attr, number = 1, col = -1,, row = -1)) ⇒ Object
258
259
260
261
262
263
264
265
|
# File 'lib/Win32/Console.rb', line 258
def FillAttr(attr, number = 1, col = -1, row = -1)
if col < 0 or row < 0
d, d, curr_col, curr_row = API.GetConsoleScreenBufferInfo(@handle)
col = curr_col if col < 0
row = curr_row if row < 0
end
API.FillConsoleOutputAttribute(@handle, attr, number, col, row)
end
|
#FillChar(char, number, col = -1,, row = -1)) ⇒ Object
267
268
269
270
271
272
273
274
|
# File 'lib/Win32/Console.rb', line 267
def FillChar(char, number, col = -1, row = -1)
if col < 0 or row < 0
d, d, curr_col, curr_row = API.GetConsoleScreenBufferInfo(@handle)
col = curr_col if col < 0
row = curr_row if row < 0
end
API.FillConsoleOutputCharacter(@handle, char[0], number, col, row)
end
|
#Flush ⇒ Object
156
157
158
|
# File 'lib/Win32/Console.rb', line 156
def Flush()
return API.FlushConsoleInputBuffer(@handle)
end
|
#GetEvents ⇒ Object
152
153
154
|
# File 'lib/Win32/Console.rb', line 152
def GetEvents()
return API.GetNumberOfConsoleInputEvents(@handle)
end
|
#Info ⇒ Object
148
149
150
|
# File 'lib/Win32/Console.rb', line 148
def Info()
return API.GetConsoleScreenBufferInfo( @handle )
end
|
170
171
172
|
# File 'lib/Win32/Console.rb', line 170
def Input()
API.ReadConsoleInput(@handle)
end
|
160
161
162
163
164
165
166
167
168
|
# File 'lib/Win32/Console.rb', line 160
def InputChar(number = nil)
number = 1 unless number
buffer = ' ' * number
if API.ReadConsole(@handle, buffer, number) == number
return buffer
else
return nil
end
end
|
#MaxWindow(flag = nil) ⇒ Object
139
140
141
142
143
144
145
146
|
# File 'lib/Win32/Console.rb', line 139
def MaxWindow(flag = nil)
if !flag
info = API.GetConsoleScreenBufferInfo(@handle)
return info[9], info[10]
else
return API.GetLargestConsoleWindowSize(@handle)
end
end
|
#Mode(mode = nil) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/Win32/Console.rb', line 178
def Mode(mode = nil)
if mode
mode = mode.pack('L') if mode === Array
API.SetConsoleMode(@handle, mode)
else
begin
x = API.GetConsoleMode(@handle)
return x
rescue
return 9999
end
end
end
|
174
175
176
|
# File 'lib/Win32/Console.rb', line 174
def PeekInput()
API.PeekConsoleInput(@handle)
end
|
#ReadAttr(size, col, row) ⇒ Object
72
73
74
75
|
# File 'lib/Win32/Console.rb', line 72
def ReadAttr(size, col, row)
x = API.ReadConsoleOutputAttribute( @handle, size, col, row )
return x.unpack('c'*size)
end
|
#ReadChar(size, col, row) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/Win32/Console.rb', line 59
def ReadChar(size, col, row)
buffer = ' ' * size
if API.ReadConsoleOutputCharacter( @handle, buffer, size, col, row )
return buffer
else
return nil
end
end
|
#ReadRect(left, top, right, bottom) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/Win32/Console.rb', line 110
def ReadRect( left, top, right, bottom )
col = right - left + 1
row = bottom - top + 1
size = col * row
buffer = ' ' * size * 4
if API.ReadConsoleOutput( @handle, buffer, col, row, 0, 0,
left, top, right, bottom )
return buffer.unpack('axS'*size) else
return nil
end
end
|
#redirected? ⇒ Boolean
Return true if console is redirected or piped and no longer is outputing to the normal console.
This can be used to determine if normal console operations will be available.
293
294
295
|
# File 'lib/Win32/Console.rb', line 293
def redirected?
self.Mode > 31
end
|
131
132
133
134
135
136
137
|
# File 'lib/Win32/Console.rb', line 131
def Scroll( left1, top1, right1, bottom1,
col, row, char, attr,
left2, top2, right2, bottom2 )
API.ScrollConsoleScreenBuffer(@handle, left1, top1, right1, bottom1,
col, row, char, attr,
left2, top2, right2, bottom2)
end
|
#Select(type) ⇒ Object
43
44
45
|
# File 'lib/Win32/Console.rb', line 43
def Select(type)
return API.SetStdHandle(type,@handle)
end
|
#Size(*t) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/Win32/Console.rb', line 233
def Size(*t)
if t.size == 0
col, row = API.GetConsoleScreenBufferInfo(@handle )
return [col, row]
else
row = -1 if !t[1]
col = -1 if !t[0]
if col < 0 or row < 0
curr_col, curr_row = Size()
col = curr_col if col < 0
row = curr_row if row < 0
end
API.SetConsoleScreenBufferSize(@handle, row, col)
end
end
|
#Title(title = nil) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/Win32/Console.rb', line 47
def Title(title = nil)
if title
return API.SetConsoleTitle(title)
else
return API.GetConsoleTitle()
end
end
|
#Window(*t) ⇒ Object
249
250
251
252
253
254
255
256
|
# File 'lib/Win32/Console.rb', line 249
def Window(*t)
if t.size != 5
info = API.GetConsoleScreenBufferInfo( @handle )
return info[5..8]
else
API.SetConsoleWindowInfo(@handle, t[0], t[1], t[2], t[3], t[4])
end
end
|
#Write(s) ⇒ Object
102
103
104
|
# File 'lib/Win32/Console.rb', line 102
def Write(s)
API.WriteConsole( @handle, s )
end
|
#WriteAttr(attr, col, row) ⇒ Object
68
69
70
|
# File 'lib/Win32/Console.rb', line 68
def WriteAttr(attr, col, row)
API.WriteConsoleOutputAttribute( @handle, attr, col, row )
end
|
#WriteChar(s, col, row) ⇒ Object
55
56
57
|
# File 'lib/Win32/Console.rb', line 55
def WriteChar(s, col, row)
API.WriteConsoleOutputCharacter( @handle, s, col, row )
end
|
#WriteFile(s) ⇒ Object
106
107
108
|
# File 'lib/Win32/Console.rb', line 106
def WriteFile(s)
API.WriteFile( @handle, s)
end
|
202
203
204
|
# File 'lib/Win32/Console.rb', line 202
def WriteInput(*t)
API.WriteConsoleInput(@handle, *t)
end
|
#WriteRect(buffer, left, top, right, bottom) ⇒ Object
124
125
126
127
128
129
|
# File 'lib/Win32/Console.rb', line 124
def WriteRect( buffer, left, top, right, bottom )
col = right - left + 1
row = bottom - top + 1
API.WriteConsoleOutput( @handle, buffer, col, row, 0, 0,
left, top, right, bottom )
end
|