class Groonga::Client::CommandLine::GroongaClient::ReadlineREPL

Public Class Methods

new(runner) click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 201
def initialize(runner)
  @runner = runner
  @history_path = guess_history_path
  read_history
end

Public Instance Methods

run() click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 207
def run
  loop do
    line = Readline.readline("> ", true)
    break if line.nil?
    add_history(line)
    @runner << line
    @runner << "\n"
  end
end

Private Instance Methods

add_history(entry) click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 243
def add_history(entry)
  updated = history_is_updated?

  if new_history_entry?(entry)
    FileUtils.mkdir_p(@history_path.dirname)
    @history_path.open("a") do |history_file|
      history_file << entry
      history_file << "\n"
    end
  else
    Readline::HISTORY.pop
  end

  if updated
    Readline::HISTORY.clear
    read_history
  end
end
guess_history_path() click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 218
def guess_history_path
  case RUBY_PLATFORM
  when /mswin/, /mingw/
    base_dir = ENV["LOCALAPPDATA"] || "~/AppData"
  when /darwin/
    base_dir = "~/Library/Preferences"
  else
    base_dir = ENV["XDG_CONFIG_HOME"] || "~/.config"
  end
  Pathname(base_dir).expand_path + "groonga-client" + "history.txt"
end
history_is_updated?() click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 262
def history_is_updated?
  @history_path.exist? and
    @history_path.mtime > @history_timestamp
end
new_history_entry?(entry) click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 267
def new_history_entry?(entry)
  return false if /\A\s*\z/ =~ entry
  if Readline::HISTORY.size > 1 and Readline::HISTORY[-2] == entry
    return false
  end
  true
end
read_history() click to toggle source
# File lib/groonga/client/command-line/groonga-client.rb, line 230
def read_history
  if @history_path.exist?
    @history_path.open do |history_file|
      history_file.each_line do |line|
        Readline::HISTORY << line.chomp
      end
    end
    @history_timestamp = @history_path.mtime
  else
    @history_timestamp = Time.now
  end
end