win32console 1.2.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/ext/extconf.rb ADDED
@@ -0,0 +1,18 @@
1
+ #################################################################
2
+ #
3
+ # To compile and install, do this:
4
+ #
5
+ # Make sure your compiling environment is in your path.
6
+ # In the case of MSVC, this involves running vcvars32.bat first
7
+ # which is located in the bin directory of MS Visual C++.
8
+ #
9
+ # Then:
10
+ #
11
+ # > ruby extconf.rb
12
+ # > nmake Makefile
13
+ # > nmake install
14
+ #
15
+ #
16
+ ##################################################################
17
+ require 'mkmf'
18
+ create_makefile('Console')
data/lib/Console.so ADDED
Binary file
@@ -0,0 +1,348 @@
1
+ #
2
+ # Win32::Console::ANSI
3
+ #
4
+ # Copyright 2004 - Gonzalo Garramuno
5
+ # Licensed under GNU General Public License or Perl's Artistic License
6
+ #
7
+ # Based on Perl's Win32::Console::ANSI
8
+ # Copyright (c) 2003 Jean-Louis Morel <jl_morel@bribes.org>
9
+ # Licensed under GNU General Public License or Perl's Artistic License
10
+ #
11
+ require "Win32/Console"
12
+
13
+
14
+ module Kernel
15
+
16
+ # Kernel#putc is equivalent to $stdout.putc, but
17
+ # it doesn't use $stdout.putc. We redefine it to do that
18
+ # so that it will buffer the escape sequences properly.
19
+ # See Win32::Console::ANSI::IO#putc
20
+ remove_method :putc
21
+ def putc(int)
22
+ $stdout.putc(int)
23
+ end
24
+
25
+ end
26
+
27
+ module Win32
28
+ class Console
29
+ module ANSI
30
+
31
+ class IO < IO
32
+
33
+ VERSION = '0.05'
34
+ DEBUG = nil
35
+
36
+ require "win32/registry"
37
+
38
+ include Win32::Console::Constants
39
+
40
+ # @todo: encode is another perl module
41
+ EncodeOk = false
42
+
43
+ # Retrieving the codepages
44
+ cpANSI = nil
45
+ Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Nls\CodePage' ) { |reg|
46
+ cpANSI = reg['ACP']
47
+ }
48
+
49
+ STDERR.puts "Unable to read Win codepage #{cpANSI}" if DEBUG && !cpANSI
50
+
51
+
52
+ cpANSI = 'cp'+(cpANSI ? cpANSI : '1252') # Windows codepage
53
+ OEM = Win32::Console::OutputCP()
54
+ cpOEM = 'cp' + OEM.to_s # DOS codepage
55
+ @@cp = cpANSI + cpOEM
56
+
57
+ STDERR.puts "EncodeOk=#{EncodeOk} cpANSI=#{cpANSI} "+
58
+ "cpOEM=#{cpOEM}" if DEBUG
59
+
60
+ @@color = { 30 => 0, # black foreground
61
+ 31 => FOREGROUND_RED, # red foreground
62
+ 32 => FOREGROUND_GREEN, # green foreground
63
+ 33 => FOREGROUND_RED|FOREGROUND_GREEN, # yellow foreground
64
+ 34 => FOREGROUND_BLUE, # blue foreground
65
+ 35 => FOREGROUND_BLUE|FOREGROUND_RED, # magenta foreground
66
+ 36 => FOREGROUND_BLUE|FOREGROUND_GREEN, # cyan foreground
67
+ 37 => FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE, # white foreground
68
+ 40 => 0, # black background
69
+ 41 => BACKGROUND_RED, # red background
70
+ 42 => BACKGROUND_GREEN, # green background
71
+ 43 => BACKGROUND_RED|BACKGROUND_GREEN, # yellow background
72
+ 44 => BACKGROUND_BLUE, # blue background
73
+ 45 => BACKGROUND_BLUE|BACKGROUND_RED, # magenta background
74
+ 46 => BACKGROUND_BLUE|BACKGROUND_GREEN, # cyan background
75
+ 47 => BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE, # white background
76
+ }
77
+
78
+ def initialize
79
+ super(1,'w')
80
+ @Out = Win32::Console.new(STD_OUTPUT_HANDLE)
81
+ @x = @y = 0 # to save cursor position
82
+ @foreground = 7
83
+ @background = 0
84
+ @bold =
85
+ @underline =
86
+ @revideo =
87
+ @concealed = nil
88
+ @conv = 1 # char conversion by default
89
+ @buffer = []
90
+ STDERR.puts "Console Mode=#{@Out.Mode}" if DEBUG
91
+ end
92
+
93
+ # this redefined #putc buffers escape sequences but passes
94
+ # other values to #write as normal.
95
+ def putc(int)
96
+ if @buffer.empty?
97
+ unless int == ?\e
98
+ write(int.chr)
99
+ else
100
+ @buffer << int
101
+ end
102
+ else
103
+ @buffer << int
104
+ case int
105
+ when ?m, ?J, ?L, ?M, ?@, ?P, ?A, ?B, ?C, ?D,
106
+ ?E, ?F, ?G, ?H, ?f, ?s, ?u, ?U, ?K, ?X
107
+ write(@buffer.pack("c*"))
108
+ @buffer.clear
109
+ end
110
+ end
111
+ end
112
+
113
+ # #write checks if $stdout is going to the console
114
+ # or if it's being redirected.
115
+ # When to the console, it passes the string to
116
+ # _PrintString to be parsed for escape codes.
117
+ #
118
+ # When redirected, it passes to WriteFile to allow escape
119
+ # codes and all to be output. The application that is
120
+ # handling the redirected IO should handle coloring.
121
+ # For Ruby applications, this means requiring Win32Conole again.
122
+ def write(*s)
123
+ if redirected?
124
+ s.each{ |x| @Out.WriteFile(x.dup.to_s) }
125
+ else
126
+ s.each{ |x| _PrintString(x) }
127
+ end
128
+ end
129
+
130
+ # returns true if outputs is being redirected.
131
+ def redirected?
132
+ @Out.Mode > 31
133
+ end
134
+
135
+ private
136
+
137
+ def _PrintString(t)
138
+ s = t.dup.to_s
139
+ while s != ''
140
+ if s.sub!( /([^\e]*)?\e([\[\(])([0-9\;\=]*)([a-zA-Z@])(.*)/s,'\5')
141
+ @Out.Write((_conv("#$1")))
142
+ if $2 == '['
143
+ case $4
144
+ when 'm' # ESC[#;#;....;#m Set display attributes
145
+ attributs = $3.split(';')
146
+ attributs.push(nil) unless attributs # ESC[m == ESC[;m ==...==ESC[0m
147
+ attributs.each do |attr|
148
+ atv = attr.to_i
149
+ case atv
150
+ when 0 # ESC[0m reset
151
+ @foreground = 7
152
+ @background = 0
153
+ @bold =
154
+ @underline =
155
+ @revideo =
156
+ @concealed = nil
157
+ when 1
158
+ @bold = 1
159
+ when 21
160
+ @bold = nil
161
+ when 4
162
+ @underline = 1
163
+ when 24
164
+ @underline = nil
165
+ when 7
166
+ @revideo = 1
167
+ when 27
168
+ @revideo = nil
169
+ when 8
170
+ @concealed = 1
171
+ when 28
172
+ @concealed = nil
173
+ when 30..37
174
+ @foreground = atv - 30
175
+ when 40..47
176
+ @background = atv - 40
177
+ end
178
+ end
179
+
180
+ if @revideo
181
+ attribut = @@color[40+@foreground] |
182
+ @@color[30+@background]
183
+ else
184
+ attribut = @@color[30+@foreground] |
185
+ @@color[40+@background]
186
+ end
187
+ attribut |= FOREGROUND_INTENSITY if @bold
188
+ attribut |= BACKGROUND_INTENSITY if @underline
189
+ @Out.Attr(attribut)
190
+ when 'J'
191
+ if !$3 or $3 == '' # ESC[0J from cursor to end of display
192
+ info = @Out.Info()
193
+ s = ' ' * ((info[1]-info[3]-1)*info[0]+info[0]-info[2]-1)
194
+ @Out.WriteChar(s, info[2], info[3])
195
+ @Out.Cursor(info[2], info[3])
196
+ elsif $3 == '1' # ESC[1J erase from start to cursor.
197
+ info = @Out.Info()
198
+ s = ' ' * (info[3]*info[0]+info[2]+1)
199
+ @Out.WriteChar(s, 0, 0)
200
+ @Out.Cursor(info[2], info[3])
201
+ elsif $3 == '2' # ESC[2J Clear screen and home cursor
202
+ @Out.Cls()
203
+ @Out.Cursor(0, 0)
204
+ else
205
+ STDERR.print "\e#$2#$3#$4" if DEBUG # if ESC-code not implemented
206
+ end
207
+ when 'K'
208
+ info = @Out.Info()
209
+ if !$3 or $3 == '' # ESC[0K Clear to end of line
210
+ s = ' ' * (info[7]-info[2]+1)
211
+ @Out.Write(s)
212
+ @Out.Cursor(info[2], info[3])
213
+ elsif $3=='1' # ESC[1K Clear from start of line to cursor
214
+ s = ' '*(info[2]+1)
215
+ @Out.WriteChar(s, 0, info[3])
216
+ @Out.Cursor(info[2], info[3])
217
+ elsif $3=='2' # ESC[2K Clear whole line.
218
+ s = ' '* info[0]
219
+ @Out.WriteChar(s, 0, info[3])
220
+ @Out.Cursor(info[2], info[3])
221
+ end
222
+ when 'L' # ESC[#L Insert # blank lines.
223
+ n = $3 == ''? 1 : $3.to_i # ESC[L == ESC[1L
224
+ info = @Out.Info()
225
+ @Out.Scroll(0, info[3], info[0]-1, info[1]-1,
226
+ 0, info[3] + n.to_i,
227
+ ' '[0], @Out.Attr(),
228
+ 0, 0, 10000, 10000)
229
+ @Out.Cursor(info[2], info[3])
230
+ when 'M' # ESC[#M Delete # line.
231
+ n = $3 == ''? 1 : $3.to_i # ESC[M == ESC[1M
232
+ info = @Out.Info();
233
+ @Out.Scroll(0, info[3]+n, info[0]-1, info[1]-1,
234
+ 0, info[3],
235
+ ' '[0], @Out.Attr(),
236
+ 0, 0, 10000, 10000)
237
+ @Out.Cursor(info[2], info[3])
238
+ when 'P' # ESC[#P Delete # characters.
239
+ n = $3 == ''? 1 : $3.to_i # ESC[P == ESC[1P
240
+ info = @Out.Info()
241
+ n = info[0]-info[2] if info[2]+n > info[0]-1
242
+ @Out.Scroll(info[2]+n, info[3] , info[0]-1, info[3],
243
+ info[2], info[3],
244
+ ' '[0], @Out.Attr(),
245
+ 0, 0, 10000, 10000)
246
+ s = ' ' * n
247
+ @Out.Cursor(info[0]-n, info[3])
248
+ @Out.Write(s)
249
+ @Out.Cursor(info[2], info[3])
250
+ when '@' # ESC[#@ Insert # blank Characters
251
+ s = ' ' * $3.to_i
252
+ info = @Out.Info()
253
+ s << @Out.ReadChar(info[7]-info[2]+1, info[2], info[3])
254
+ s = s[0..-($3.to_i)]
255
+ @Out.Write(s);
256
+ @Out.Cursor(info[2], info[3])
257
+ when 'A' # ESC[#A Moves cursor up # lines
258
+ (x, y) = @Out.Cursor()
259
+ n = $3 == ''? 1 : $3.to_i; # ESC[A == ESC[1A
260
+ @Out.Cursor(x, y-n)
261
+ when 'B' # ESC[#B Moves cursor down # lines
262
+ (x, y) = @Out.Cursor()
263
+ n = $3 == ''? 1 : $3.to_i; # ESC[B == ESC[1B
264
+ @Out.Cursor(x, y+n)
265
+ when 'C' # ESC[#C Moves cursor forward # spaces
266
+ (x, y) = @Out.Cursor()
267
+ n = $3 == ''? 1 : $3.to_i; # ESC[C == ESC[1C
268
+ @Out.Cursor(x+n, y)
269
+ when 'D' # ESC[#D Moves cursor back # spaces
270
+ (x, y) = @Out.Cursor()
271
+ n = $3 == ''? 1 : $3.to_i; # ESC[D == ESC[1D
272
+ @Out.Cursor(x-n, y)
273
+ when 'E' # ESC[#E Moves cursor down # lines, column 1.
274
+ x, y = @Out.Cursor()
275
+ n = $3 == ''? 1 : $3.to_i; # ESC[E == ESC[1E
276
+ @Out.Cursor(0, y+n)
277
+ when 'F' # ESC[#F Moves cursor up # lines, column 1.
278
+ x, y = @Out.Cursor()
279
+ n = $3 == ''? 1 : $3.to_i; # ESC[F == ESC[1F
280
+ @Out.Cursor(0, y-n)
281
+ when 'G' # ESC[#G Moves cursor column # in current row.
282
+ x, y = @Out.Cursor()
283
+ n = $3 == ''? 1 : $3.to_i; # ESC[G == ESC[1G
284
+ @Out.Cursor(n-1, y)
285
+ when 'f' # ESC[#;#f Moves cursor to line #, column #
286
+ y, x = $3.split(';')
287
+ x = 1 unless x # ESC[;5H == ESC[1;5H ...etc
288
+ y = 1 unless y
289
+ @Out.Cursor(x.to_i-1, y.to_i-1) # origin (0,0) in DOS console
290
+ when 'H' # ESC[#;#H Moves cursor to line #, column #
291
+ y, x = $3.split(';')
292
+ x = 1 unless x # ESC[;5H == ESC[1;5H ...etc
293
+ y = 1 unless y
294
+ @Out.Cursor(x.to_i-1, y.to_i-1) # origin (0,0) in DOS console
295
+ when 's' # ESC[s Saves cursor position for recall later
296
+ (@x, @y) = @Out.Cursor()
297
+ when 'u' # ESC[u Return to saved cursor position
298
+ @Out.Cursor(@x, @y)
299
+ when 'U' # ESC(U no mapping
300
+ @conv = nil
301
+ when 'K' # ESC(K mapping if it exist
302
+ @Out.OutputCP(OEM) # restore original codepage
303
+ @conv = 1
304
+ when 'X' # ESC(#X codepage **EXPERIMENTAL**
305
+ @conv = nil
306
+ @Out.OutputCP($3)
307
+ else
308
+ STDERR.puts "\e#$2#$3#$4 not implemented" if DEBUG # ESC-code not implemented
309
+ end
310
+ end
311
+ else
312
+ @Out.Write(_conv(s))
313
+ s=''
314
+ end
315
+ end
316
+ end
317
+
318
+ def _conv(s)
319
+ if @concealed
320
+ s.gsub!( /\S/,' ')
321
+ elsif @conv
322
+ if EncodeOk
323
+ from_to(s, cpANSI, cpOEM)
324
+ elsif @@cp == 'cp1252cp850' # WinLatin1 --> DOSLatin1
325
+ s.tr!("���������������������������������������������������������������������������������������������������ÁāŁƁǁȁɁʁˁ́́΁ρЁсҁӁԁՁցׁ؁فځہ܁݁ށ߁��������������������������������������������","???�??????????????????????????��������ρ��݁�����������������������������������������󁨁������ǎ����Ԑ�ҁӁށցׁ؁с������噞����ꚁ��ᅁ���Ƅ�������������Ё������䔁����������")
326
+ elsif @@cp == 'cp1252cp437' # WinLatin1 --> DOSLatinUS
327
+ s.tr!("���������������������������������������������������������������������������������������������������ÁāŁƁǁȁɁʁˁ́́΁ρЁсҁӁԁՁցׁ؁فځہ܁݁ށ߁��������������������������������������������", "??????????????????????????????������?�????������???�����??��?��??��������?��????����?�???????��????�?????�??�ᅁ��?�������������?������?���?�����??�")
328
+ elsif @@cp == 'cp1250cp852' # WinLatin2 --> DOSLatin2
329
+ s.tr!("���������������������������������������������������������������������������������������������������ÁāŁƁǁȁɁʁˁ́́΁ρЁсҁӁԁՁցׁ؁فځہ܁݁ށ߁��������������������������������������������",
330
+ "??????????��?��??????????��?������������ρ�?����?��������?����?���???����������񖁾�聵���Ǝ���������Ӂ��ցׁҁс�Ձ��⊙����ށ�뚁�݁�ꁠ��DŽ���������؁���ԁЁ�偢�����������������" )
331
+ elsif @@cp == 'cp1251cp855' # WinCyrillic --> DOSCyrillic
332
+ s.tr!("���������������������������������������������������������������������������������������������������ÁāŁƁǁȁɁʁˁ́́΁ρЁсҁӁԁՁցׁ؁فځہ܁݁ށ߁��������������������������������������������",
333
+ "��?�??????�?����?????????�?����������??���?���?��?�??��????������������쁭������􁸁��ǁсӁՁׁ݁���聫�����������������������끬������󁷁��ƁЁҁԁց؁���灪������������������")
334
+ end
335
+ end
336
+ return s
337
+ end
338
+
339
+ end
340
+
341
+ # end print overloading
342
+
343
+ end
344
+ end
345
+ end
346
+
347
+ $stdout = Win32::Console::ANSI::IO.new()
348
+