Module: IRB::HistorySavingAbility

Included in:
ReadlineInputMethod, RelineInputMethod
Defined in:
lib/irb/history.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#load_historyObject

[View source]

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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_counterObject

[View source]

39
40
41
# File 'lib/irb/history.rb', line 39

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

#save_historyObject

[View source]

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/irb/history.rb', line 64

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

#support_history_saving?Boolean

Returns:

  • (Boolean)
[View source]

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

def support_history_saving?
  true
end