Class: TTY2::Reader::History Private
- Inherits:
-
Object
- Object
- TTY2::Reader::History
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/tty2/reader/history.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class responsible for storing a history of all lines entered by user when interacting with shell prompt.
Constant Summary collapse
- DEFAULT_SIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default maximum size
32 << 4
- DEFAULT_EXCLUDE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default exclude
->(line) { line.chomp == "" }
Instance Attribute Summary collapse
-
#cycle ⇒ Boolean
Decides whether or not to allow cycling through stored lines.
-
#duplicates ⇒ Boolean
Decides wether or not duplicate lines are stored.
-
#exclude ⇒ Proc
private
Dictates which lines are stored.
-
#index ⇒ Integer
readonly
private
The current index.
-
#max_size ⇒ Object
private
Set and retrieve the maximum size of the buffer.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Return line at the specified index.
-
#clear ⇒ Object
Empty all history lines.
-
#each(&block) ⇒ Object
Iterates over history lines.
-
#get ⇒ Object
Get current line.
-
#initialize(max_size = DEFAULT_SIZE, duplicates: true, cycle: false, exclude: DEFAULT_EXCLUDE) {|_self| ... } ⇒ History
constructor
Create a History buffer.
-
#next ⇒ Object
Move the pointer to the next line in the history.
- #next? ⇒ Boolean private
-
#previous ⇒ Object
private
Move the pointer to the previous line in the history.
- #previous? ⇒ Boolean private
-
#push(line) ⇒ Object
(also: #<<)
Add the last typed line to history buffer.
-
#replace(line) ⇒ Object
Replace the current line with a new one.
Constructor Details
#initialize(max_size = DEFAULT_SIZE, duplicates: true, cycle: false, exclude: DEFAULT_EXCLUDE) {|_self| ... } ⇒ History
Create a History buffer
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tty2/reader/history.rb', line 66 def initialize(max_size = DEFAULT_SIZE, duplicates: true, cycle: false, exclude: DEFAULT_EXCLUDE) @max_size = max_size @index = nil @history = [] @duplicates = duplicates @exclude = exclude @cycle = cycle yield self if block_given? end |
Instance Attribute Details
#cycle ⇒ Boolean
Decides whether or not to allow cycling through stored lines.
38 39 40 |
# File 'lib/tty2/reader/history.rb', line 38 def cycle @cycle end |
#duplicates ⇒ Boolean
Decides wether or not duplicate lines are stored.
45 46 47 |
# File 'lib/tty2/reader/history.rb', line 45 def duplicates @duplicates end |
#exclude ⇒ Proc
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dictates which lines are stored.
52 53 54 |
# File 'lib/tty2/reader/history.rb', line 52 def exclude @exclude end |
#index ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The current index
31 32 33 |
# File 'lib/tty2/reader/history.rb', line 31 def index @index end |
#max_size ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set and retrieve the maximum size of the buffer
24 25 26 |
# File 'lib/tty2/reader/history.rb', line 24 def max_size @max_size end |
Instance Method Details
#[](index) ⇒ Object
Return line at the specified index
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/tty2/reader/history.rb', line 155 def [](index) if index < 0 index += @history.size if index < 0 end line = @history[index] if line.nil? raise IndexError, "invalid index" end line.dup end |
#clear ⇒ Object
Empty all history lines
178 179 180 181 |
# File 'lib/tty2/reader/history.rb', line 178 def clear @history.clear @index = 0 end |
#each(&block) ⇒ Object
Iterates over history lines
81 82 83 84 85 86 87 |
# File 'lib/tty2/reader/history.rb', line 81 def each(&block) if block_given? @history.each(&block) else @history.to_enum end end |
#get ⇒ Object
Get current line
169 170 171 172 173 |
# File 'lib/tty2/reader/history.rb', line 169 def get return if size.zero? self[@index] end |
#next ⇒ Object
Move the pointer to the next line in the history
121 122 123 124 125 126 127 128 129 |
# File 'lib/tty2/reader/history.rb', line 121 def next return if size.zero? if @index == size - 1 @index = 0 if @cycle else @index += 1 end end |
#next? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
131 132 133 |
# File 'lib/tty2/reader/history.rb', line 131 def next? size > 0 && !(@index == size - 1 && !@cycle) end |
#previous ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Move the pointer to the previous line in the history
136 137 138 139 140 141 142 143 144 |
# File 'lib/tty2/reader/history.rb', line 136 def previous return if size.zero? if @index.zero? @index = size - 1 if @cycle else @index -= 1 end end |
#previous? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
146 147 148 |
# File 'lib/tty2/reader/history.rb', line 146 def previous? size > 0 && !(@index < 0 && !@cycle) end |
#push(line) ⇒ Object Also known as: <<
Add the last typed line to history buffer
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/tty2/reader/history.rb', line 94 def push(line) @history.delete(line) unless @duplicates return if line.to_s.empty? || @exclude[line] @history.shift if size >= max_size @history << line @index = @history.size - 1 self end |
#replace(line) ⇒ Object
Replace the current line with a new one
112 113 114 115 116 |
# File 'lib/tty2/reader/history.rb', line 112 def replace(line) return if @index.to_i >= size @history[index] = line.dup end |