Class SSHMenu::History
In: lib/sshmenu.rb  (Git)
Parent: Object

SSHMenu::History manages the history of hostnames entered in the quick- connect entry box.

Methods

Public Class methods

Constructor, expects a single argument: the config object

[Source]

# File lib/sshmenu.rb, line 1747
    def initialize(config)
      @hist_file = config.home_dir + '.sshmenu_history'
      @timestamp = nil
      load_history
    end

Public Instance methods

Adds a line to the start of the history list and then saves the list to the history file

[Source]

# File lib/sshmenu.rb, line 1773
    def add_line(line)
      @history = [ line, @history.find_all { |l| l != line } ].flatten
      fh = File.new(@hist_file, 'w')
      @history.each { |l| fh.puts(l) }
      fh.close
      @timestamp = File.mtime(@hist_file)
    end

Iterator which yields a list of history strings which match the supplied text string

[Source]

# File lib/sshmenu.rb, line 1784
    def each_match(text, prefix_only = false)
      pattern = Regexp.quote(text)
      @history.each do |line|
        yield(line) if line =~ /^#{pattern}/i  # match at start
      end
      if not prefix_only
        @history.each do |line|
          next if line =~ /^#{pattern}/i
          yield(line) if line =~ /#{pattern}/i   # match, but not at start
        end
      end
    end

Reloads the history file if it has been modified

[Source]

# File lib/sshmenu.rb, line 1765
    def freshen
      return unless File.exists?(@hist_file)
      load_history if File.mtime(@hist_file) != @timestamp
    end

Reads lines from the history file into memory

[Source]

# File lib/sshmenu.rb, line 1755
    def load_history
      @history = []
      if File.readable?(@hist_file)
        @history = @hist_file.readlines.collect{|l| l.chomp}
        @timestamp = File.mtime(@hist_file)
      end
    end

[Validate]