Skip to content

Commit

Permalink
The world's most basic search
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Dec 13, 2011
1 parent ccef7e0 commit 9574244
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/gitdocs/manager.rb
Expand Up @@ -10,6 +10,17 @@ def initialize(config_root, debug)
yield @config if block_given?
end

RepoDescriptor = Struct.new(:name, :index)

def search(term)
results = {}
@runners.each_with_index do |runner, index|
descriptor = RepoDescriptor.new(runner.root, index)
results[descriptor] = runner.search(term)
end
results
end

def run
run = true
trap('USR1') { run = true; EM.stop }
Expand All @@ -21,11 +32,11 @@ def run
# Start the repo watchers
runners = nil
EM.run do
runners = config.shares.map { |share| Runner.new(share) }
runners.each(&:run)
@runners = config.shares.map { |share| Runner.new(share) }
@runners.each(&:run)
# Start the web front-end
if self.config.global.start_web_frontend
Server.new(self, *runners).start
Server.new(self, *@runners).start
i = 0
web_started = false
begin
Expand All @@ -39,7 +50,7 @@ def run
system("open http://localhost:8888/") if self.config.global.load_browser_on_startup && web_started
end
end
sleep(10) if runners && runners.empty?
sleep(10) if @runners && @runners.empty?
end
end

Expand Down
9 changes: 9 additions & 0 deletions lib/gitdocs/runner.rb
Expand Up @@ -11,6 +11,15 @@ def initialize(share)
@icon = File.expand_path("../../img/icon.png", __FILE__)
end

SearchResult = Struct.new(:file, :context)
def search(term)
results = []
sh_string("git grep #{ShellTools.escape(term)}").scan(/(.*?):([^\n]*)/) do |(file, context)|
results << SearchResult.new(file, context)
end
results
end

def run
return false unless self.valid?
out, status = sh_with_code "which growlnotify"
Expand Down
4 changes: 4 additions & 0 deletions lib/gitdocs/server.rb
Expand Up @@ -36,6 +36,10 @@ def start(port = 8888)
end
end

path('search').get do
render! "search", :layout => 'app', :locals => {:conf => manager.config, :results => manager.search(request.GET['q']), :nav_state => nil}
end

var :int do |idx|
gd = gds[idx]
halt 404 if gd.nil?
Expand Down
2 changes: 2 additions & 0 deletions lib/gitdocs/views/app.haml
Expand Up @@ -19,6 +19,8 @@
%a(href = "/") Home
%li{ :class => ("active" if nav_state == "settings") }
%a(href = "/settings") Settings
%form{:class => "pull-left", :action => "/search", :method => 'GET'}
%input{:type => "text", :placeholder => "Search", :name => 'q'}

#main.container
.content
Expand Down
10 changes: 10 additions & 0 deletions lib/gitdocs/views/search.haml
@@ -0,0 +1,10 @@
-results.each do |repo, search_results|
%h2
=repo.name
%dl
-search_results.each do |res|
%dt
%a{:href => "/#{repo.index}/#{res.file}"}
="/#{res.file}"
%dd
=res.context

0 comments on commit 9574244

Please sign in to comment.