1
- import json, marshal, tables, os
1
+ import json, marshal, tables, os, algorithm, strutils, sequtils
2
2
3
3
# # Helper library for Wox plugin authors
4
- # # Version 0.2 .0
4
+ # # Version 0.3 .0
5
5
# # roose 2016
6
6
7
7
type
24
24
25
25
RpcProc * = proc (params: string )
26
26
27
+ type
28
+ SortBy * = enum
29
+ # # Sort by title or subtitle or title and subtitle
30
+ byTitle, bySub, byTitleSub
31
+
27
32
# name, proc table for call proc
28
33
var procs: Table [string , RpcProc ] = initTable [string , RpcProc ]()
29
34
30
35
proc register * (name: string , prc: RpcProc ) =
31
36
# # Register proc as name
32
- # # :param string name: name to register proc
33
- # # :param proc prc: proc
34
37
procs[name] = prc
35
38
36
39
proc call * (name, params: string ) =
37
40
# # Call proc by it's name
38
- # # :param string name: proc name
39
- # # :param string params: proc parameters
40
41
procs [name](params)
41
42
42
43
proc run * () =
@@ -53,13 +54,6 @@ proc newResult*(): WoxResult =
53
54
54
55
proc addItem * (self: var WoxResult , title, sub, icon, `method`, params: string , hide: bool = true ) =
55
56
# # 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
63
57
64
58
self.result .add (WoxItem (
65
59
Title : title,
@@ -75,3 +69,31 @@ proc addItem*(self: var WoxResult, title, sub, icon, `method`, params: string, h
75
69
proc results * (self: var WoxResult ): string =
76
70
# # Return results with all items
77
71
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