class RawLine::HistoryBuffer

The HistoryBuffer class is used to hold the editor and line histories, as well as word completion matches.

Attributes

cycle[RW]
duplicates[RW]
exclude[RW]
position[R]
size[R]

Public Class Methods

new(size) { |self| ... } click to toggle source

Create an instance of RawLine::HistoryBuffer. This method takes an optional block used to override the following instance attributes:

  • @duplicates - whether or not duplicate items will be stored in the buffer.

  • @exclude - a Proc object defining exclusion rules to prevent items from being added to the buffer.

  • @cycle - Whether or not the buffer is cyclic.

# File lib/rawline/history_buffer.rb, line 32
def initialize(size)
        @duplicates = true
        @exclude = lambda{|a|}
        @cycle = false
        yield self if block_given?
        @size = size
        @position = nil
end

Public Instance Methods

<<(item)
Alias for: push
back() click to toggle source

Decrement @position.

# File lib/rawline/history_buffer.rb, line 86
def back
        return nil unless length > 0
        case @position
        when nil then
                @position = length-1
        when 0 then
             @position = length-1 if @cycle
        else 
                @position -= 1
        end
end
empty() click to toggle source

Clear the content of the buffer and reset @position to nil.

# File lib/rawline/history_buffer.rb, line 55
def empty
        @position = nil 
        clear
end
end?() click to toggle source

Return true if @position is at the end of the buffer.

# File lib/rawline/history_buffer.rb, line 72
def end?
        @position == length-1
end
forward() click to toggle source

Increment @position.

# File lib/rawline/history_buffer.rb, line 101
def forward
        return nil unless length > 0
        case @position
        when nil then
                @position = length-1
        when length-1 then
             @position = 0 if @cycle
        else 
                @position += 1
        end
end
get() click to toggle source

Retrieve the element at @position.

# File lib/rawline/history_buffer.rb, line 63
def get
        return nil unless length > 0
        @position = length-1 unless @position
        at @position
end
push(item) click to toggle source

Add a new item to the buffer.

Calls superclass method
# File lib/rawline/history_buffer.rb, line 116
def push(item)
        delete(item) unless @duplicates
        unless @exclude.call(item)
                # Remove the oldest element if size is exceeded
                if @size <= length
                        reverse!.pop 
                        reverse!
                end
                # Add the new item and reset the position
                super(item)
                @position = nil 
        end
end
Also aliased as: <<
resize(new_size) click to toggle source

Resize the buffer, resetting @position to nil.

# File lib/rawline/history_buffer.rb, line 44
def resize(new_size)
        if new_size < @size
                @size-new_size.times { pop }
        end
        @size = new_size
        @position = nil 
end
start?() click to toggle source

Return true if @position is at the start of the buffer.

# File lib/rawline/history_buffer.rb, line 79
def start?
        @position == 0
end