Navigation Menu

Skip to content

Commit

Permalink
groonga-client: support repl
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Feb 16, 2018
1 parent 4ebbf71 commit 552f978
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions lib/groonga/client/command-line/groonga-client.rb
@@ -1,4 +1,4 @@
# Copyright (C) 2015-2017 Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2015-2018 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -45,8 +45,12 @@ def run(arguments)
runner = Runner.new(client, @runner_options)

if command_file_paths.empty?
$stdin.each_line do |line|
runner << line
if $stdin.tty? and $stdout.tty?
runner.repl
else
$stdin.each_line do |line|
runner << line
end
end
else
command_file_paths.each do |command_file_path|
Expand Down Expand Up @@ -114,6 +118,16 @@ def finish
@parser.finish
end

def repl
begin
require "readline"
rescue LoadError
repl_bare
else
repl_readline
end
end

private
def create_command_parser
parser = Groonga::Command::Parser.new
Expand Down Expand Up @@ -175,6 +189,25 @@ def run_command(command)
puts(response.body)
end
end

def repl_bare
loop do
print("> ")
$stdout.flush
line = gets
break if line.nil?
self << line
end
end

def repl_readline
loop do
line = Readline.readline("> ", true)
break if line.nil?
self << line
self << "\n"
end
end
end
end
end
Expand Down

0 comments on commit 552f978

Please sign in to comment.