class MonoclePrint::SingleLine

Public Class Methods

clear_cache() click to toggle source
# File lib/monocle-print/atomic.rb, line 28
def self.clear_cache
  @@width.clear
  @@invisible_size.clear
  return( self )
end

Public Instance Methods

align( alignment, width, fill = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 81
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 85
def align!( alignment, width, fill = ' ' )
  case alignment.to_sym
  when :left then left!( width, fill )
  when :center then center!( width, fill )
  when :right then right!( width, fill )
  end
end
blank?() click to toggle source
# File lib/monocle-print/atomic.rb, line 93
def blank?
  empty? or self =~ /^(\s|#{COLOR_ESCAPE})*$/
end
bleach() click to toggle source
# File lib/monocle-print/atomic.rb, line 97
def bleach
  gsub( COLOR_ESCAPE, '' )
end
bleach!() click to toggle source
# File lib/monocle-print/atomic.rb, line 101
def bleach!
  gsub!( COLOR_ESCAPE, '' )
end
center!( w, fill = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 105
def center!( w, fill = ' ' )
  w > width or return( self )
  if fill.length == 1
    replace( center( w + invisible_size, fill ) )
  else fill = self.class.new( fill )
    even, odd = ( width - w ).divmod( 2 )
    insert( 0, fill.tile( even ) )
    self << fill.tile( even + odd )
  end
  self
end
char_byte( n ) click to toggle source
# File lib/monocle-print/atomic.rb, line 39
def char_byte( n )
  n.zero? and return( 0 )
  seen = byte = 0
  while c = self[ byte ] and seen < n
    step =
      case c
      when ONE_BYTE then seen += 1; 1
      when ?\e
        self[ byte, size ] =~ /^#{COLOR_ESCAPE}/ ? $~.end(0) : 1
      when TWO_BYTES then seen += 1; 2
      when THREE_BYTES then seen += 1; 3
      when FOUR_BYTES then seen += 1; 4
      else 1
      end
    byte += step
  end
  return( byte )
end
divide_at( len ) click to toggle source
# File lib/monocle-print/atomic.rb, line 117
def divide_at( len )
  pos = char_byte( len )
  return( [ self[ 0, pos ], self[ pos, size ] ] )
end
each_escape() { |esc| ... } click to toggle source
# File lib/monocle-print/atomic.rb, line 122
def each_escape
  block_given? or return( enum_for( :each_escape ) )
  scan( COLOR_ESCAPE ) do |esc|
    yield( esc )
  end
end
escapes() click to toggle source
# File lib/monocle-print/atomic.rb, line 129
def escapes
  each_escape.inject( self.class.new ) do | escs, esc |
    escs << esc
  end
end
height() click to toggle source
# File lib/monocle-print/atomic.rb, line 211
def height
  1
end
indent( n ) click to toggle source
# File lib/monocle-print/atomic.rb, line 135
def indent( n )
  dup.indent!( n )
end
indent!( num_spaces ) click to toggle source
# File lib/monocle-print/atomic.rb, line 139
def indent!( num_spaces )
  if num_spaces < 0
    remaining_indent = Utils.at_least( level_of_indent + num_spaces, 0 )
    lstrip!
    indent!( remaining_indent )
  else
    insert( 0, ' ' * num_spaces )
  end
  return( self )
end
invisible_size() click to toggle source
# File lib/monocle-print/atomic.rb, line 150
def invisible_size
  @@invisible_size[ hash ] ||= size - width
end
left!( w, fill = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 154
def left!( w, fill = ' ' )
  w > width or return( self )
  if fill.length == 1
    replace( ljust( w + invisible_size, fill ) )
  else fill = self.class.new( fill )
    insert( 0, fill.tile( width - w ) )
  end
  self
end
level_of_indent() click to toggle source
# File lib/monocle-print/atomic.rb, line 164
def level_of_indent
  self =~ /^(\s+)/ ? $1.length : 0
end
pad!( left, right = left ) click to toggle source
# File lib/monocle-print/atomic.rb, line 168
def pad!( left, right = left )
  right!( width + left )
  left!( width + right )
end
partial(len) click to toggle source
# File lib/monocle-print/atomic.rb, line 173
def partial(len)
  self[ 0, char_byte( len ) ]
end
right!( w, fill = ' ' ) click to toggle source
# File lib/monocle-print/atomic.rb, line 177
def right!( w, fill = ' ' )
  w > width or return( self )
  if fill.length == 1
    replace( rjust( w + invisible_size, fill ) )
  else fill = self.class.new( fill )
    self.insert( 0, fill.tile( w - width ) )
  end
  self
end
tile( size ) click to toggle source
# File lib/monocle-print/atomic.rb, line 187
def tile( size )
  width == 0 and return( )
  full, partial = size.divmod( width )
  self * full << partial( partial )
end
truncate( w, tail = nil ) click to toggle source
# File lib/monocle-print/atomic.rb, line 193
def truncate( w, tail = nil )
  dup.truncate!( w, tail )
end
truncate!( w, tail = nil ) click to toggle source
# File lib/monocle-print/atomic.rb, line 197
def truncate!( w, tail = nil )
  width > w or return( self )
  if tail then tail = Line( tail )
    return( partial( w - tail.width ) << tail )
  else
    return( partial( w ) )
  end
  return( self )
end
width() click to toggle source
# File lib/monocle-print/atomic.rb, line 58
def width
  @@width[ hash ] ||= begin
    (temp = bleach).gsub!( MULTIBYTE_CHARACTER, ' ' )
    temp.size
  end
end
words() click to toggle source
# File lib/monocle-print/atomic.rb, line 207
def words
  strip.split(/\s+/)
end
wrap( w ) click to toggle source
# File lib/monocle-print/atomic.rb, line 215
def wrap( w )
  if width > w
    words = split( /\s+/ ).inject( [] ) do | words, word |
      while word.width > w
        frag, word = word.divide_at( w )
        words << frag
      end
      words << word
    end

    line = words.shift || self.class.new
    text = Text.new
    w -= 1
    while word = words.shift
      if line.width + word.width > w
        text << line
        line = word
      else
        line << ' ' << word
      end
    end
    text << line
    return( text )
  else
    return( Text( self.dup ) )
  end
end