Class: TTY2::Reader::History Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(max_size = DEFAULT_SIZE, duplicates: true, cycle: false, exclude: DEFAULT_EXCLUDE) {|_self| ... } ⇒ History

Create a History buffer

Parameters:

  • max_size (Integer) (defaults to: DEFAULT_SIZE)

    the maximum size for history buffer

  • cycle (Boolean) (defaults to: false)

    whether or not the history should cycle, false by default

  • duplicates (Boolean) (defaults to: true)

    whether or not to store duplicates, true by default

  • exclude (Boolean) (defaults to: DEFAULT_EXCLUDE)

    a Proc to exclude items from storing in history

Yields:

  • (_self)

Yield Parameters:



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

#cycleBoolean

Decides whether or not to allow cycling through stored lines.

Returns:

  • (Boolean)


38
39
40
# File 'lib/tty2/reader/history.rb', line 38

def cycle
  @cycle
end

#duplicatesBoolean

Decides wether or not duplicate lines are stored.

Returns:

  • (Boolean)


45
46
47
# File 'lib/tty2/reader/history.rb', line 45

def duplicates
  @duplicates
end

#excludeProc

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.

Returns:

  • (Proc)


52
53
54
# File 'lib/tty2/reader/history.rb', line 52

def exclude
  @exclude
end

#indexInteger (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

Returns:

  • (Integer)


31
32
33
# File 'lib/tty2/reader/history.rb', line 31

def index
  @index
end

#max_sizeObject

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

Raises:

  • (IndexError)

    index out of range



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

#clearObject

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

#getObject

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

#nextObject

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.

Returns:

  • (Boolean)


131
132
133
# File 'lib/tty2/reader/history.rb', line 131

def next?
  size > 0 && !(@index == size - 1 && !@cycle)
end

#previousObject

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.

Returns:

  • (Boolean)


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

Parameters:

  • line (String)


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

Parameters:

  • line (String)

    the new line to replace with



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