Skip to content

Commit

Permalink
improve search results
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderStrike committed Dec 12, 2024
1 parent f9ed99a commit f793cf7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
files
browzr
.vscode
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

type FileInfo struct {
Path string `json:"path"`
Size string `json:"size"`
Path string `json:"path"` // Full path for the href
Dir string `json:"dir"` // Directory part
Filename string `json:"filename"` // Just the filename
Size string `json:"size"` // File size
}

func main() {
Expand Down Expand Up @@ -62,9 +64,12 @@ func fileJSON() []byte {
if info.IsDir() {
return nil
}
dir, file := filepath.Split(path)
files = append(files, FileInfo{
Path: path,
Size: humanizeBytes(info.Size()),
Path: path,
Dir: dir,
Filename: file,
Size: humanizeBytes(info.Size()),
})
return nil
})
Expand Down
25 changes: 16 additions & 9 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ fetch("/filelist")
// console.log(data)
const options = {
includeScore: true,
isCaseSensitive: true,
shouldSort: true,
keys: ['path']
threshold: 0.4,
location: 0,
distance: 100,
minMatchCharLength: 2,
keys: [{
name: 'filename',
weight: 2.0
}, {
name: 'dir',
weight: 1.0
}],
findAllMatches: true,
ignoreLocation: true
}

const fuse = new Fuse(data, options)
Expand All @@ -22,19 +33,15 @@ fetch("/filelist")
}

function performSearch(query) {
const result = fuse.search(query.toLowerCase()).reverse()
const result = fuse.search(query.toLowerCase())
const elements = result.slice(0, 200).map(x => {
const file = x.item
const pathParts = file.path.split('/')
const filename = pathParts.pop()
const path = pathParts.length ? pathParts.join('/') + '/' : ''

return `<a href="${file.path}">
<li>
${getFileIcon(file.path)}
${getFileIcon(file.filename)}
<span class="file-details">
<span class="filename">
<span class="filepath">${path}</span>${filename}
<span class="filepath">${file.dir}</span>${file.filename}
</span>
<span class="filesize">${file.size}</span>
</span>
Expand Down

0 comments on commit f793cf7

Please sign in to comment.