Sha256: 095ffaa197eb133fb9a81d3458e62905ae994eeb2bd46c9ff62d04a8e0adde44

Contents?: true

Size: 1.81 KB

Versions: 36

Compression:

Stored size: 1.81 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.strip == @history.last.strip)
        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

36 entries across 36 versions & 1 rubygems

Version Path
pry-0.9.6.2 lib/pry/history.rb
pry-0.9.6.2-i386-mswin32 lib/pry/history.rb
pry-0.9.6.2-i386-mingw32 lib/pry/history.rb
pry-0.9.6.2-java lib/pry/history.rb
pry-0.9.6.1-i386-mswin32 lib/pry/history.rb
pry-0.9.6.1-i386-mingw32 lib/pry/history.rb
pry-0.9.6 lib/pry/history.rb
pry-0.9.6-i386-mswin32 lib/pry/history.rb
pry-0.9.6-i386-mingw32 lib/pry/history.rb
pry-0.9.6-java lib/pry/history.rb
pry-0.9.5 lib/pry/history.rb
pry-0.9.5-i386-mswin32 lib/pry/history.rb
pry-0.9.5-i386-mingw32 lib/pry/history.rb
pry-0.9.5-java lib/pry/history.rb
pry-0.9.4pre6 lib/pry/history.rb
pry-0.9.4pre6-i386-mswin32 lib/pry/history.rb
pry-0.9.4pre6-i386-mingw32 lib/pry/history.rb
pry-0.9.4pre6-java lib/pry/history.rb
pry-0.9.4-i386-mswin32 lib/pry/history.rb
pry-0.9.4-i386-mingw32 lib/pry/history.rb