Class: RawLine::HistoryBuffer
- Inherits:
-
Array
- Object
- Array
- RawLine::HistoryBuffer
- Defined in:
- lib/rawline/history_buffer.rb
Overview
The HistoryBuffer class is used to hold the editor and line histories, as well as word completion matches.
Instance Attribute Summary collapse
-
#cycle ⇒ Object
Returns the value of attribute cycle.
-
#duplicates ⇒ Object
Returns the value of attribute duplicates.
-
#exclude ⇒ Object
Returns the value of attribute exclude.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#back(options = {}) ⇒ Object
Decrement
@position. -
#clear_position ⇒ Object
Clears the current position on the history object.
-
#empty ⇒ Object
Clear the content of the buffer and reset
@positionto nil. -
#end? ⇒ Boolean
Return true if
@positionis at the end of the buffer. - #find_match_backward(text) ⇒ Object
- #find_match_forward(text) ⇒ Object
-
#forward(options = {}) ⇒ Object
Increment
@position. -
#get ⇒ Object
Retrieve a copy of the element at
@position. -
#initialize(size) {|_self| ... } ⇒ HistoryBuffer
constructor
Create an instance of RawLine::HistoryBuffer.
-
#push(item) ⇒ Object
(also: #<<)
Add a new item to the buffer.
-
#resize(new_size) ⇒ Object
Resize the buffer, resetting
@positionto nil. -
#start? ⇒ Boolean
Return true if
@positionis at the start of the buffer.
Constructor Details
#initialize(size) {|_self| ... } ⇒ HistoryBuffer
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.
32 33 34 35 36 37 38 39 |
# 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 |
Instance Attribute Details
#cycle ⇒ Object
Returns the value of attribute cycle.
22 23 24 |
# File 'lib/rawline/history_buffer.rb', line 22 def cycle @cycle end |
#duplicates ⇒ Object
Returns the value of attribute duplicates.
22 23 24 |
# File 'lib/rawline/history_buffer.rb', line 22 def duplicates @duplicates end |
#exclude ⇒ Object
Returns the value of attribute exclude.
22 23 24 |
# File 'lib/rawline/history_buffer.rb', line 22 def exclude @exclude end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
21 22 23 |
# File 'lib/rawline/history_buffer.rb', line 21 def position @position end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
21 22 23 |
# File 'lib/rawline/history_buffer.rb', line 21 def size @size end |
Instance Method Details
#back(options = {}) ⇒ Object
Decrement @position. By default the history will become positioned at the previous item.
If @cycle is set to true then the history will cycle to the end when it finds itself at the beginning. If false calling this when at the beginning will result in the position not changing.
If a search strategy is assigned then the method search_backward will be called on the search strategy to determine the position. This method is given any passed in options as well as a :history option. The :history option will be a reference to self.
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rawline/history_buffer.rb', line 124 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_position ⇒ Object
Clears the current position on the history object. Useful when deciding to cancel/reset history navigation.
45 46 47 |
# File 'lib/rawline/history_buffer.rb', line 45 def clear_position @position = nil end |
#empty ⇒ Object
Clear the content of the buffer and reset @position to nil.
83 84 85 86 |
# File 'lib/rawline/history_buffer.rb', line 83 def empty @position = nil clear end |
#end? ⇒ Boolean
Return true if @position is at the end of the buffer.
100 101 102 |
# File 'lib/rawline/history_buffer.rb', line 100 def end? @position == length-1 end |
#find_match_backward(text) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/rawline/history_buffer.rb', line 60 def find_match_backward(text) regex = to_regex(text) offset = @position ? length - position : 0 reverse[offset..-1].detect.with_index do |item, index| if item.match(regex) @position = length - index - (offset + 1) end end end |
#find_match_forward(text) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/rawline/history_buffer.rb', line 70 def find_match_forward(text) regex = to_regex(text) offset = @position ? @position + 1 : 0 self[offset..-1].detect.with_index do |item, index| if item.match(regex) @position = index + offset end end end |
#forward(options = {}) ⇒ Object
Increment @position. By default the history will become positioned at the next item.
If @cycle is set to true then the history will cycle back to the beginning when it finds itself at the end. If false calling this when at the end will result in the position not changing.
If a search strategy is assigned then the method search_forward will be called on the search strategy to determine the position. This method is given any passed in options as well as a :history option. The :history option will be a reference to self. If <tt>
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rawline/history_buffer.rb', line 150 def forward(={}) return nil unless length > 0 case @position when nil then @position = 0 when length-1 then @position = 0 if @cycle else @position += 1 end end |
#get ⇒ Object
Retrieve a copy of the element at @position.
91 92 93 94 95 |
# File 'lib/rawline/history_buffer.rb', line 91 def get return nil unless length > 0 @position = length-1 unless @position at(@position).dup end |
#push(item) ⇒ Object Also known as: <<
Add a new item to the buffer.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rawline/history_buffer.rb', line 166 def push(item) if !@duplicates && include?(item) # skip adding this line return end 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(new_size) ⇒ Object
Resize the buffer, resetting @position to nil.
52 53 54 55 56 57 58 |
# File 'lib/rawline/history_buffer.rb', line 52 def resize(new_size) if new_size < @size @size-new_size.times { pop } end @size = new_size @position = nil end |
#start? ⇒ Boolean
Return true if @position is at the start of the buffer.
107 108 109 |
# File 'lib/rawline/history_buffer.rb', line 107 def start? @position == 0 end |