Class: MonoclePrint::Text
- Inherits:
-
Array
- Object
- Array
- MonoclePrint::Text
show all
- Includes:
- MonoclePrint
- Defined in:
- lib/monocle-print/atomic.rb
Constant Summary
collapse
- @@width =
{}
- @@uniform =
{}
COLOR_ESCAPE, FOUR_BYTES, MULTIBYTE_CHARACTER, ONE_BYTE, THREE_BYTES, TWO_BYTES, VERSION
Class Method Summary
collapse
Instance Method Summary
collapse
Line, Output, Rectangle, Style, Text, buffer, included, library_path, stderr, stdout, version
Constructor Details
#initialize(lines = nil, default = nil) ⇒ Text
Returns a new instance of Text.
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/monocle-print/atomic.rb', line 255
def initialize( lines = nil, default = nil )
case lines
when Fixnum
if block_given?
super( lines ) { | i | Line( yield( i ) ) }
else
default = Line( default )
super( lines, default )
end
when Text then super( lines )
when Array
super( lines.join( $/ ).map { | l | Line( l.chomp! || l ) } )
when SingleLine then super(1, lines)
when nil then super()
else
super( lines.to_s.lines.map { |l| Line( l.chomp! || l ) } )
end
end
|
Class Method Details
.clear_cache ⇒ Object
245
246
247
248
249
|
# File 'lib/monocle-print/atomic.rb', line 245
def self.clear_cache
@@width.clear
@@uniform.clear
return( self )
end
|
Instance Method Details
#`(str) ⇒ Object
‘ # comment here cos my editor’s colorizing freaks out
456
457
458
|
# File 'lib/monocle-print/atomic.rb', line 456
def `(str) SingleLine.new( str )
end
|
#align(alignment, width, fill = ' ') ⇒ Object
280
281
282
|
# File 'lib/monocle-print/atomic.rb', line 280
def align( alignment, width, fill = ' ' )
dup.align!( alignment, width, fill )
end
|
#align!(alignment, width, fill = ' ') ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/monocle-print/atomic.rb', line 284
def align!( alignment, width, fill = ' ' )
width = Utils.at_least( width, self.width )
if empty?
self << SingleLine.new( ' ' * width )
else
map! do | line |
line.align( alignment, width, fill )
end
end
return( self )
end
|
#bleach ⇒ Object
300
301
302
|
# File 'lib/monocle-print/atomic.rb', line 300
def bleach
dup.bleach!
end
|
#bleach! ⇒ Object
304
305
306
|
# File 'lib/monocle-print/atomic.rb', line 304
def bleach!
each { | l | l.bleach! }
end
|
#fix ⇒ Object
308
309
310
|
# File 'lib/monocle-print/atomic.rb', line 308
def fix
dup.fix!
end
|
#fix! ⇒ Object
312
313
314
|
# File 'lib/monocle-print/atomic.rb', line 312
def fix!
align!( :left, width )
end
|
#fixed_indent(level = 0) ⇒ Object
322
323
324
|
# File 'lib/monocle-print/atomic.rb', line 322
def fixed_indent( level = 0 )
dup.fixed_indent!( level )
end
|
#fixed_indent!(level = 0) ⇒ Object
316
317
318
319
320
|
# File 'lib/monocle-print/atomic.rb', line 316
def fixed_indent!( level = 0 )
level = Utils.at_least( level, 0 )
offset = level - level_of_indent
indent!( offset )
end
|
#frame(graphic_style) ⇒ Object
342
343
344
|
# File 'lib/monocle-print/atomic.rb', line 342
def frame( graphic_style )
dup.frame!( graphic_style )
end
|
#frame!(graphic_style) ⇒ Object
346
347
348
349
350
351
352
353
354
355
356
|
# File 'lib/monocle-print/atomic.rb', line 346
def frame!( graphic_style )
top = graphic_style.box_top( width )
bottom = graphic_style.box_bottom( width )
for line in self
line.insert( 0, graphic_style.v )
line.insert( -1, graphic_style.v )
end
unshift( top )
push( bottom )
return( self )
end
|
#indent(spaces) ⇒ Object
338
339
340
|
# File 'lib/monocle-print/atomic.rb', line 338
def indent( spaces )
dup.indent!( spaces )
end
|
#indent!(spaces) ⇒ Object
333
334
335
336
|
# File 'lib/monocle-print/atomic.rb', line 333
def indent!( spaces )
for line in self do line.indent!( spaces ) end
self
end
|
#initialize_copy(orig) ⇒ Object
274
275
276
277
278
|
# File 'lib/monocle-print/atomic.rb', line 274
def initialize_copy( orig )
for line in orig
self << line.dup
end
end
|
#inspect ⇒ Object
358
359
360
361
362
363
364
|
# File 'lib/monocle-print/atomic.rb', line 358
def inspect
digits = Math.log10( Utils.at_least( length, 1 ) ).floor + 1
$/ + each_with_index.map do | line, i |
line_no = i.to_s.rjust( digits )
"#{ line_no } | #{ line }"
end.join( $/ ) + $/
end
|
#juxtapose(text, joint = ' ') ⇒ Object
Also known as:
|
366
367
368
|
# File 'lib/monocle-print/atomic.rb', line 366
def juxtapose( text, joint = ' ' )
dup.juxtapose!( text, joint )
end
|
#juxtapose!(text, joint = ' ') ⇒ Object
370
371
372
373
374
375
376
377
378
|
# File 'lib/monocle-print/atomic.rb', line 370
def juxtapose!( text, joint = ' ' )
text = self.class.new( text ).fix!.valign!( :top, height )
valign!( :top, text.height ); fix!
zip( text ) do | left, right |
left << joint.to_s << right
end
return( self )
end
|
#level_of_indent ⇒ Object
326
327
328
329
330
331
|
# File 'lib/monocle-print/atomic.rb', line 326
def level_of_indent
level = nil
levels = map { | line | line.blank? ? nil : line.level_of_indent }
levels.compact!
levels.min || 0
end
|
#pad(padding) ⇒ Object
380
381
382
|
# File 'lib/monocle-print/atomic.rb', line 380
def pad( padding )
dup.pad!( padding )
end
|
#pad!(padding) ⇒ Object
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/monocle-print/atomic.rb', line 384
def pad!( padding )
padding.top.times { unshift( Line('') ) }
padding.bottom.times { push( Line('') ) }
w = width
for line in self
line.left!( w )
line.pad!( padding.left, padding.right )
end
self
end
|
#reflow(paragraph_style = true) ⇒ Object
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
# File 'lib/monocle-print/atomic.rb', line 395
def reflow( paragraph_style = true )
if paragraph_style
cursor, new = 0, self.class.new
text = self.to_s
while text =~ /\n\s*\n/
before, text = $`, $'
new << `#{ before.gsub!( /[ \t]*\n/, ' ' ) || before }`
new << ``
end
new << `#{ text.gsub!( /[ \t]*\n/, ' ' ) || text }`
return( new )
else
text = self.to_s
text.strip!
text.gsub!( /\s+/, ' ' )
self.class.new( text )
end
end
|
#reflow!(paragraph_style = true) ⇒ Object
414
415
416
|
# File 'lib/monocle-print/atomic.rb', line 414
def reflow!( paragraph_style = true )
replace( reflow( paragraph_style ) )
end
|
#to_s ⇒ Object
424
425
426
|
# File 'lib/monocle-print/atomic.rb', line 424
def to_s
join( $/ )
end
|
428
429
430
431
432
|
# File 'lib/monocle-print/atomic.rb', line 428
def uniform?
@@uniform.fetch( hash ) do | h |
@@uniform[ h ] = all? { | l | l.width == width }
end
end
|
#valign!(alignment, h, filler = '') ⇒ Object
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/monocle-print/atomic.rb', line 434
def valign!( alignment, h, filler = '' )
filler = Line( filler )
h > height or return( self )
case alignment
when :top
fill( filler, height, h - height )
when :bottom
( h - height ).times { unshift( filler ) }
when :center
even, odd = ( h - height ).divmod( 2 )
even.times { push( filler ); unshift( filler ) }
odd == 1 and push( filler )
end
return( self )
end
|
#width ⇒ Object
450
451
452
|
# File 'lib/monocle-print/atomic.rb', line 450
def width
@@width[ hash ] ||= ( map { | line | line.width }.max || 0 )
end
|
#wrap(width) ⇒ Object
418
419
420
421
422
|
# File 'lib/monocle-print/atomic.rb', line 418
def wrap( width )
reflow.inject( self.class.new ) do | wrapped, line |
wrapped.concat( line.wrap( width ) )
end
end
|