Sha256: 0d0233a10ec7b8bb804cc26bcd967ae66e91cb87389c1c08e6b3021f46ed4ae7

Contents?: true

Size: 1.79 KB

Versions: 20

Compression:

Stored size: 1.79 KB

Contents

class Pry
  # The History class is responsible for maintaining the user's input history, both
  # internally and within Readline::HISTORY.
  class History
    def initialize
      @history = []
      @saved_lines = 0
    end

    # Loads a file's contents into the input history.
    # @param [String] filename
    # @return [Integer] The number of lines loaded
    def load(filename)
      File.foreach(filename) do |line|
        Readline::HISTORY << line.chomp
        @history << line.chomp
      end
      @saved_lines = @history.length
    end

    # Appends input history from this session to a file.
    # @param [String] filename
    # @return [Integer] The number of lines saved
    def save(filename)
      history_to_save = @history[@saved_lines..-1]
      File.open(filename, 'a') do |f|
        history_to_save.each { |ln| f.puts ln }
      end
      @saved_lines = @history.length
      history_to_save.length
    end

    # Adds a line to the input history, ignoring blank and duplicate lines.
    # @param [String] line
    # @return [String] The same line that was passed in
    def push(line)
      unless line.empty? || (@history.last && line == @history.last)
        Readline::HISTORY << line
        @history << line
      end
      line
    end
    alias << push

    # Clears 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.
    def clear
      Readline::HISTORY.shift until Readline::HISTORY.empty?
      @history = []
      @saved_lines = 0
    end

    # Returns an Array containing all stored history.
    # @return [Array<String>] An Array containing all lines of history loaded
    #   or entered by the user in the current session.
    def to_a
      @history.dup
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
pry-0.9.7.4 lib/pry/history.rb
pry-0.9.7.4-i386-mswin32 lib/pry/history.rb
pry-0.9.7.4-i386-mingw32 lib/pry/history.rb
pry-0.9.7.4-java lib/pry/history.rb
pry-0.9.7.3 lib/pry/history.rb
pry-0.9.7.3-i386-mswin32 lib/pry/history.rb
pry-0.9.7.3-i386-mingw32 lib/pry/history.rb
pry-0.9.7.3-java lib/pry/history.rb
pry-0.9.7.2 lib/pry/history.rb
pry-0.9.7.2-i386-mswin32 lib/pry/history.rb
pry-0.9.7.2-i386-mingw32 lib/pry/history.rb
pry-0.9.7.2-java lib/pry/history.rb
pry-0.9.7.1 lib/pry/history.rb
pry-0.9.7.1-i386-mswin32 lib/pry/history.rb
pry-0.9.7.1-i386-mingw32 lib/pry/history.rb
pry-0.9.7.1-java lib/pry/history.rb
pry-0.9.7 lib/pry/history.rb
pry-0.9.7-i386-mswin32 lib/pry/history.rb
pry-0.9.7-i386-mingw32 lib/pry/history.rb
pry-0.9.7-java lib/pry/history.rb