-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathactions.go
162 lines (141 loc) · 3.34 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2020 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
)
var (
tabActions = map[string]tabAction{}
urlActions = map[string]urlAction{}
)
type tabAction interface {
Name() string
Icon() *aw.Icon
Run(tabID int) error
}
type urlAction interface {
Name() string
Icon() *aw.Icon
Run(URL string) error
}
func init() {
for _, a := range []tabAction{
tAction{name: "Activate Tab", action: "activate", icon: iconTab},
tAction{name: "Close Tabs to Left", action: "close-left", icon: iconTab},
tAction{name: "Close Tabs to Right", action: "close-right", icon: iconTab},
tAction{name: "Close Other Tabs", action: "close-other", icon: iconTab},
} {
tabActions[a.Name()] = a
}
a := openIncognito{}
urlActions[a.Name()] = a
}
func loadURLActions() error {
var (
scripts = map[string]string{}
infos []os.FileInfo
err error
)
for _, dir := range scriptDirs {
if infos, err = ioutil.ReadDir(dir); err != nil {
return err
}
for _, fi := range infos {
if fi.IsDir() {
continue
}
var (
path = filepath.Join(dir, fi.Name())
ext = strings.ToLower(filepath.Ext(fi.Name()))
name = fi.Name()[0 : len(fi.Name())-len(ext)]
_, known = util.DefaultInterpreters[ext]
exe = fi.Mode()&0111 != 0
)
if exe || known {
scripts[name] = path
}
if imageExts[ext] {
scriptIcons[name] = &aw.Icon{Value: path}
}
}
}
for name, path := range scripts {
log.Printf("loaded URL action %q from %q", name, util.PrettyPath(path))
a := uAction{
name: name,
icon: actionIcon(name, iconURL),
script: path,
}
urlActions[name] = a
}
return nil
}
type tAction struct {
name string
icon *aw.Icon
action string
}
func (a tAction) Name() string { return a.name }
func (a tAction) Icon() *aw.Icon { return a.icon }
func (a tAction) Run(tabID int) error {
c := mustClient()
switch a.action {
case "activate":
_, err := util.RunAS(fmt.Sprintf(`tell application "%s" to activate`, c.appName))
if err != nil {
return err
}
return c.ActivateTab(tabID)
case "close-left":
return c.CloseTabsLeft(tabID)
case "close-right":
return c.CloseTabsRight(tabID)
case "close-other":
return c.CloseTabsOther(tabID)
default:
return fmt.Errorf("unknown action %q", action)
}
}
type uAction struct {
name string
icon *aw.Icon
script string
}
func (a uAction) Name() string { return a.name }
func (a uAction) Icon() *aw.Icon { return a.icon }
func (a uAction) Run(URL string) error {
c, err := newClient()
if err == nil {
os.Setenv("BROWSER", c.appName)
}
data, err := util.Run(a.script, URL)
if err != nil {
return err
}
s := string(data)
if s != "" {
log.Print(util.Pad(fmt.Sprintf(" output: %q ", a.name), "-", 50))
log.Print(s)
}
return nil
}
// URL action to open a URL in a new incognito window
type openIncognito struct{}
func (a openIncognito) Name() string { return "Open in Incognito Window" }
func (a openIncognito) Icon() *aw.Icon { return iconIncognito }
func (a openIncognito) Run(URL string) error {
mustClient().OpenIncognito(URL)
return nil
}
var (
_ tabAction = (*tAction)(nil)
_ urlAction = (*uAction)(nil)
_ urlAction = openIncognito{}
)