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.
55
56
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
|
# File 'lib/monocle-print/output-device.rb', line 55
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.
52
53
54
|
# File 'lib/monocle-print/output-device.rb', line 52
def device
@device
end
|
#style ⇒ Object
Returns the value of attribute style.
53
54
55
|
# File 'lib/monocle-print/output-device.rb', line 53
def style
@style
end
|
Class Method Details
.buffer(options = {}) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/monocle-print/output-device.rb', line 16
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
10
11
12
13
14
|
# File 'lib/monocle-print/output-device.rb', line 10
def self.open( path, options = {} )
File.open( path, options.fetch( :mode, 'w' ) ) do | file |
return( yield( new( file, options ) ) )
end
end
|
.stderr(options = {}) ⇒ Object
41
42
43
44
|
# File 'lib/monocle-print/output-device.rb', line 41
def self.stderr( options = {} )
device = new( $stderr, options )
block_given? ? yield( device ) : device
end
|
.stdout(options = {}) ⇒ Object
36
37
38
39
|
# File 'lib/monocle-print/output-device.rb', line 36
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
111
112
113
114
115
116
117
118
119
|
# File 'lib/monocle-print/output-device.rb', line 111
def background( color = nil )
color or return( @background_stack.last )
begin
@background_stack.push( color )
yield
ensure
@background_stack.pop
end
end
|
#clear ⇒ Object
364
365
366
367
368
369
|
# File 'lib/monocle-print/output-device.rb', line 364
def clear
@cursor.line = @cursor.column = 0
@device.print( set_cursor( 0, 0 ), clear_screen )
@device.flush
return( self )
end
|
#clear_line ⇒ Object
371
372
373
374
375
|
# File 'lib/monocle-print/output-device.rb', line 371
def clear_line
@device.print( super )
@device.flush
return!
end
|
#close ⇒ Object
272
273
274
|
# File 'lib/monocle-print/output-device.rb', line 272
def close
@device.close rescue nil
end
|
#color_code ⇒ Object
387
388
389
390
391
392
393
394
395
396
397
398
399
|
# File 'lib/monocle-print/output-device.rb', line 387
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
107
108
109
|
# File 'lib/monocle-print/output-device.rb', line 107
def colors( fg, bg )
foreground( fg ) { background( bg ) { yield } }
end
|
#column ⇒ Object
356
357
358
|
# File 'lib/monocle-print/output-device.rb', line 356
def column
@cursor.column
end
|
#column_layout(*args) ⇒ Object
218
219
220
221
222
223
|
# File 'lib/monocle-print/output-device.rb', line 218
def column_layout( *args )
ColumnLayout.new( *args ) do | t |
block_given? and yield( t )
t.render( self )
end
end
|
#fill(width, char = ' ') ⇒ Object
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
# File 'lib/monocle-print/output-device.rb', line 320
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
97
98
99
100
101
102
103
104
105
|
# File 'lib/monocle-print/output-device.rb', line 97
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
280
281
282
|
# File 'lib/monocle-print/output-device.rb', line 280
def full_width
screen_size.width
end
|
#height ⇒ Object
276
277
278
|
# File 'lib/monocle-print/output-device.rb', line 276
def height
screen_size.height
end
|
#indent(n = 0, m = 0) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/monocle-print/output-device.rb', line 152
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
176
177
178
|
# File 'lib/monocle-print/output-device.rb', line 176
def left_margin
@margin.left
end
|
#left_margin=(n) ⇒ Object
184
185
186
|
# File 'lib/monocle-print/output-device.rb', line 184
def left_margin= n
@margin.left = n.to_i
end
|
#leger(char = '<h>', w = width) ⇒ Object
225
226
227
|
# File 'lib/monocle-print/output-device.rb', line 225
def leger( char = '<h>', w = width )
puts( @style.format( char ).tile( w ) )
end
|
#line ⇒ Object
360
361
362
|
# File 'lib/monocle-print/output-device.rb', line 360
def line
@cursor.line
end
|
#list(*args) ⇒ Object
202
203
204
205
206
207
208
209
|
# File 'lib/monocle-print/output-device.rb', line 202
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
148
149
150
|
# File 'lib/monocle-print/output-device.rb', line 148
def margin= n
self.left_margin = self.right_margin = Utils.at_least( n.to_i, 0 )
end
|
#newline! ⇒ Object
341
342
343
344
345
346
|
# File 'lib/monocle-print/output-device.rb', line 341
def newline!
+@cursor
@device.print( @newline )
@device.flush
self
end
|
#outdent(n) ⇒ Object
168
169
170
171
172
173
174
|
# File 'lib/monocle-print/output-device.rb', line 168
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
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/monocle-print/output-device.rb', line 229
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
253
254
255
|
# File 'lib/monocle-print/output-device.rb', line 253
def printf( fmt, *args )
print( sprintf( fmt, *args ) )
end
|
#put(str, options = nil) ⇒ Object
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/monocle-print/output-device.rb', line 288
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
309
310
311
312
|
# File 'lib/monocle-print/output-device.rb', line 309
def put!( str, options = nil )
put( str, options )
newline!
end
|
#puts(*objs) ⇒ Object
243
244
245
246
247
248
249
250
251
|
# File 'lib/monocle-print/output-device.rb', line 243
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
257
258
259
|
# File 'lib/monocle-print/output-device.rb', line 257
def putsf( fmt, *args )
puts( sprintf( fmt, *args ) )
end
|
#reset! ⇒ Object
192
193
194
195
196
197
198
|
# File 'lib/monocle-print/output-device.rb', line 192
def reset!
@fg_stack.clear
@bg_stack.clear
@margin = Pair.new( 0, 0 )
@cursor = Pair.new( 0, 0 )
@screen_size = nil
end
|
#return! ⇒ Object
314
315
316
317
318
|
# File 'lib/monocle-print/output-device.rb', line 314
def return!
~@cursor
@device.print("\r")
@device.flush
end
|
#right_margin ⇒ Object
180
181
182
|
# File 'lib/monocle-print/output-device.rb', line 180
def right_margin
@margin.right
end
|
#right_margin=(n) ⇒ Object
188
189
190
|
# File 'lib/monocle-print/output-device.rb', line 188
def right_margin= n
@margin.right = n.to_i
end
|
#space(n_lines = 1) ⇒ Object
348
349
350
351
352
353
354
|
# File 'lib/monocle-print/output-device.rb', line 348
def space( n_lines = 1 )
n_lines.times do
put( '' )
newline!
end
self
end
|
#table(*args) ⇒ Object
211
212
213
214
215
216
|
# File 'lib/monocle-print/output-device.rb', line 211
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?
139
140
141
142
|
# File 'lib/monocle-print/output-device.rb', line 139
def use_color( v = nil )
v.nil? or self.use_color = v
return( @use_color )
end
|
#use_color=(v) ⇒ Object
144
145
146
|
# File 'lib/monocle-print/output-device.rb', line 144
def use_color= v
@use_color = !!v
end
|
#width ⇒ Object
284
285
286
|
# File 'lib/monocle-print/output-device.rb', line 284
def width
screen_size.width - @margin.left - @margin.right
end
|