Class: Pry::History
Overview
The History class is responsible for maintaining the user's input history, both internally and within Readline.
Instance Attribute Summary collapse
-
#history_line_count ⇒ Integer
readonly
Total number of lines, including original lines.
-
#loader ⇒ Object
Returns the value of attribute loader.
-
#original_lines ⇒ Fixnum
readonly
Number of lines in history when Pry first loaded.
-
#saver ⇒ Object
Returns the value of attribute saver.
Class Method Summary collapse
Instance Method Summary collapse
-
#clear ⇒ Object
Clear this session's history.
-
#filter(history) ⇒ Array<String>
Filter the history with the histignore options.
-
#history_file ⇒ Object
private
The history file, opened for appending.
- #history_file_path ⇒ Object private
-
#initialize(options = {}) ⇒ History
constructor
A new instance of History.
- #invalid_readline_line?(line) ⇒ Boolean private
-
#load ⇒ Integer
Load the input history using
History.loader. -
#push(line) ⇒ String
(also: #<<)
Add a line to the input history, ignoring blank and duplicate lines.
-
#read_from_file ⇒ Object
private
The default loader.
-
#save_to_file(line) ⇒ Object
private
The default saver.
-
#session_line_count ⇒ Fixnum
The number of lines in history from just this session.
-
#should_ignore?(line) ⇒ Boolean
private
Check if the line match any option in the histignore [Pry.config.history_ignorelist].
-
#to_a ⇒ Array<String>
Return an Array containing all stored history.
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( = {}) @history = [:history] || [] @history_line_count = @history.count @file_path = [:file_path] @original_lines = 0 @loader = method(:read_from_file) @saver = method(:save_to_file) end |
Instance Attribute Details
#history_line_count ⇒ Integer (readonly)
Returns 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 |
#loader ⇒ Object
Returns the value of attribute loader.
21 22 23 |
# File 'lib/pry/history.rb', line 21 def loader @loader end |
#original_lines ⇒ Fixnum (readonly)
Returns 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 |
#saver ⇒ Object
Returns the value of attribute saver.
21 22 23 |
# File 'lib/pry/history.rb', line 21 def saver @saver end |
Class Method Details
.default_file ⇒ Object
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.('~/.pry_history')) '~/.pry_history' else '~/.local/share/pry/pry_history' end File.(history_file) end |
Instance Method Details
#clear ⇒ Object
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
95 96 97 |
# File 'lib/pry/history.rb', line 95 def filter(history) history.select { |l| l unless should_ignore?(l) } end |
#history_file ⇒ Object (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_path ⇒ Object (private)
143 144 145 |
# File 'lib/pry/history.rb', line 143 def history_file_path File.(@file_path || Pry.config.history_file) end |
#invalid_readline_line?(line) ⇒ Boolean (private)
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 |
#load ⇒ Integer
Load the input history using History.loader.
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.
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_file ⇒ Object (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_count ⇒ Fixnum
Returns 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]
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_a ⇒ Array<String>
Return an Array containing all stored history.
88 89 90 |
# File 'lib/pry/history.rb', line 88 def to_a @history.to_a end |