class Ribosome::Block

Class Block represents a rectangular area of text.

Attributes

text[RW]
width[RW]

Public Class Methods

new(s) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 34
def initialize(s)
    @text = []
    @width = 0
    return if s == nil

    # Split the string into individual lines.
                start = 0
                loop do
                        i = s.index("\n", start) || s.size
                        @text << (i == start ? "" : s[start..i - 1])
        @width = [@width, @text.last.size].max
                        start = i + 1
                        break if start > s.size
                end
end

Public Instance Methods

add_bottom(block) click to toggle source

Weld the supplied block to the bottom side of this block.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 71
def add_bottom(block)
    @text += block.text
    @width = [@width, block.width].max
end
add_right(block) click to toggle source

Weld the supplied block to the right side of this block.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 51
def add_right(block)

    # Merge the blocks while taking care to add whitespace
    # where they do not align properly.
    i = 0
    for l in block.text
        if(@text[i])
            @text[i] += (" " * (@width - @text[i].size)) + l
        else
            @text << (" " * @width) + l
        end
        i += 1
    end

    # Adjust the overall width of the block.
    @width += block.width
    
end
last_offset() click to toggle source

Returns offset of the last line in the block.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 142
def last_offset()
    return 0 if @text.empty?
    return @text.last.size - @text.last.lstrip.size
end
trim() click to toggle source

Trim the whitespace from the block.

# File lib/swift_generator/code_generation/swift_file_template.rb, line 77
def trim()

    # Find the boundaries of the text.
    top = -1
    bottom = -1
    left = -1
    right = -1

    i = 0
    for l in @text
        if(!l.lstrip().empty?)
            top = i if top == -1
            bottom = i;
            if (left == -1)
                left = l.size() - l.lstrip().size()
            else
                left = [left, l.size() - l.lstrip().size()].min
            end
            if (right == -1)
                right = l.rstrip().size()
            else
                right = [right, l.rstrip().size()].max
            end
        end
        i += 1
    end

    # The case of block with no text whatsoever.
    if bottom == -1
        @text = []
        @width = 0
        return
    end

    # Strip off the top and bottom whitespace.
    @text = @text[top..bottom]

    # Strip off the whitespace on the left and on the right.
    for i in 0..@text.size() - 1
        @text[i] = @text[i].rstrip()[left..right]
        @text[i] = "" if @text[i] == nil
    end

    # Adjust the overall width of the block.
    @width = (@text.max {|x,y| x.size <=> y.size} || "").size

end
write(out, tabsize) click to toggle source
# File lib/swift_generator/code_generation/swift_file_template.rb, line 125
def write(out, tabsize)
    for l in @text

        # If required, replace the initial whitespace by tabs.
        if(tabsize > 0)
            ws = l.size - l.lstrip.size
            l = "\t" * (ws / tabsize) +
                " " * (ws % tabsize) + l.lstrip
        end

        # Write an individual line to the output file.
        out.write(l)
        out.write("\n")
    end
end