class WindowTerminal::WrappedText

A subclass of Text which allows for wrapped text within a Window.

Public Class Methods

new(orientation,text,y,mode=:destroy) click to toggle source

Initializes a WrappedText object.

Calls superclass method WindowTerminal::Text::new
# File lib/accu-window.rb, line 256
def initialize(orientation,text,y,mode=:destroy)
        @mode = mode
        super(orientation,text,y)
end

Public Instance Methods

render(lines,cols,rows,padding=2) click to toggle source

Renders the WrappedText object based on passed lines and returns the modified lines.

# File lib/accu-window.rb, line 264
def render(lines,cols,rows,padding=2)
        #--
        lines = lines.dup
        words = wrap(@text,rows-padding*2)
        if words.length > (cols - @y) then
                words.slice!((cols - @y + 1)..(words.length-1))
        end
        range = (@y - 1)..(cols-1)
        sum = 0
        ending = cols - padding - 1
        lines.each_index { |index|
                if (range === index and words[sum]) and index < ending then
                        #puts "|" + words[sum] + "|"
                        lines[index] = WindowTerminal::Text.new(orientation,words[sum],0).render_line(lines[index],rows,padding)
                        sum += 1
                end
        }
        return lines
        #++
end

Private Instance Methods

wrap(s,width) click to toggle source

Wraps a string based on width.

<i>Credit: RubyCookbook</i>
# File lib/accu-window.rb, line 289
def wrap(s,width)
        #--
        if @mode == :destroy then
                s = s.dup
                s = s.split(/\s+/) if s.is_a? String
                lines = []
                line = ""
                #puts s.length
                s.each do |word|
                        if line.size + word.size >= width then
                                lines << line
                                line = word
                        elsif line.empty? then
                                line = word
                        else
                                line << " " << word
                        end
                end
                
                lines << line if line
                #puts lines.length
                return lines
        else
                string = s.gsub(/(.{1,#{width}})( |\Z)/, "\\1\n").split(/\n/)
                #puts string
                string
        end
end