-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. 增加tags用于辅助搜索
- Loading branch information
Showing
10 changed files
with
203 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package action | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/work-helper/command-search-alfred/model" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var re = regexp.MustCompile("[-_\\s]") | ||
var icon = model.Path{Path: "icon.png"} | ||
|
||
func SearchKeysAndPrint(searchKey string, projects []model.Project) { | ||
// 为空则默认返回 | ||
if "" == searchKey { | ||
parseCommands(projects) | ||
return | ||
} | ||
|
||
// 查找 | ||
searchKey = re.ReplaceAllString(searchKey, "") | ||
var matchCommands []model.Project | ||
var noMatchCommands []model.Project | ||
for _, v := range projects { | ||
//以key开头 | ||
key := re.ReplaceAllString(v.Key, "") | ||
tag := re.ReplaceAllString(v.Tags, "") | ||
if strings.Contains(key, searchKey) || strings.Contains(tag, searchKey) { | ||
matchCommands = append(matchCommands, v) | ||
} else { | ||
noMatchCommands = append(noMatchCommands, v) | ||
} | ||
|
||
// 完全匹配则直接处理 | ||
if strings.EqualFold(key, searchKey) { | ||
parseCommandValue(&v) | ||
return | ||
} | ||
} | ||
|
||
//部分匹配的处理,匹配上的优先排序 | ||
if len(matchCommands) == 0 { | ||
matchCommands = projects | ||
parseCommands(matchCommands) | ||
} else { | ||
matchCommands = append(matchCommands, noMatchCommands...) | ||
parseCommands(matchCommands) | ||
} | ||
} | ||
|
||
// 解析外围指令 | ||
func parseCommands(commands []model.Project) { | ||
var items = make([]model.Item, 0, len(commands)) | ||
for _, v := range commands { | ||
var temp = "" | ||
if "" != v.Tags { | ||
temp = "\t\t--" + v.Tags | ||
} | ||
|
||
var item = model.Item{ | ||
Autocomplete: v.Key, | ||
Arg: "", | ||
Title: v.Key, | ||
Subtitle: v.Remark + temp, | ||
Icon: icon, | ||
} | ||
items = append(items, item) | ||
} | ||
|
||
PrintResult(items) | ||
} | ||
|
||
// 解析匹配后内部指令 | ||
func parseCommandValue(command *model.Project) { | ||
var items = make([]model.Item, 0, len(command.Values)) | ||
for _, v := range command.Values { | ||
var item = model.Item{ | ||
Autocomplete: command.Key, | ||
Arg: v.Cmd, | ||
Title: v.Remark, | ||
Subtitle: v.Cmd, | ||
Icon: icon, | ||
} | ||
items = append(items, item) | ||
} | ||
PrintResult(items) | ||
} | ||
|
||
func PrintResult(items []model.Item) { | ||
result := model.Items{Items: items} | ||
bytes, _ := json.Marshal(result) | ||
fmt.Println(string(bytes)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
- key: ss | ||
remark: 搜索引擎 | ||
tags: sousuo,baidu,duoji | ||
values: | ||
- cmd: https://www.baidu.com/s?wd={clip_0} | ||
remark: 百度地址 | ||
- cmd: https://www.dogedoge.com/results?q={clip_0} | ||
remark: 多吉搜索 | ||
- key: cc | ||
remark: 通用命令 | ||
values: | ||
- cmd: git branch -r | sed 's/origin///g' | grep '/' | xargs git push origin --delete | ||
remark: git批量删除远程分支} | ||
- cmd: npm install --registry=http://registry.npm.alibaba-inc.com {clip_0} | ||
remark: npm使用淘宝源安装 |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/work-helper/command-search-alfred | ||
|
||
go 1.15 | ||
|
||
require gopkg.in/yaml.v2 v2.2.1 |
Oops, something went wrong.