]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/search-results-mode.rb
Merge branch 'master' into next
[sup] / lib / sup / modes / search-results-mode.rb
index 5502b3cdaff2f54a5b794b6527c76bc45228d230..227ee9ba7f7a469ed751a45a9bcc52a767c697b9 100644 (file)
@@ -1,27 +1,40 @@
 module Redwood
 
 class SearchResultsMode < ThreadIndexMode
-  def initialize qobj
+  def initialize qobj, qopts = nil
     @qobj = qobj
-    super
+    @qopts = qopts
+
+    super [], { :qobj => @qobj }.merge(@qopts)
+  end
+
+  register_keymap do |k|
+    k.add :refine_search, "Refine search", '|'
+  end
+
+  def refine_search
+    query = BufferManager.ask :search, "refine query: ", (@qobj.to_s + " ")
+    return unless query && query !~ /^\s*$/
+    SearchResultsMode.spawn_from_query query
   end
 
-  ## TODO: think about this
-  def is_relevant? m; super; end
+  ## a proper is_relevant? method requires some way of asking ferret
+  ## if an in-memory object satisfies a query. i'm not sure how to do
+  ## that yet. in the worst case i can make an in-memory index, add
+  ## the message, and search against it to see if i have > 0 results,
+  ## but that seems pretty insane.
 
-  def load_threads opts={}
-    n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
-    load_n_threads_background n, :qobj => @qobj,
-                                 :load_killed => true,
-                                 :load_spam => false,
-                                 :when_done =>(lambda do |num|
-      opts[:when_done].call if opts[:when_done]
-      if num > 0
-        BufferManager.flash "Found #{num} threads"
-      else
-        BufferManager.flash "No matches"
-      end
-    end)
+  def self.spawn_from_query text
+    begin
+      qobj, extraopts = Index.parse_user_query_string(text)
+      return unless qobj
+      short_text = text.length < 20 ? text : text[0 ... 20] + "..."
+      mode = SearchResultsMode.new qobj, extraopts
+      BufferManager.spawn "search: \"#{short_text}\"", mode
+      mode.load_threads :num => mode.buffer.content_height
+    rescue Index::ParseError => e
+      BufferManager.flash "Problem: #{e.message}!"
+    end
   end
 end