Class: MonoclePrint::OutputDevice
Constant Summary
collapse
- DEFAULT_SIZE =
Pair.new( 80, 22 ).freeze
- IO_PRINT_METHODS =
%w( puts print printf putc )
- SIZE_IOCTL =
0x5413
- SIZE_STRUCT =
[ 0, 0, 0, 0 ].pack( "SSSS" ).freeze
TerminalEscapes::ANSI_COLORS
COLOR_ESCAPE, FOUR_BYTES, MULTIBYTE_CHARACTER, ONE_BYTE, THREE_BYTES, TWO_BYTES, VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
ansi_color, blink, bold, clear_attr, clear_down, clear_left, clear_line, clear_right, clear_screen, clear_up, conceal, cursor_backward, cursor_down, cursor_forward, cursor_up, dobule_height_bottom, double_height_top, double_width, konsole_color, restore_cursor, reverse, save_cursor, set_cursor, single_width, underline, xterm_color
Line, Output, Rectangle, Style, Text, included, library_path, version
Constructor Details
#initialize(output, options = {}) ⇒ OutputDevice
Returns a new instance of OutputDevice.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/monocle-print/output-device.rb', line 57
def initialize( output, options = {} )
@device =
case output
when String then File.open( output, options.fetch( :mode, 'w' ) )
when IO then output
else
if IO_PRINT_METHODS.all? { | m | output.respond_to?( m ) }
output
else
msg = "%s requires an IO-like object, but was given: %p" % [ self.class, output ]
raise( ArgumentError, msg )
end
end
super( @device )
@background_stack = []
@foreground_stack = []
@background = @foreground = nil
@use_color = options.fetch( :use_color, tty? )
@cursor = Pair.new( 0, 0 )
@tab_width = options.fetch( :tab_width, 8 )
@margin = Pair.new( 0, 0 )
@newline = options.fetch( :newline, $/ )
case margin = options.fetch( :margin, 0 )
when Numeric then @margin.left = @margin.right = margin.to_i
else
@margin.left = margin[ :left ].to_i
@margin.right = margin[ :right ].to_i
end
@tabs = {}
@screen_size = nil
@forced_width = options[ :width ]
@forced_height = options[ :height ]
@alignment = options.fetch( :alignment, :left )
@style = Style( options[ :style ] )
end
|
Instance Attribute Details
#device ⇒ Object
Returns the value of attribute device.
54
55
56
|
# File 'lib/monocle-print/output-device.rb', line 54
def device
@device
end
|
#style ⇒ Object
Returns the value of attribute style.
55
56
57
|
# File 'lib/monocle-print/output-device.rb', line 55
def style
@style
end
|
Class Method Details
.buffer(options = {}) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/monocle-print/output-device.rb', line 18
def self.buffer( options = {} )
case options
when Numeric then options = { :width => options }
when nil then options = {}
end
buffer = StringIO.new( '', options.fetch( :mode, 'w' ) )
out = new( buffer, options )
if block_given?
begin
yield( out )
return( out.string )
ensure
out.close
end
else
return out
end
end
|
.open(path, options = {}) ⇒ Object
12
13
14
15
16
|
# File 'lib/monocle-print/output-device.rb', line 12
def self.open( path, options = {} )
File.open( path, options.fetch( :mode, 'w' ) ) do | file |
return( yield( new( file, options ) ) )
end
end
|
.stderr(options = {}) ⇒ Object
43
44
45
46
|
# File 'lib/monocle-print/output-device.rb', line 43
def self.stderr( options = {} )
device = new( $stderr, options )
block_given? ? yield( device ) : device
end
|
.stdout(options = {}) ⇒ Object
38
39
40
41
|
# File 'lib/monocle-print/output-device.rb', line 38
def self.stdout( options = {} )
device = new( $stdout, options )
block_given? ? yield( device ) : device
end
|
Instance Method Details
#background(color = nil) ⇒ Object
Also known as:
on
113
114
115
116
117
118
119
120
121
|
# File 'lib/monocle-print/output-device.rb', line 113
def background( color = nil )
color or return( @background_stack.last )
begin
@background_stack.push( color )
yield
ensure
@background_stack.pop
end
end
|
#clear ⇒ Object
366
367
368
369
370
371
|
# File 'lib/monocle-print/output-device.rb', line 366
def clear
@cursor.line = @cursor.column = 0
@device.print( set_cursor( 0, 0 ), clear_screen )
@device.flush
return( self )
end
|
#clear_line ⇒ Object
373
374
375
376
377
|
# File 'lib/monocle-print/output-device.rb', line 373
def clear_line
@device.print( super )
@device.flush
return!
end
|
#close ⇒ Object
274
275
276
|
# File 'lib/monocle-print/output-device.rb', line 274
def close
@device.close rescue nil
end
|
#color_code ⇒ Object
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/monocle-print/output-device.rb', line 389
def color_code
@use_color or return ''
code = ''
case fg = @foreground_stack.last
when Fixnum then code << xterm_color( ?f, fg )
when String, Symbol then code << ansi_color( ?f, fg )
end
case bg = @background_stack.last
when Fixnum then code << xterm_color( ?b, bg )
when String, Symbol then code << ansi_color( ?b, bg )
end
code
end
|
#colors(fg, bg) ⇒ Object
109
110
111
|
# File 'lib/monocle-print/output-device.rb', line 109
def colors( fg, bg )
foreground( fg ) { background( bg ) { yield } }
end
|
#column ⇒ Object
358
359
360
|
# File 'lib/monocle-print/output-device.rb', line 358
def column
@cursor.column
end
|
#column_layout(*args) ⇒ Object
220
221
222
223
224
225
|
# File 'lib/monocle-print/output-device.rb', line 220
def column_layout( *args )
ColumnLayout.new( *args ) do | t |
block_given? and yield( t )
t.render( self )
end
end
|
#fill(width, char = ' ') ⇒ Object
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/monocle-print/output-device.rb', line 322
def fill( width, char = ' ' )
width =
case width
when Symbol, String
distance_to( width )
when Fixnum
Utils.at_least( width, 0 )
end
if width > 0
fill_str =
if char.length > 1 and ( char = SingleLine( char ) ).width > 1
char.tile( width )
else
char * width
end
@cursor.column += width
@device.print( fill_str )
end
self
end
|
#foreground(color = nil) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/monocle-print/output-device.rb', line 99
def foreground( color = nil )
color or return( @foreground_stack.last )
begin
@foreground_stack.push( color )
yield
ensure
@foreground_stack.pop
end
end
|
#full_width ⇒ Object
282
283
284
|
# File 'lib/monocle-print/output-device.rb', line 282
def full_width
screen_size.width
end
|
#height ⇒ Object
278
279
280
|
# File 'lib/monocle-print/output-device.rb', line 278
def height
screen_size.height
end
|
#indent(n = 0, m = 0) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/monocle-print/output-device.rb', line 154
def indent( n = 0, m = 0 )
n, m = n.to_i, m.to_i
self.left_margin += n
self.right_margin += m
if block_given?
begin
yield
ensure
self.left_margin -= n
self.right_margin -= m
end
else
self
end
end
|
#left_margin ⇒ Object
178
179
180
|
# File 'lib/monocle-print/output-device.rb', line 178
def left_margin
@margin.left
end
|
#left_margin=(n) ⇒ Object
186
187
188
|
# File 'lib/monocle-print/output-device.rb', line 186
def left_margin= n
@margin.left = n.to_i
end
|
#leger(char = '<h>', w = width) ⇒ Object
227
228
229
|
# File 'lib/monocle-print/output-device.rb', line 227
def leger( char = '<h>', w = width )
puts( @style.format( char ).tile( w ) )
end
|
#line ⇒ Object
362
363
364
|
# File 'lib/monocle-print/output-device.rb', line 362
def line
@cursor.line
end
|
#list(*args) ⇒ Object
204
205
206
207
208
209
210
211
|
# File 'lib/monocle-print/output-device.rb', line 204
def list( *args )
List.new( *args ) do | list |
list.output = self
block_given? and yield( list )
list.render
end
return( self )
end
|
#margin=(n) ⇒ Object
150
151
152
|
# File 'lib/monocle-print/output-device.rb', line 150
def margin= n
self.left_margin = self.right_margin = Utils.at_least( n.to_i, 0 )
end
|
#newline! ⇒ Object
343
344
345
346
347
348
|
# File 'lib/monocle-print/output-device.rb', line 343
def newline!
+@cursor
@device.print( @newline )
@device.flush
self
end
|
#outdent(n) ⇒ Object
170
171
172
173
174
175
176
|
# File 'lib/monocle-print/output-device.rb', line 170
def outdent( n )
self.left_margin -= n.to_i
block_given? and
begin yield
ensure self.left_margin += n.to_i
end
end
|
#print(*objs) ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/monocle-print/output-device.rb', line 231
def print( *objs )
text = Text( [ objs ].flatten!.join )
@use_color or text.bleach!
last_line = text.pop
for line in text
put!( line )
end
fill( @margin.left )
@device.print( color_code )
@cursor + last_line.width
@device.print( last_line )
self
end
|
#printf(fmt, *args) ⇒ Object
255
256
257
|
# File 'lib/monocle-print/output-device.rb', line 255
def printf( fmt, *args )
print( sprintf( fmt, *args ) )
end
|
#put(str, options = nil) ⇒ Object
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/monocle-print/output-device.rb', line 290
def put( str, options = nil )
if options
fill = @style.format( options.fetch( :fill, ' ' ) )
align = options.fetch( :align, @alignment )
else
fill, align = ' ', @alignment
end
str = SingleLine.new( str ).align!( align, width, fill )
code = color_code
str.gsub!( /\e\[0m/ ) { "\e[0m" << code }
fill( @margin.left )
@device.print( code )
@device.print( str )
@cursor + str.width
@device.print( clear_attr )
fill( screen_size.width - @cursor.column )
self
end
|
#put!(str, options = nil) ⇒ Object
311
312
313
314
|
# File 'lib/monocle-print/output-device.rb', line 311
def put!( str, options = nil )
put( str, options )
newline!
end
|
#puts(*objs) ⇒ Object
245
246
247
248
249
250
251
252
253
|
# File 'lib/monocle-print/output-device.rb', line 245
def puts( *objs )
text = Text( [ objs ].flatten!.join( @newline ) )
( text.empty? or text.last.empty? ) and text << Line( '' )
for line in text
put( line )
newline!
end
self
end
|
#putsf(fmt, *args) ⇒ Object
259
260
261
|
# File 'lib/monocle-print/output-device.rb', line 259
def putsf( fmt, *args )
puts( sprintf( fmt, *args ) )
end
|
#reset! ⇒ Object
194
195
196
197
198
199
200
|
# File 'lib/monocle-print/output-device.rb', line 194
def reset!
@fg_stack.clear
@bg_stack.clear
@margin = Pair.new( 0, 0 )
@cursor = Pair.new( 0, 0 )
@screen_size = nil
end
|
#return! ⇒ Object
316
317
318
319
320
|
# File 'lib/monocle-print/output-device.rb', line 316
def return!
~@cursor
@device.print("\r")
@device.flush
end
|
#right_margin ⇒ Object
182
183
184
|
# File 'lib/monocle-print/output-device.rb', line 182
def right_margin
@margin.right
end
|
#right_margin=(n) ⇒ Object
190
191
192
|
# File 'lib/monocle-print/output-device.rb', line 190
def right_margin= n
@margin.right = n.to_i
end
|
#space(n_lines = 1) ⇒ Object
350
351
352
353
354
355
356
|
# File 'lib/monocle-print/output-device.rb', line 350
def space( n_lines = 1 )
n_lines.times do
put( '' )
newline!
end
self
end
|
#table(*args) ⇒ Object
213
214
215
216
217
218
|
# File 'lib/monocle-print/output-device.rb', line 213
def table( *args )
Table.new( *args ) do | t |
block_given? and yield( t )
t.render( self )
end
end
|
#use_color(v = nil) ⇒ Object
Also known as:
use_color?
141
142
143
144
|
# File 'lib/monocle-print/output-device.rb', line 141
def use_color( v = nil )
v.nil? or self.use_color = v
return( @use_color )
end
|
#use_color=(v) ⇒ Object
146
147
148
|
# File 'lib/monocle-print/output-device.rb', line 146
def use_color= v
@use_color = !!v
end
|
#width ⇒ Object
286
287
288
|
# File 'lib/monocle-print/output-device.rb', line 286
def width
screen_size.width - @margin.left - @margin.right
end
|