class MonoclePrint::Text

Public Class Methods

clear_cache() click to toggle source
# File lib/monocle-print/atomic.rb, line 245
def self.clear_cache
  @@width.clear
  @@uniform.clear
  return( self )
end
new( lines = nil, default = nil ) { |i| ... } click to toggle source
Calls superclass method
# 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

Public Instance Methods

`(str) click to toggle source
# File lib/monocle-print/atomic.rb, line 456
def `(str) #` # comment here cos my editor's colorizing freaks out
  SingleLine.new( str )
end
align( alignment, width, fill = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 280
def align( alignment, width, fill = ' ' )
  dup.align!( alignment, width, fill )
end
align!( alignment, width, fill = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 284
def align!( alignment, width, fill = ' ' )
  # if the width argument is less than the full block width of the text,
  # make it the full block width to ensure uniform width
  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() click to toggle source
# File lib/monocle-print/atomic.rb, line 300
def bleach
  dup.bleach!
end
bleach!() click to toggle source
# File lib/monocle-print/atomic.rb, line 304
def bleach!
  each { | l | l.bleach! }
end
fix() click to toggle source
# File lib/monocle-print/atomic.rb, line 308
def fix
  dup.fix!
end
fix!() click to toggle source
# File lib/monocle-print/atomic.rb, line 312
def fix!
  align!( :left, width )
end
fixed_indent( level = 0 ) click to toggle source
# File lib/monocle-print/atomic.rb, line 322
def fixed_indent( level = 0 )
  dup.fixed_indent!( level )
end
fixed_indent!( level = 0 ) click to toggle source
# 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 ) click to toggle source
# File lib/monocle-print/atomic.rb, line 342
def frame( graphic_style )
  dup.frame!( graphic_style )
end
frame!( graphic_style ) click to toggle source
# 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 ) click to toggle source
# File lib/monocle-print/atomic.rb, line 338
def indent( spaces )
  dup.indent!( spaces )
end
indent!( spaces ) click to toggle source
# 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 ) click to toggle source
# File lib/monocle-print/atomic.rb, line 274
def initialize_copy( orig )
  for line in orig
    self << line.dup
  end
end
inspect() click to toggle source
# 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 = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 366
def juxtapose( text, joint = ' ' )
  dup.juxtapose!( text, joint )
end
Also aliased as: |
juxtapose!( text, joint = ' ' ) click to toggle source
# 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() click to toggle source
# 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 ) click to toggle source
# File lib/monocle-print/atomic.rb, line 380
def pad( padding )
  dup.pad!( padding )
end
pad!( padding ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# File lib/monocle-print/atomic.rb, line 414
def reflow!( paragraph_style = true )
  replace( reflow( paragraph_style ) )
end
to_s() click to toggle source
# File lib/monocle-print/atomic.rb, line 424
def to_s
  join( $/ )
end
uniform?() click to toggle source
# 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 = '' ) click to toggle source
# 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() click to toggle source
# File lib/monocle-print/atomic.rb, line 450
def width
  @@width[ hash ] ||= ( map { | line | line.width }.max || 0 )
end
wrap( width ) click to toggle source
# 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
|( text, joint = ' ' )
Alias for: juxtapose