|
| 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