Class: TTY::Reader::History Private

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

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/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

#cycleBoolean

Decides whether or not to allow cycling through stored lines.

Returns:

  • (Boolean)


38
39
40
# File 'lib/tty/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/tty/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/tty/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/tty/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/tty/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



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

#clearObject

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

#getObject

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

#nextObject

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.

Returns:

  • (Boolean)


119
120
121
# File 'lib/tty/reader/history.rb', line 119

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



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.

Returns:

  • (Boolean)


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

Parameters:

  • line (String)


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