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
- 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.
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/tty/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/tty/reader/history.rb', line 38 def cycle @cycle end |
#duplicates ⇒ Boolean
Decides wether or not duplicate lines are stored.
45 46 47 |
# File 'lib/tty/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/tty/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/tty/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/tty/reader/history.rb', line 24 def max_size @max_size end |
Instance Method Details
#[](index) ⇒ Object
Return line at the specified index
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/tty/reader/history.rb', line 143 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
166 167 168 169 |
# File 'lib/tty/reader/history.rb', line 166 def clear @history.clear @index = 0 end |
#each(&block) ⇒ Object
Iterates over history lines
81 82 83 84 85 86 87 |
# File 'lib/tty/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
157 158 159 160 161 |
# File 'lib/tty/reader/history.rb', line 157 def get return if size.zero? self[@index] end |
#next ⇒ Object
Move the pointer to the next line in the history
109 110 111 112 113 114 115 116 117 |
# File 'lib/tty/reader/history.rb', line 109 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.
119 120 121 |
# File 'lib/tty/reader/history.rb', line 119 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
124 125 126 127 128 129 130 131 132 |
# File 'lib/tty/reader/history.rb', line 124 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.
134 135 136 |
# File 'lib/tty/reader/history.rb', line 134 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/tty/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 |