class RawLine::HistoryBuffer
The HistoryBuffer
class is used to hold the editor and line histories, as well as word completion matches.
Attributes
Public Class Methods
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
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
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
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
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
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
Add a new item to the buffer.
# 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
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
Return true if @position
is at the start of the buffer.
# File lib/rawline/history_buffer.rb, line 79 def start? @position == 0 end