123456789_123456789_123456789_123456789_123456789_

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

Instance Method Summary

Instance Attribute Details

#support_history_saving? ⇒ Boolean (readonly)

[ GitHub ]

  
# File 'lib/irb/history.rb', line 35

def support_history_saving?
  true
end

Instance Method Details

#ensure_history_file_permissions(history_file) (private)

Permissions of already existing history_file are changed to owner-only-readable if necessary [BUG #7694].

[ GitHub ]

  
# File 'lib/irb/history.rb', line 106

def ensure_history_file_permissions(history_file)
  history_file = Pathname.new(history_file)

  return unless history_file.exist?

  if history_file.stat.mode & 0o66 != 0
    history_file.chmod 0o600
  end
end

#load_history

[ GitHub ]

  
# File 'lib/irb/history.rb', line 43

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 39

def reset_history_counter
  @loaded_history_lines = self.class::HISTORY.size
end

#save_history

[ GitHub ]

  
# File 'lib/irb/history.rb', line 64

def save_history
  return unless History.save_history?
  return unless (history_file = History.history_file)

  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

  begin
    ensure_history_file_permissions(history_file)

    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
  rescue Errno::ENOENT
    warn <<~WARN
      Can't write history to #{History.history_file.inspect} because the folder does not exist.
      Please verify the value of `IRB.conf[:HISTORY_FILE]`. Ensure the folder exists.
    WARN
  rescue Errno::EACCES, Errno::EPERM
    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
  end
end