Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit d3c7fba

Browse files
committed
nim v0.3.0, add sorting results
1 parent 53a81e6 commit d3c7fba

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

README.NIM.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ proc query*(query: string) =
2222
let desc = item["description"][0].str
2323
let url = item["homepage"][0].str
2424
result.addItem(title, desc, "Images\\exe.png", "openUrl", url, false)
25+
# sort results
26+
result.sort(query)
2527
# print results json
2628
echo result.results()
2729

wox.nim

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import json, marshal, tables, os
1+
import json, marshal, tables, os, algorithm, strutils, sequtils
22

33
## Helper library for Wox plugin authors
4-
## Version 0.2.0
4+
## Version 0.3.0
55
## roose 2016
66

77
type
@@ -24,19 +24,20 @@ type
2424

2525
RpcProc* = proc (params: string)
2626

27+
type
28+
SortBy* = enum
29+
## Sort by title or subtitle or title and subtitle
30+
byTitle, bySub, byTitleSub
31+
2732
# name, proc table for call proc
2833
var procs: Table[string, RpcProc] = initTable[string, RpcProc]()
2934

3035
proc register*(name: string, prc: RpcProc) =
3136
## Register proc as name
32-
## :param string name: name to register proc
33-
## :param proc prc: proc
3437
procs[name] = prc
3538

3639
proc call*(name, params: string) =
3740
## Call proc by it's name
38-
## :param string name: proc name
39-
## :param string params: proc parameters
4041
procs[name](params)
4142

4243
proc run*() =
@@ -53,13 +54,6 @@ proc newResult*(): WoxResult =
5354

5455
proc addItem*(self: var WoxResult, title, sub, icon, `method`, params: string, hide: bool = true) =
5556
## Add item to the return list
56-
##
57-
## :param str title: item title
58-
## :param str sub: item subtitle
59-
## :param str icon: path to item icon
60-
## :param str method: a function called when an item is selected
61-
## :param list params: parameters for callable function
62-
## :param str hide: hide Wox after select item or not
6357

6458
self.result.add(WoxItem(
6559
Title: title,
@@ -75,3 +69,31 @@ proc addItem*(self: var WoxResult, title, sub, icon, `method`, params: string, h
7569
proc results*(self: var WoxResult): string =
7670
## Return results with all items
7771
return $$self
72+
73+
proc sort*(self: var WoxResult, query: string, sortBy = byTitleSub) =
74+
## Fuzzy sorting the results, default sorted by title and subtitle
75+
76+
proc score(value: string): float =
77+
## Calculate score
78+
79+
var score = 0.0
80+
if value.toLower.startsWith(query):
81+
score = 100.0 - (value.len / query.len)
82+
elif query in value.toLower:
83+
score = 80.0 - (value.len / query.len)
84+
return score
85+
86+
self.result.sort(
87+
proc(x, y: WoxItem) :int =
88+
var text: array[0..1, string]
89+
case sortBy:
90+
of byTitle:
91+
text = [x.Title, y.Title]
92+
of bySub:
93+
text = [x.SubTitle, y.SubTitle]
94+
of byTitleSub:
95+
text = [x.Title & " " & x.SubTitle, y.Title & " " & y.SubTitle]
96+
else:
97+
text = [x.Title & " " & x.SubTitle, y.Title & " " & y.SubTitle]
98+
cmp(score(text[0]), score(text[1])), SortOrder.Descending
99+
)

0 commit comments

Comments
 (0)