Skip to content

Commit de0754b

Browse files
committed
- Settings > Programs > Actions
1 parent 5b02704 commit de0754b

26 files changed

Lines changed: 1200 additions & 10 deletions

‎internal/app/action_settings.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package app
2+
3+
import "github.com/dannyben/commando/internal/settings"
4+
5+
// The third row is a projection of the draft, not a second copy of its values.
6+
// Its empty placeholder preserves the dialog footprint for Both actions.
7+
func (s *settingsSession) projectActionFields() {
8+
if s.collection != settings.ActionCollection {
9+
return
10+
}
11+
key := ""
12+
switch s.draft.Get(settings.CatalogueDefault, settings.ActionCollection+".target").Text {
13+
case settings.ActionFiles:
14+
key = settings.ActionCollection + ".ext"
15+
case settings.ActionFolders:
16+
key = settings.ActionCollection + ".mode"
17+
}
18+
s.fields[2] = settings.Field{}
19+
for _, field := range settings.CatalogueDefault.UI.Fields {
20+
if field.Key == key {
21+
s.fields[2] = field
22+
break
23+
}
24+
}
25+
}

‎internal/app/commands.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func (v *view) commandCatalogue() []command {
4242
c.mask = c.presentation(v)
4343
}
4444
catalogue = append(catalogue, v.resolveCommandBindings(c))
45+
if c.group == "Programs" && (i == len(commands)-1 || commands[i+1].group != "Programs") {
46+
catalogue = append(catalogue, v.customActionCommands()...)
47+
}
4548
if c.group == "Panel" && (i == len(commands)-1 || commands[i+1].group != "Panel") {
4649
for index := range v.panels {
4750
catalogue = append(catalogue, v.resolveCommandBindings(panelFocusCommand(index+1)))

‎internal/app/custom_actions.go‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package app
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/dannyben/commando/internal/browser"
10+
"github.com/dannyben/commando/internal/feedback"
11+
"github.com/dannyben/commando/internal/settings"
12+
)
13+
14+
const customActionCommandPrefix = "programs.actions.run."
15+
16+
type customActionTarget struct {
17+
path string
18+
directory bool
19+
}
20+
21+
type customActionLaunch struct {
22+
arguments []string
23+
directory string
24+
}
25+
26+
type customActionRequest struct {
27+
program string
28+
terminal bool
29+
launches []customActionLaunch
30+
}
31+
32+
func customActionIdentity(commandID string) (string, bool) {
33+
id, ok := strings.CutPrefix(commandID, customActionCommandPrefix)
34+
return id, ok && settings.ValidIdentity(id)
35+
}
36+
37+
func (v *view) customActionCommands() []command {
38+
var catalogue []command
39+
for _, rule := range v.settingsValues.ActionRules() {
40+
if !settings.ValidIdentity(rule.ID) {
41+
continue
42+
}
43+
id := rule.ID
44+
catalogue = append(catalogue, v.resolveCommandBindings(command{
45+
id: customActionCommandPrefix + id, group: "Programs › Actions", name: rule.Name,
46+
available: func(v *view) bool {
47+
rule, ok := v.customActionRule(id)
48+
return ok && terminalReady(v) && len(v.panels[v.active].customActionTargets(rule)) > 0
49+
},
50+
execute: func(v *view) { v.runCustomAction(id) },
51+
}))
52+
}
53+
return catalogue
54+
}
55+
56+
func (v *view) customActionRule(id string) (settings.ActionRule, bool) {
57+
for _, rule := range v.settingsValues.ActionRules() {
58+
if rule.ID == id {
59+
return rule, true
60+
}
61+
}
62+
return settings.ActionRule{}, false
63+
}
64+
65+
// Marks take precedence over the current row, using only this panel. Folder
66+
// actions ignore files; file actions ignore folders. Both accepts every kind.
67+
func (p *panel) customActionTargets(rule settings.ActionRule) []customActionTarget {
68+
if rule.Target == settings.ActionFolders && rule.Mode == settings.ActionHere {
69+
return []customActionTarget{{p.path, true}}
70+
}
71+
var targets []customActionTarget
72+
for row, item := range p.items {
73+
if len(p.marks) > 0 && !p.marks[row] || len(p.marks) == 0 && row != p.selected {
74+
continue
75+
}
76+
file := item.file
77+
if file.Parent || rule.Target == settings.ActionFolders && !file.Directory || rule.Target == settings.ActionFiles && file.Directory {
78+
continue
79+
}
80+
if file.Path != "" || !rule.Matches(file.Name, file.Directory) {
81+
return nil
82+
}
83+
targets = append(targets, customActionTarget{filepath.Join(p.path, file.Name), file.Directory})
84+
}
85+
if len(targets) == 0 && rule.Target == settings.ActionFolders && rule.Mode == settings.ActionAuto {
86+
return []customActionTarget{{p.path, true}}
87+
}
88+
return targets
89+
}
90+
91+
func (v *view) runCustomAction(id string) {
92+
rule, ok := v.customActionRule(id)
93+
if !ok || !v.interaction.idle() || !terminalReady(v) {
94+
return
95+
}
96+
p := &v.panels[v.active]
97+
targets := p.customActionTargets(rule)
98+
if len(targets) == 0 {
99+
return
100+
}
101+
paths := make([]string, len(targets))
102+
for i, target := range targets {
103+
paths[i] = target.path
104+
}
105+
arguments, err := settings.ActionArguments(rule.Arguments, paths)
106+
if err != nil {
107+
v.showFeedback(v.active, feedback.Error, "Invalid action arguments")
108+
return
109+
}
110+
request := &customActionRequest{program: rule.Program, terminal: rule.Terminal}
111+
for i, args := range arguments {
112+
directory := p.path
113+
// One-folder launches start inside that folder. Batch launches use
114+
// the panel's folder when they receive more than one target.
115+
if len(targets) == 1 && targets[0].directory {
116+
directory = targets[0].path
117+
} else if strings.Contains(rule.Arguments, settings.EntryToken) && targets[i].directory {
118+
directory = targets[i].path
119+
}
120+
request.launches = append(request.launches, customActionLaunch{args, directory})
121+
}
122+
if v.loader.submit(loadRequest{panel: v.active, path: p.path, customAction: request}) {
123+
p.loading = true
124+
}
125+
}
126+
127+
func (v *view) acceptCustomAction(result loadResult) {
128+
v.panels[result.request.panel].loading = false
129+
if result.err == nil {
130+
return
131+
}
132+
message := "Cannot start action"
133+
switch {
134+
case result.actionsStarted > 0:
135+
message = "Some actions failed to start"
136+
case errors.Is(result.err, browser.ErrTerminalUnavailable):
137+
message = "Terminal requires Windows"
138+
case errors.Is(result.err, os.ErrPermission):
139+
message = "Action access denied"
140+
case errors.Is(result.err, os.ErrNotExist):
141+
message = "Action program not found"
142+
}
143+
v.showFeedback(result.request.panel, feedback.Error, message)
144+
}

0 commit comments

Comments
 (0)