3
# axi-searchasyoutype - Search-as-you-type demo
5
# Copyright (C) 2007 Enrico Zini <enrico@debian.org>
6
# Copyright (C) 2008 Daniel Brumbaugh Keeney
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#############################
24
# INCOMPLETE, does not work
26
#############################
30
OptionParser.new do |opts|
32
opts.program_name = 'axi-query-pkgtype.rb'
34
opts.release = '1203587714'
37
"Query the Apt Xapian index. Command line arguments can be keywords or Debtags tags"
40
opts.on '-t', '--type TYPE', "package type, one of 'game', 'gui', 'cmdline' or 'editor'" do |v|
44
opts.on_tail("-h", "--help", "Show this message") do
49
end.parse! rescue ( puts 'try axi-query-pkgtype.rb --help'; exit 2 )
51
args = ARGV.collect do |i| i.dup; end
54
# Import the rest here so we don't need dependencies to be installed only to
55
# print commandline help
57
require 'aptxapianindex'
61
# Instantiate a xapian.Database object for read only access to the index
62
db = Xapian::Database.new(XAPIANDB)
64
# Show the results of a query while we type it
68
maxy, maxx = stdscr.getmaxyx
71
win = curses.newwin(size, maxx, 0, 0)
75
def noresults(suggestion = "type more")
77
win.addstr(0, 0, "No results, " + suggestion, curses.A_BOLD)
81
# Show the results of the search done using the given line of text
83
line = line.lower.strip
85
# No text given: abort
90
# Split the line in words
91
args = splitline.split(line)
94
# No text given: abort
99
# To understand the following code, keep in mind that we do
100
# search-as-you-type, so the last word may be partially typed.
102
if args[-1].length == 1
103
# If the last term has only one character, we ignore it waiting for
104
# the user to type more. A better implementation can set up a
105
# timer to disable this some time after the user stopped typing, to
106
# give meaningful results to searches like "programming in c"
114
# Convert the words into terms for the query
115
terms = terms_for_simple_query(args)
117
# Since the last word can be partially typed, we add all words that
118
# begin with the last one.
119
terms.extend(db.allterms(args[-1]).collect do |x| x.term; end)
122
query = Xapian::Query.new(Xapian::Query::OP_OR, terms)
124
# Add the simple user filter, if requested. This is to show that even
125
# when we do search-as-you-type, everything works as usual
126
query = addSimpleFilterToQuery(query, type)
129
enquire = Xapian::Enquire.new(db)
130
enquire.query = query
132
# This does the adaptive cutoff trick on the query results (see
133
# axi-query-adaptivecutoff.py)
134
mset = enquire.mset(0, 1)
136
top_weight = mset[0].weight
138
noresults 'change your query'
141
enquire.set_cutoff(0, top_weight * 0.7)
143
# Retrieve as many results as we can show
144
mset = enquire.mset(0, self.size - 1)
150
self.win.addstr(0, 0, "%i results found." % mset.matches_estimated, curses.A_BOLD)
153
mset.each_pair do |y, m|
154
# /var/lib/apt-xapian-index/README tells us that the Xapian document data
155
# is the package name.
156
name = m.document.data
158
# Print the match, together with the short description
159
self.win.addstr(y+1, 0, "%i%% %s - %s" % [m.percent, name, pkg.summary])
166
# Build the base query as seen in axi-query-simple.py
169
def initialize stdscr, results
170
maxy, maxx = stdscr.getmaxyx
172
win = curses.newwin(1, maxx, maxy-1, 0)
173
win.bkgdset(' ', curses.A_REVERSE)
175
win.addstr(0, 0, "> ", curses.A_REVERSE)
183
break if c == 10 or c == 27
193
win.addstr(0, 0, "> " + self.line, curses.A_REVERSE)
196
results.update(self.line)
204
results = Results.new(stdscr)
205
input = Input(stdscr, results)
210
# curses.wrapper(main)