alfred/workflow/workflow.go
2026-03-28 19:27:32 +08:00

99 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package workflow
import (
aw "github.com/deanishe/awgo"
"gitea.micah.wiki/pandora/alfred/command"
)
// 初始化 aw 实例,这是 awgo 库的核心对象
var wf *aw.Workflow
func Do() {
// 初始化 Workflow启用日志方便调试日志会输出到 Alfred 的调试面板)
wf = aw.New()
// 初始化错误及校验参数
var err error
args := wf.Args()
if len(args) <= 1 {
return
}
// 默认返回 SendFeed
defer func() {
if err == nil {
wf.SendFeedback()
return
}
}()
// 命令获取
cmd := args[0]
params := make([]string, 0)
if len(args) > 1 {
params = args[1:]
}
items := command.Do(cmd, params)
wfItems := make([]*aw.Item, len(items))
for i, item := range items {
wfItem := &aw.Item{}
if item.Title != "" {
wfItem.Title(item.Title)
}
if item.Subtitle != nil {
wfItem.Subtitle(*item.Subtitle)
}
if item.Match != nil {
wfItem.Match(*item.Match)
}
if item.UID != nil {
wfItem.UID(*item.UID)
}
if item.Autocomplete != nil {
wfItem.Autocomplete(*item.Autocomplete)
}
if len(item.Arg) > 0 {
wfItem.Arg(item.Arg...)
}
if item.Valid {
wfItem.Valid(item.Valid)
}
if item.File {
wfItem.IsFile(item.File)
}
if item.Copytext != nil {
wfItem.Copytext(*item.Copytext)
}
if item.LargeType != nil {
wfItem.Largetype(*item.LargeType)
}
if item.QL != nil {
wfItem.Quicklook(*item.QL)
}
if len(item.Vars) > 0 {
for key, value := range item.Vars {
wfItem.Var(key, value)
}
}
if len(item.Mods) > 0 {
for _, mod := range item.Mods {
wfItem.SetModifier(mod)
}
}
if len(item.Actions) > 0 {
for t, action := range item.Actions {
wfItem.ActionForType(t, action...)
}
}
if item.Icon != nil {
wfItem.Icon(item.Icon)
}
wfItems[i] = wfItem
}
if len(items) > 0 {
wf.Feedback.Items = wfItems
}
}