Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 9d4d4db

Browse files
committed
Add GUI filters for selecting Language and Repo
1 parent dd9b88b commit 9d4d4db

File tree

3 files changed

+150
-49
lines changed

3 files changed

+150
-49
lines changed

web/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ type Repository struct {
107107

108108
// PrintInput is provided to the server.Print template.
109109
type PrintInput struct {
110-
Repo, Name string
111-
Lines []string
112-
Last LastInput
110+
Repo, Name string
111+
Lines []string
112+
Last LastInput
113+
FileMatches []*FileMatch
113114
}

web/server.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ var Funcmap = template.FuncMap{
4242
"More": func(orig int) int {
4343
return orig * 3
4444
},
45+
"IsLangFilterChecked": func(query, lang string) bool {
46+
return strings.Contains(query, "lang:"+lang)
47+
},
48+
"IsRepoFilterChecked": func(query, repo string) bool {
49+
return strings.Contains(query, "r:"+repo)
50+
},
51+
"Repos": func(fileMatches []*FileMatch) map[string]int {
52+
repos := make(map[string]int)
53+
for _, fileMatch := range fileMatches {
54+
repos[fileMatch.Repo]++
55+
}
56+
57+
return repos
58+
},
59+
"Languages": func(fileMatches []*FileMatch) map[string]int {
60+
languages := make(map[string]int)
61+
for _, fileMatch := range fileMatches {
62+
languages[fileMatch.Language]++
63+
}
64+
65+
return languages
66+
},
4567
"HumanUnit": func(orig int64) string {
4668
b := orig
4769
suffix := ""
@@ -542,6 +564,11 @@ func (s *Server) servePrintErr(w http.ResponseWriter, r *http.Request) error {
542564
strLines = append(strLines, string(l))
543565
}
544566

567+
fileMatches, err := s.formatResults(result, queryStr, s.Print)
568+
if err != nil {
569+
return err
570+
}
571+
545572
d := PrintInput{
546573
Name: f.FileName,
547574
Repo: f.Repository,
@@ -551,6 +578,7 @@ func (s *Server) servePrintErr(w http.ResponseWriter, r *http.Request) error {
551578
Num: num,
552579
AutoFocus: false,
553580
},
581+
FileMatches: fileMatches,
554582
}
555583

556584
var buf bytes.Buffer

web/templates.go

Lines changed: 118 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ var TemplateText = map[string]string{
7575
padding: unset;
7676
overflow: unset;
7777
}
78+
79+
.info-count {
80+
margin-bottom: 0.5rem;
81+
display: flex;
82+
justify-content: space-between;
83+
}
84+
7885
:target { background-color: #ccf; }
7986
table tbody tr td { border: none !important; padding: 2px !important; }
8087
</style>
@@ -122,15 +129,61 @@ var TemplateText = map[string]string{
122129
<input class="form-control"
123130
placeholder="Search for some code..." role="search"
124131
id="navsearchbox" type="text" name="q" autofocus
125-
{{if .Query}}
126-
value={{.Query}}
132+
{{if .Last.Query}}
133+
value={{.Last.Query}}
127134
{{end}}>
128135
<div class="input-group">
129136
<div class="input-group-addon">Max Results</div>
130-
<input class="form-control" type="number" id="maxhits" name="num" value="{{.Num}}">
137+
<input class="form-control" type="number" id="maxhits" name="num" value="{{.Last.Num}}">
131138
</div>
132-
<button class="btn btn-primary">Search</button>
133139
</div>
140+
<div class="input-group dropdown">
141+
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
142+
Language
143+
<span class="caret"></span>
144+
</button>
145+
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
146+
{{range $key, $value := Languages .FileMatches}}
147+
<li><a>
148+
<div class="info-count">
149+
<div>
150+
{{if IsLangFilterChecked $.Last.Query $key}}
151+
<input type="checkbox" checked onclick="addFilter('lang:{{$key}}')"/>
152+
{{else}}
153+
<input type="checkbox" onclick="addFilter('lang:{{$key}}')"/>
154+
{{end}}
155+
{{$key}}
156+
</div>
157+
<div class="badge badge-pill">{{$value}}</div>
158+
</div>
159+
</a></li>
160+
{{end}}
161+
</ul>
162+
</div>
163+
<div class="input-group dropdown">
164+
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
165+
Repo
166+
<span class="caret"></span>
167+
</button>
168+
<ul class="dropdown-menu" aria-labelledby="dropdownMenu2">
169+
{{range $key, $value := Repos .FileMatches}}
170+
<li><a>
171+
<div class="info-count">
172+
<div>
173+
{{if IsRepoFilterChecked $.Last.Query $key}}
174+
<input type="checkbox" checked onclick="addFilter('r:{{$key}}')"/>
175+
{{else}}
176+
<input type="checkbox" onclick="addFilter('r:{{$key}}')"/>
177+
{{end}}
178+
{{$key}}
179+
</div>
180+
<div class="badge badge-pill">{{$value}}</div>
181+
</div>
182+
</a></li>
183+
{{end}}
184+
</ul>
185+
</div>
186+
<button class="btn btn-primary">Search</button>
134187
</form>
135188
</div>
136189
</div>
@@ -205,49 +258,70 @@ var TemplateText = map[string]string{
205258
window.location.href = "/search?q=" + escape("{{.QueryStr}}" + " " + atom) +
206259
"&" + "num=" + {{.Last.Num}};
207260
}
261+
262+
function addFilter(filter) {
263+
var searchBox = document.getElementById("navsearchbox");
264+
var search = document.querySelector(".btn.btn-primary");
265+
266+
var oldQuery = searchBox.value.trim();
267+
268+
if (oldQuery.includes(filter)) {
269+
searchBox.value = oldQuery.replace(filter, '');
270+
search.click();
271+
return;
272+
}
273+
274+
searchBox.value = (oldQuery + ' ' + filter).trim();
275+
search.click();
276+
}
277+
208278
</script>
279+
</style>
209280
<body id="results">
210-
{{template "navbar" .Last}}
211-
<div class="container-fluid container-results">
212-
<h5>
213-
{{if .Stats.Crashes}}<br><b>{{.Stats.Crashes}} shards crashed</b><br>{{end}}
214-
{{ $fileCount := len .FileMatches }}
215-
Found {{.Stats.MatchCount}} results in {{.Stats.FileCount}} files{{if or (lt $fileCount .Stats.FileCount) (or (gt .Stats.ShardsSkipped 0) (gt .Stats.FilesSkipped 0)) }},
216-
showing top {{ $fileCount }} files (<a rel="nofollow"
217-
href="search?q={{.Last.Query}}&num={{More .Last.Num}}">show more</a>).
218-
{{else}}.{{end}}
219-
</h5>
220-
{{range .FileMatches}}
221-
<table class="table table-hover table-condensed">
222-
<thead>
223-
<tr>
224-
<th>
225-
{{if .URL}}<a name="{{.ResultID}}" class="result"></a><a href="{{.URL}}" >{{else}}<a name="{{.ResultID}}">{{end}}
226-
<small>
227-
{{.Repo}}:{{.FileName}}</a>:
228-
<span style="font-weight: normal">[ {{if .Branches}}{{range .Branches}}<span class="label label-default">{{.}}</span>,{{end}}{{end}} ]</span>
229-
{{if .Language}}<button
230-
title="restrict search to files written in {{.Language}}"
231-
onclick="zoektAddQ('lang:{{.Language}}')" class="label label-primary">language {{.Language}}</button></span>{{end}}
232-
{{if .DuplicateID}}<a class="label label-dup" href="#{{.DuplicateID}}">Duplicate result</a>{{end}}
233-
</small>
234-
</th>
235-
</tr>
236-
</thead>
237-
{{if not .DuplicateID}}
238-
<tbody>
239-
{{range .Matches}}
240-
<tr>
241-
<td style="background-color: rgba(238, 238, 255, 0.6);">
242-
<pre class="inline-pre"><span class="noselect">{{if .URL}}<a href="{{.URL}}">{{end}}<u>{{.LineNum}}</u>{{if .URL}}</a>{{end}}: </span>{{range .Fragments}}{{LimitPre 100 .Pre}}<b>{{.Match}}</b>{{LimitPost 100 .Post}}{{end}}</pre>
243-
</td>
244-
</tr>
281+
{{template "navbar" .}}
282+
<div class="search-main">
283+
<div class="container-fluid container-results">
284+
<h5>
285+
{{if .Stats.Crashes}}<br><b>{{.Stats.Crashes}} shards crashed</b><br>{{end}}
286+
{{ $fileCount := len .FileMatches }}
287+
Found {{.Stats.MatchCount}} results in {{.Stats.FileCount}} files{{if or (lt $fileCount .Stats.FileCount) (or (gt .Stats.ShardsSkipped 0) (gt .Stats.FilesSkipped 0)) }},
288+
showing top {{ $fileCount }} files (<a rel="nofollow"
289+
href="search?q={{.Last.Query}}&num={{More .Last.Num}}">show more</a>).
290+
{{else}}.{{end}}
291+
</h5>
292+
{{range .FileMatches}}
293+
<table class="table table-hover table-condensed">
294+
<thead>
295+
<tr>
296+
<th>
297+
{{if .URL}}<a name="{{.ResultID}}" class="result"></a><a href="{{.URL}}" >{{else}}<a name="{{.ResultID}}">{{end}}
298+
<small>
299+
{{.Repo}}:{{.FileName}}</a>:
300+
<span style="font-weight: normal">[ {{if .Branches}}{{range .Branches}}<span class="label label-default">{{.}}</span>,{{end}}{{end}} ]</span>
301+
{{if .Language}}<button
302+
title="restrict search to files written in {{.Language}}"
303+
onclick="zoektAddQ('lang:{{.Language}}')" class="label label-primary">language {{.Language}}</button></span>{{end}}
304+
{{if .DuplicateID}}<a class="label label-dup" href="#{{.DuplicateID}}">Duplicate result</a>{{end}}
305+
</small>
306+
</th>
307+
</tr>
308+
</thead>
309+
{{if not .DuplicateID}}
310+
<tbody>
311+
{{range .Matches}}
312+
<tr>
313+
<td style="background-color: rgba(238, 238, 255, 0.6);">
314+
<pre class="inline-pre"><span class="noselect">{{if .URL}}<a href="{{.URL}}">{{end}}<u>{{.LineNum}}</u>{{if .URL}}</a>{{end}}: </span>{{range .Fragments}}{{LimitPre 100 .Pre}}<b>{{.Match}}</b>{{LimitPost 100 .Post}}{{end}}</pre>
315+
</td>
316+
</tr>
317+
{{end}}
318+
</tbody>
245319
{{end}}
246-
</tbody>
320+
</table>
247321
{{end}}
248-
</table>
249-
{{end}}
250322
323+
</div>
324+
</div>
251325
<nav class="navbar navbar-default navbar-bottom">
252326
<div class="container">
253327
{{template "footerBoilerplate"}}
@@ -262,18 +336,16 @@ var TemplateText = map[string]string{
262336
</p>
263337
</div>
264338
</nav>
265-
</div>
266339
{{ template "jsdep"}}
267340
</body>
268341
</html>
269342
`,
270-
271343
"repolist": `
272344
<html>
273345
{{template "head"}}
274346
<body id="results">
275347
<div class="container">
276-
{{template "navbar" .Last}}
348+
{{template "navbar" .}}
277349
<div><b>
278350
Found {{.Stats.Repos}} repositories ({{.Stats.Documents}} files, {{HumanUnit .Stats.ContentBytes}}b content)
279351
</b></div>
@@ -321,7 +393,7 @@ var TemplateText = map[string]string{
321393
{{template "head"}}
322394
<title>{{.Repo}}:{{.Name}}</title>
323395
<body id="results">
324-
{{template "navbar" .Last}}
396+
{{template "navbar" .}}
325397
<div class="container-fluid container-results" >
326398
<div class="table table-hover table-condensed" style="overflow:auto; background: #eef;">
327399
{{ range $index, $ln := .Lines}}

0 commit comments

Comments
 (0)