Skip to content

Commit

Permalink
1. 重构,搜索更加稳定
Browse files Browse the repository at this point in the history
2. 增加tags用于辅助搜索
  • Loading branch information
mrdear committed Feb 22, 2021
1 parent 8fd684c commit d39b6fa
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 179 deletions.
77 changes: 38 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,38 @@

该workflow把命令分为key -> values形式,如下所示,key属于大分类,匹配到key后会显示其下全部value.

目前支持json以及yaml格式配置
目前支持yaml格式(推荐)以及json配置,这里以yaml为例介绍字段含义

**json格式**
```yaml
- key: 用于搜索指令
remark: 用于指令描述
tags: 辅助搜索关键词,可以不填写
values:
- cmd: 搜索命中后展示的内容
remark: 命中后内容描述
```
**eg:yaml格式**
```yaml
- 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使用淘宝源安装
```
**eg:json格式**
```json
[
{
Expand All @@ -33,54 +62,24 @@
]
```

**yaml格式**

```yaml
- key: 搜索引擎
remark: 搜索引擎
values:
- cmd: https://www.baidu.com/s?wd={clip_0}
remark: 百度地址
- key: git-common
remark: git通用命令
values:
- cmd: git branch -r | sed 's/origin///g' | grep '/' | xargs git push origin --delete
remark: git批量删除远程分支}

```

主要功能:
1. cmd(触发关键词)->搜索key->选择value->复制到粘贴板(可以自动粘贴)
2. cmd(触发关键词)->搜索key->选择value-> 判断是网址 -> 调用浏览器打开
3. cmd(触发关键词)->open->选择打开命令配置->调用你喜欢的编辑器打开命令配置的json(主要是alfred添加数据不太好用)
3. cmd(触发关键词)->open->选择打开命令配置->调用你喜欢的编辑器打开命令配置(需要在workflow中配置打开应用,默认是TextEdit)

支持获取粘贴板,使`{clip_0}`来代替,最终渲染时会自动进行粘贴板数据替换。比如我选择了`https://www.baidu.com/s?wd={clip_0}`,此时我粘贴板数据假设为 `张三`,那么最终打开浏览器的地址为`https://www.baidu.com/s?wd=张三`

### 数据保存
该插件对应json命令数据都是外置的(便于云端保存,丢到同步盘中即可),因此自己指定一个路径后,以参数形式传入即可.
该插件对应命令数据支持外置的(便于云端保存,丢到同步盘中即可),因此自己指定一个路径后,以参数形式传入即可.

也由于json外置,如果你觉得workflow添加太麻烦的话,直接改这个json,添加对应的数据即可.

![](http://oobu4m7ko.bkt.clouddn.com/1519546315.png)
![](https://imgblog.mrdear.cn/uPic/PJgMxW_1614003592.png)

#### 更多变量支持
变量的支持依赖于alfred,可以在自己的脚本中配置多个变量,在后面使用`Utils`工具替换。
![](http://imgblog.mrdear.cn/1539613678.png?imageMogr2/thumbnail/!100p)

### 演示

![](https://github.com/mrdear/Command_Search/blob/master/img/yulan.gif)


### 其他问题
![](https://github.com/mrdear/Command_Search/blob/master/assert/yulan.gif)

#### 不想要自动粘贴

在alfred插件中选择粘贴这个环节

![](http://oobu4m7ko.bkt.clouddn.com/1519546487.png)

然后勾掉自动粘贴即可.

![](http://oobu4m7ko.bkt.clouddn.com/1519546513.png)

#### 更多变量支持
变量的支持依赖于alfred,可以在自己的脚本中配置多个变量,在后面使用`Utils`工具替换。
![](http://imgblog.mrdear.cn/1539613678.png?imageMogr2/thumbnail/!100p)
40 changes: 0 additions & 40 deletions action/KeyAction.go

This file was deleted.

58 changes: 0 additions & 58 deletions action/ResultParse.go

This file was deleted.

94 changes: 94 additions & 0 deletions action/SearchAction.go
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))
}
15 changes: 15 additions & 0 deletions assert/dataTpl.yaml
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
22 changes: 0 additions & 22 deletions data.json

This file was deleted.

2 changes: 2 additions & 0 deletions go.mod
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
Loading

0 comments on commit d39b6fa

Please sign in to comment.