Module: IRB::HistorySavingAbility
Do not use. This module is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/irb/history.rb |
Instance Attribute Summary
- #support_history_saving? ⇒ Boolean readonly
Instance Method Summary
- #load_history
- #reset_history_counter
- #save_history
-
#ensure_history_file_writable(history_file)
private
Returns boolean whether writing to
history_file
will be possible.
Instance Attribute Details
#support_history_saving? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/irb/history.rb', line 31
def support_history_saving? true end
Instance Method Details
#ensure_history_file_writable(history_file) (private)
Returns boolean whether writing to history_file
will be possible. Permissions of already existing history_file
are changed to owner-only-readable if necessary [BUG #7694].
# File 'lib/irb/history.rb', line 96
def ensure_history_file_writable(history_file) history_file = Pathname.new(history_file) return false unless history_file.dirname.writable? return true unless history_file.exist? begin if history_file.stat.mode & 0o66 != 0 history_file.chmod 0o600 end true rescue Errno::EPERM # no permissions false end end
#load_history
[ GitHub ]# File 'lib/irb/history.rb', line 39
def load_history history_file = History.history_file return unless File.exist?(history_file.to_s) history = self.class::HISTORY File.open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f| f.each { |l| l = l.chomp if self.class == RelineInputMethod and history.last&.end_with?("\\") history.last.delete_suffix!("\\") history.last << "\n" << l else history << l end } end @loaded_history_lines = history.size @loaded_history_mtime = File.mtime(history_file) end
#reset_history_counter
[ GitHub ]# File 'lib/irb/history.rb', line 35
def reset_history_counter @loaded_history_lines = self.class::HISTORY.size end
#save_history
[ GitHub ]# File 'lib/irb/history.rb', line 60
def save_history return unless History.save_history? return unless (history_file = History.history_file) unless ensure_history_file_writable(history_file) warn <<~WARN Can't write history to #{History.history_file.inspect} due to insufficient permissions. Please verify the value of `IRB.conf[:HISTORY_FILE]`. Ensure the folder exists and that both the folder and file (if it exists) are writable. WARN return end history = self.class::HISTORY.to_a if File.exist?(history_file) && File.mtime(history_file) != @loaded_history_mtime history = history[@loaded_history_lines..-1] if @loaded_history_lines append_history = true end File.open(history_file, (append_history ? "a" : "w"), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f| hist = history.map { |l| l.scrub.split("\n").join("\\\n") } unless append_history || History.infinite? # Check size before slicing because array.last(huge_number) raises RangeError. hist = hist.last(History.save_history) if hist.size > History.save_history end f.puts(hist) end end