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)

Instance Method Summary (collapse)

Constructor Details

- (History) initialize

A new instance of History



7
8
9
10
11
# File 'lib/pry/history.rb', line 7

def initialize
  @history = []
  @saved_lines = 0
  restore_default_behavior
end

Instance Attribute Details

- (Object) clearer

Returns the value of attribute clearer



5
6
7
# File 'lib/pry/history.rb', line 5

def clearer
  @clearer
end

- (Object) loader

Returns the value of attribute loader



5
6
7
# File 'lib/pry/history.rb', line 5

def loader
  @loader
end

- (Object) pusher

Returns the value of attribute pusher



5
6
7
# File 'lib/pry/history.rb', line 5

def pusher
  @pusher
end

- (Object) saver

Returns the value of attribute saver



5
6
7
# File 'lib/pry/history.rb', line 5

def saver
  @saver
end

Instance Method Details

- (Object) clear

Clear all history. Anything the user entered before this point won't be saved, but anything they put in afterwards will still be appended to the history file on exit.



55
56
57
58
59
# File 'lib/pry/history.rb', line 55

def clear
  @clearer.call
  @history = []
  @saved_lines = 0
end

- (Object) clear_readline (private)

The default clearer. Clears Readline::HISTORY.



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

def clear_readline
  Readline::HISTORY.shift until Readline::HISTORY.empty?
end

- (Integer) load

Load the input history using History.loader.

Returns:

  • (Integer)

    The number of lines loaded



23
24
25
26
27
28
29
# File 'lib/pry/history.rb', line 23

def load
  @loader.call do |line|
    @pusher.call(line.chomp)
    @history << line.chomp
  end
  @saved_lines = @history.length
end

- (String) push(line) 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



43
44
45
46
47
48
49
# File 'lib/pry/history.rb', line 43

def push(line)
  unless line.empty? || (@history.last && line == @history.last)
    @pusher.call(line)
    @history << line
  end
  line
end

- (Object) push_to_readline(line) (private)

The default pusher. Appends the given line to Readline::HISTORY.

Parameters:

  • line (String)


100
101
102
# File 'lib/pry/history.rb', line 100

def push_to_readline(line)
  Readline::HISTORY << line
end

- (Object) read_from_file (private)

The default loader. Yields lines from Pry.history.config.file.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pry/history.rb', line 70

def read_from_file
  begin
    history_file = File.expand_path(Pry.config.history.file)
    if File.exists?(history_file)
      File.foreach(history_file) { |line| yield(line) }
    end
  rescue => error
    unless error.message.empty?
      warn "History file not loaded, received an error: #{error.message}"
    end
  end
end

- (Object) restore_default_behavior

Assign the default methods for loading, saving, pushing, and clearing.



14
15
16
17
18
19
# File 'lib/pry/history.rb', line 14

def restore_default_behavior
  @loader  = method(:read_from_file)
  @saver   = method(:write_to_file)
  @pusher  = method(:push_to_readline)
  @clearer = method(:clear_readline)
end

- (Integer) save

Write this session's history using History.saver.

Returns:

  • (Integer)

    The number of lines saved



33
34
35
36
37
38
# File 'lib/pry/history.rb', line 33

def save
  history_to_save = @history[@saved_lines..-1]
  @saver.call(history_to_save)
  @saved_lines = @history.length
  history_to_save.length
end

- (Array<String>) to_a

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.



64
65
66
# File 'lib/pry/history.rb', line 64

def to_a
  @history.dup
end

- (Object) write_to_file(lines) (private)

The default saver. Appends the given lines to Pry.history.config.file.

Parameters:



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pry/history.rb', line 85

def write_to_file(lines)
  history_file = File.expand_path(Pry.config.history.file)

  begin
    File.open(history_file, 'a') do |f|
      lines.each { |ln| f.puts ln }
    end
  rescue Errno::EACCES
    # We should probably create an option Pry.show_warnings?!?!?!
    warn 'Unable to write to your history file, history not saved'
  end
end