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

#history_fileObject (private)

The history file, opened for appending.


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pry/history.rb', line 127

def history_file
  if defined?(@history_file)
    @history_file
  else
    unless File.exist?(history_file_path)
      FileUtils.mkdir_p(File.dirname(history_file_path))
    end
    @history_file = File.open(history_file_path, 'a', 0o600).tap do |file|
      file.sync = true
    end
  end
rescue SystemCallError => error
  warn "Unable to write history file: #{error.message}"
  @history_file = false
end

#history_file_pathObject (private)


143
144
145
# File 'lib/pry/history.rb', line 143

def history_file_path
  File.expand_path(@file_path || Pry.config.history_file)
end

#invalid_readline_line?(line) ⇒ Boolean (private)

Returns:

  • (Boolean)

147
148
149
150
151
# File 'lib/pry/history.rb', line 147

def invalid_readline_line?(line)
  # `Readline::HISTORY << line` raises an `ArgumentError` if `line`
  # includes a null byte
  line.include?("\0")
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

#read_from_fileObject (private)

The default loader. Yields lines from Pry.config.history_file.


113
114
115
116
117
118
119
# File 'lib/pry/history.rb', line 113

def read_from_file
  path = history_file_path

  File.foreach(path) { |line| yield(line) } if File.exist?(path)
rescue SystemCallError => error
  warn "Unable to read history file: #{error.message}"
end

#save_to_file(line) ⇒ Object (private)

The default saver. Appends the given line to Pry.config.history_file.


122
123
124
# File 'lib/pry/history.rb', line 122

def save_to_file(line)
  history_file.puts line if history_file
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

#should_ignore?(line) ⇒ Boolean (private)

Check if the line match any option in the histignore [Pry.config.history_ignorelist]

Returns:

  • (Boolean)

    a boolean that notifies if the line was found in the histignore array.


105
106
107
108
109
110
# File 'lib/pry/history.rb', line 105

def should_ignore?(line)
  hist_ignore = Pry.config.history_ignorelist
  return false if hist_ignore.nil? || hist_ignore.empty?

  hist_ignore.any? { |p| line.to_s.match(p) }
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