Class: Pry::History

Inherits:
Object show all
Defined in:
lib/pry/history.rb

Overview

The History class is responsible for maintaining the user’s input history, both internally and within Readline.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ History

Returns a new instance of History.



29
30
31
32
33
34
35
36
# File 'lib/pry/history.rb', line 29

def initialize(options = {})
  @history = options[:history] || []
  @history_line_count = @history.count
  @file_path = options[:file_path]
  @original_lines = 0
  @loader = method(:read_from_file)
  @saver = method(:save_to_file)
end

Instance Attribute Details

#history_line_countInteger (readonly)

Returns total number of lines, including original lines.

Returns:

  • (Integer)

    total number of lines, including original lines



27
28
29
# File 'lib/pry/history.rb', line 27

def history_line_count
  @history_line_count
end

#loaderObject

Returns the value of attribute loader.



21
22
23
# File 'lib/pry/history.rb', line 21

def loader
  @loader
end

#original_linesFixnum (readonly)

Returns Number of lines in history when Pry first loaded.

Returns:

  • (Fixnum)

    Number of lines in history when Pry first loaded.



24
25
26
# File 'lib/pry/history.rb', line 24

def original_lines
  @original_lines
end

#saverObject

Returns the value of attribute saver.



21
22
23
# File 'lib/pry/history.rb', line 21

def saver
  @saver
end

Class Method Details

.default_fileObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pry/history.rb', line 7

def self.default_file
  history_file =
    if (xdg_home = Pry::Env['XDG_DATA_HOME'])
      # See XDG Base Directory Specification at
      # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
      xdg_home + '/pry/pry_history'
    elsif File.exist?(File.expand_path('~/.pry_history'))
      '~/.pry_history'
    else
      '~/.local/share/pry/pry_history'
    end
  File.expand_path(history_file)
end

Instance Method Details

#clearObject

Clear this session’s history. This won’t affect the contents of the history file.



74
75
76
77
78
# File 'lib/pry/history.rb', line 74

def clear
  @history.clear
  @history_line_count = 0
  @original_lines = 0
end

#filter(history) ⇒ Array<String>

Filter the history with the histignore options

Returns:

  • (Array<String>)

    An array containing all the lines that are not included in the histignore.



95
96
97
# File 'lib/pry/history.rb', line 95

def filter(history)
  history.select { |l| l unless should_ignore?(l) }
end

#loadInteger

Load the input history using ‘History.loader`.

Returns:

  • (Integer)

    The number of lines loaded



40
41
42
43
44
45
46
47
48
# File 'lib/pry/history.rb', line 40

def load
  @loader.call do |line|
    next if invalid_readline_line?(line)

    @history << line.chomp
    @original_lines += 1
    @history_line_count += 1
  end
end

#push(line) ⇒ String Also known as: <<

Add a line to the input history, ignoring blank and duplicate lines.

Parameters:

  • line (String)

Returns:

  • (String)

    The same line that was passed in



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pry/history.rb', line 53

def push(line)
  return line if line.empty? || invalid_readline_line?(line)

  begin
    last_line = @history[-1]
  rescue IndexError
    last_line = nil
  end

  return line if line == last_line

  @history << line
  @history_line_count += 1
  @saver.call(line) if !should_ignore?(line) && Pry.config.history_save

  line
end

#session_line_countFixnum

Returns The number of lines in history from just this session.

Returns:

  • (Fixnum)

    The number of lines in history from just this session.



81
82
83
# File 'lib/pry/history.rb', line 81

def session_line_count
  @history_line_count - @original_lines
end

#to_aArray<String>

Return an Array containing all stored history.

Returns:

  • (Array<String>)

    An Array containing all lines of history loaded or entered by the user in the current session.



88
89
90
# File 'lib/pry/history.rb', line 88

def to_a
  @history.to_a
end