Class: MonoclePrint::OutputDevice

Inherits:
IO
  • Object
show all
Includes:
MonoclePrint, TerminalEscapes
Defined in:
lib/monocle-print/output-device.rb

Direct Known Subclasses

Pager, Progress

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

Constants included from TerminalEscapes

TerminalEscapes::ANSI_COLORS, TerminalEscapes::ANSI_COLOR_NAMES, TerminalEscapes::ANSI_MODIFIERS, TerminalEscapes::ANSI_MODIFIER_NAMES

Constants included from MonoclePrint

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

Methods included from TerminalEscapes

ansi_color, ansi_modifier, 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

Methods included from MonoclePrint

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
96
97
# 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 = []
  @modifier_stack   = []

  @background       = @foreground = @modifier = 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

#deviceObject (readonly)

Returns the value of attribute device.



52
53
54
# File 'lib/monocle-print/output-device.rb', line 52

def device
  @device
end

#styleObject

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



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

#clearObject



387
388
389
390
391
392
# File 'lib/monocle-print/output-device.rb', line 387

def clear
  @cursor.line = @cursor.column = 0
  @device.print( set_cursor( 0, 0 ), clear_screen )
  @device.flush
  return( self )
end

#clear_lineObject



394
395
396
397
398
# File 'lib/monocle-print/output-device.rb', line 394

def clear_line
  @device.print( super )
  @device.flush
  return!
end

#closeObject



295
296
297
# File 'lib/monocle-print/output-device.rb', line 295

def close
  @device.close rescue nil
end

#color_codeObject



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/monocle-print/output-device.rb', line 409

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

  case mod = @modifier_stack.last
  when String, Symbol then code << ansi_modifier( mod )
  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

#columnObject



379
380
381
# File 'lib/monocle-print/output-device.rb', line 379

def column
  @cursor.column
end

#column_layout(*args) ⇒ Object



238
239
240
241
242
243
# File 'lib/monocle-print/output-device.rb', line 238

def column_layout( *args )
  ColumnLayout.new( *args ) do | t |
    block_given? and yield( t )
    t.render( self )
  end
end

#fill(width, char = ' ') ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/monocle-print/output-device.rb', line 343

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_widthObject



303
304
305
# File 'lib/monocle-print/output-device.rb', line 303

def full_width
  screen_size.width
end

#heightObject



299
300
301
# File 'lib/monocle-print/output-device.rb', line 299

def height
  screen_size.height
end

#indent(n = 0, m = 0) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/monocle-print/output-device.rb', line 172

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_marginObject



196
197
198
# File 'lib/monocle-print/output-device.rb', line 196

def left_margin
  @margin.left
end

#left_margin=(n) ⇒ Object



204
205
206
# File 'lib/monocle-print/output-device.rb', line 204

def left_margin= n
  @margin.left = n.to_i
end

#leger(char = '<h>', w = width) ⇒ Object



245
246
247
# File 'lib/monocle-print/output-device.rb', line 245

def leger( char = '<h>', w = width )
  puts( @style.format( char ).tile( w ) )
end

#lineObject



383
384
385
# File 'lib/monocle-print/output-device.rb', line 383

def line
  @cursor.line
end

#list(*args) ⇒ Object



222
223
224
225
226
227
228
229
# File 'lib/monocle-print/output-device.rb', line 222

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



168
169
170
# File 'lib/monocle-print/output-device.rb', line 168

def margin= n
  self.left_margin = self.right_margin = Utils.at_least( n.to_i, 0 )
end

#modifier(name = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/monocle-print/output-device.rb', line 125

def modifier( name = nil )
  name or return( @modifier_stack.last )
  begin
    @modifier_stack.push( name )
    yield
  ensure
    @modifier_stack.pop
  end
end

#newline!Object



364
365
366
367
368
369
# File 'lib/monocle-print/output-device.rb', line 364

def newline!
  +@cursor
  @device.print( @newline )
  @device.flush
  self
end

#outdent(n) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/monocle-print/output-device.rb', line 188

def outdent( n )
  self.left_margin -= n.to_i
  block_given? and
    begin yield
    ensure self.left_margin += n.to_i
    end
end


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/monocle-print/output-device.rb', line 249

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 )
  @device.print( clear_attr )
  
  self
end

#printf(fmt, *args) ⇒ Object



276
277
278
# File 'lib/monocle-print/output-device.rb', line 276

def printf( fmt, *args )
  print( sprintf( fmt, *args ) )
end

#put(str, options = nil) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/monocle-print/output-device.rb', line 311

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



332
333
334
335
# File 'lib/monocle-print/output-device.rb', line 332

def put!( str, options = nil )
  put( str, options )
  newline!
end

#puts(*objs) ⇒ Object



266
267
268
269
270
271
272
273
274
# File 'lib/monocle-print/output-device.rb', line 266

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



280
281
282
# File 'lib/monocle-print/output-device.rb', line 280

def putsf( fmt, *args )
  puts( sprintf( fmt, *args ) )
end

#reset!Object



212
213
214
215
216
217
218
# File 'lib/monocle-print/output-device.rb', line 212

def reset!
  @fg_stack.clear
  @bg_stack.clear
  @margin = Pair.new( 0, 0 )
  @cursor = Pair.new( 0, 0 )
  @screen_size = nil
end

#return!Object



337
338
339
340
341
# File 'lib/monocle-print/output-device.rb', line 337

def return!
  ~@cursor
  @device.print("\r")
  @device.flush
end

#right_marginObject



200
201
202
# File 'lib/monocle-print/output-device.rb', line 200

def right_margin
  @margin.right
end

#right_margin=(n) ⇒ Object



208
209
210
# File 'lib/monocle-print/output-device.rb', line 208

def right_margin= n
  @margin.right = n.to_i
end

#space(n_lines = 1) ⇒ Object



371
372
373
374
375
376
377
# File 'lib/monocle-print/output-device.rb', line 371

def space( n_lines = 1 )
  n_lines.times do
    put( '' )
    newline!
  end
  self
end

#table(*args) ⇒ Object



231
232
233
234
235
236
# File 'lib/monocle-print/output-device.rb', line 231

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?



159
160
161
162
# File 'lib/monocle-print/output-device.rb', line 159

def use_color( v = nil )
  v.nil? or self.use_color = v
  return( @use_color )
end

#use_color=(v) ⇒ Object



164
165
166
# File 'lib/monocle-print/output-device.rb', line 164

def use_color= v
  @use_color = !!v
end

#widthObject



307
308
309
# File 'lib/monocle-print/output-device.rb', line 307

def width
  screen_size.width - @margin.left - @margin.right
end