Skip to content

Commit

Permalink
autocomplete for search
Browse files Browse the repository at this point in the history
  • Loading branch information
dbuthay committed Nov 27, 2010
1 parent 66100a7 commit c18c520
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
19 changes: 16 additions & 3 deletions console
Expand Up @@ -27,8 +27,17 @@ COMMANDS = {
"quit" => "exits this shell"
}

comp = proc { |s| COMMANDS.keys.grep( /^#{Regexp.escape(s)}/ ) }
comp = proc { |s| if s =~ /^search/ then
# TODO find a rubier way to split / tail
@index.autocomplete(s.split(' ')[1..10].join(' '))['suggestions'].map { |sug| "search #{sug}" }
else
# default, complete with commands
COMMANDS.keys.grep( /^#{Regexp.escape(s)}/ )
end
}

# change the work break character, hacky way to allow multi word autocomplete :)
Readline.completer_word_break_characters = "|"
Readline.completion_append_character = " "
Readline.completion_proc = comp

Expand Down Expand Up @@ -74,7 +83,11 @@ def command(line)
when "suggest"
query = args[1..args.length].join ' '
results = @index.autocomplete(query)
printf results['suggestions']
if results['suggestions'].length > 0
puts results['suggestions'].join ' '
else
printf "no suggestions for %s :(\n", query
end
when "add"
if @index == nil
puts 'set an index first'
Expand Down Expand Up @@ -137,4 +150,4 @@ while line = Readline.readline(get_prompt, true) and line != 'quit'
puts e
end
end

puts 'bye'
15 changes: 14 additions & 1 deletion lib/indextank_client.rb
Expand Up @@ -183,7 +183,20 @@ def search(query, options={})
raise
end
end


def autocomplete(query, options={})
#options.merge!( :query => query, :callback => "mono" )
options.merge!( :query => query )
begin
code, r = GET "/autocomplete", options
return r
rescue HttpCodeException
raise
end
end



def add_function(function_index, definition, options={})
options.merge!( :definition => definition )
code, r = PUT "/functions/#{function_index}", options
Expand Down

0 comments on commit c18c520

Please sign in to comment.