Class: TTY::Reader::History Private
- Inherits:
-
Object
- Object
- TTY::Reader::History
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/tty/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
Instance Attribute Summary collapse
- #cycle ⇒ Object private
- #duplicates ⇒ Object private
- #exclude ⇒ Object private
- #index ⇒ Object readonly private
-
#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 ⇒ Object
Iterates over history lines.
-
#get ⇒ Object
Get current line.
-
#initialize(max_size = DEFAULT_SIZE, **options) {|_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.
Constructor Details
#initialize(max_size = DEFAULT_SIZE, **options) {|_self| ... } ⇒ History
Create a History buffer
param [Integer] max_size
the maximum size for history buffer
param [Hash] options
45 46 47 48 49 50 51 52 53 |
# File 'lib/tty/reader/history.rb', line 45 def initialize(max_size = DEFAULT_SIZE, **) @max_size = max_size @index = nil @history = [] @duplicates = .fetch(:duplicates) { true } @exclude = .fetch(:exclude) { proc {} } @cycle = .fetch(:cycle) { false } yield self if block_given? end |
Instance Attribute Details
#cycle ⇒ 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.
25 26 27 |
# File 'lib/tty/reader/history.rb', line 25 def cycle @cycle end |
#duplicates ⇒ 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.
27 28 29 |
# File 'lib/tty/reader/history.rb', line 27 def duplicates @duplicates end |
#exclude ⇒ 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.
29 30 31 |
# File 'lib/tty/reader/history.rb', line 29 def exclude @exclude end |
#index ⇒ Object (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.
23 24 25 |
# File 'lib/tty/reader/history.rb', line 23 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
21 22 23 |
# File 'lib/tty/reader/history.rb', line 21 def max_size @max_size end |
Instance Method Details
#[](index) ⇒ Object
Return line at the specified index
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/tty/reader/history.rb', line 118 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
141 142 143 144 |
# File 'lib/tty/reader/history.rb', line 141 def clear @history.clear @index = 0 end |
#each ⇒ Object
Iterates over history lines
58 59 60 61 62 63 64 |
# File 'lib/tty/reader/history.rb', line 58 def each if block_given? @history.each { |line| yield line } else @history.to_enum end end |
#get ⇒ Object
Get current line
132 133 134 135 136 |
# File 'lib/tty/reader/history.rb', line 132 def get return if size.zero? self[@index] end |
#next ⇒ Object
Move the pointer to the next line in the history
86 87 88 89 90 91 92 93 |
# File 'lib/tty/reader/history.rb', line 86 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.
95 96 97 |
# File 'lib/tty/reader/history.rb', line 95 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
100 101 102 103 104 105 106 107 |
# File 'lib/tty/reader/history.rb', line 100 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.
109 110 111 |
# File 'lib/tty/reader/history.rb', line 109 def previous? size > 0 && !(@index < 0 && !@cycle) end |
#push(line) ⇒ Object Also known as: <<
Add the last typed line to history buffer
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tty/reader/history.rb', line 71 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 |