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

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



52
53
54
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
# File 'lib/monocle-print/output-device.rb', line 52

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 )

  @cursor = Pair.new( 0, 0 )
  @background_stack = []
  @foreground_stack = []
  @background = @foreground = nil
  @tab_width = options.fetch( :tab_width, 8 )

  @newline = options.fetch( :newline, $/ )
  @use_color = options.fetch( :use_color, tty? )

  @margin = Pair.new( 0, 0 )
  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.



49
50
51
# File 'lib/monocle-print/output-device.rb', line 49

def device
  @device
end

#styleObject

Returns the value of attribute style.



50
51
52
# File 'lib/monocle-print/output-device.rb', line 50

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

.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



106
107
108
109
110
111
112
113
114
# File 'lib/monocle-print/output-device.rb', line 106

def background( color = nil )
  color or return( @background_stack.last )
  begin
    @background_stack.push( color )
    yield
  ensure
    @background_stack.pop
  end
end

#clearObject



359
360
361
362
363
364
# File 'lib/monocle-print/output-device.rb', line 359

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

#clear_lineObject



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

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

#closeObject



267
268
269
# File 'lib/monocle-print/output-device.rb', line 267

def close
  @device.close rescue nil
end

#color_codeObject



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/monocle-print/output-device.rb', line 382

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



102
103
104
# File 'lib/monocle-print/output-device.rb', line 102

def colors( fg, bg )
  foreground( fg ) { background( bg ) { yield } }
end

#columnObject



351
352
353
# File 'lib/monocle-print/output-device.rb', line 351

def column
  @cursor.column
end

#column_layout(*args) ⇒ Object



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

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

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



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/monocle-print/output-device.rb', line 315

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



92
93
94
95
96
97
98
99
100
# File 'lib/monocle-print/output-device.rb', line 92

def foreground( color = nil )
  color or return( @foreground_stack.last )
  begin
    @foreground_stack.push( color )
    yield
  ensure
    @foreground_stack.pop
  end
end

#full_widthObject



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

def full_width
  screen_size.width
end

#heightObject



271
272
273
# File 'lib/monocle-print/output-device.rb', line 271

def height
  screen_size.height
end

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/monocle-print/output-device.rb', line 147

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



171
172
173
# File 'lib/monocle-print/output-device.rb', line 171

def left_margin
  @margin.left
end

#left_margin=(n) ⇒ Object



179
180
181
# File 'lib/monocle-print/output-device.rb', line 179

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

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



220
221
222
# File 'lib/monocle-print/output-device.rb', line 220

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

#lineObject



355
356
357
# File 'lib/monocle-print/output-device.rb', line 355

def line
  @cursor.line
end

#list(*args) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/monocle-print/output-device.rb', line 197

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



143
144
145
# File 'lib/monocle-print/output-device.rb', line 143

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

#newline!Object



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

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

#outdent(n) ⇒ Object



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

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


224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/monocle-print/output-device.rb', line 224

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



248
249
250
# File 'lib/monocle-print/output-device.rb', line 248

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

#put(str, options = nil) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/monocle-print/output-device.rb', line 283

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



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

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

#puts(*objs) ⇒ Object



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

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



252
253
254
# File 'lib/monocle-print/output-device.rb', line 252

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

#reset!Object



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

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

#return!Object



309
310
311
312
313
# File 'lib/monocle-print/output-device.rb', line 309

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

#right_marginObject



175
176
177
# File 'lib/monocle-print/output-device.rb', line 175

def right_margin
  @margin.right
end

#right_margin=(n) ⇒ Object



183
184
185
# File 'lib/monocle-print/output-device.rb', line 183

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

#space(n_lines = 1) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/monocle-print/output-device.rb', line 343

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

#table(*args) ⇒ Object



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

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?



134
135
136
137
# File 'lib/monocle-print/output-device.rb', line 134

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

#use_color=(v) ⇒ Object



139
140
141
# File 'lib/monocle-print/output-device.rb', line 139

def use_color= v
  @use_color = ! ! v # 'not not x' casts x to either `true' or 'false'
end

#widthObject



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

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