| 1 |
package commands |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"os/exec" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/app" |
| 11 |
"git.sr.ht/~rjarry/aerc/config" |
| 12 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 14 |
"git.sr.ht/~rjarry/aerc/models" |
| 15 |
"git.sr.ht/~rjarry/go-opt/v2" |
| 16 |
) |
| 17 |
|
| 18 |
type Menu struct { |
| 19 |
ErrExit bool `opt:"-e" desc:"Stop executing commands on the first error."` |
| 20 |
Background bool `opt:"-b" desc:"Do NOT spawn the popover dialog."` |
| 21 |
Accounts bool `opt:"-a" desc:"Feed command with account names."` |
| 22 |
Directories bool `opt:"-d" desc:"Feed command with folder names."` |
| 23 |
Links bool `opt:"-l" desc:"Feed command with links extracted from message."` |
| 24 |
Command string `opt:"-c" desc:"Override [general].default-menu-cmd."` |
| 25 |
Title string `opt:"-t" desc:"Override the default menu title."` |
| 26 |
Xargs string `opt:"..." complete:"CompleteXargs" desc:"Command name."` |
| 27 |
} |
| 28 |
|
| 29 |
func init() { |
| 30 |
Register(Menu{}) |
| 31 |
} |
| 32 |
|
| 33 |
func (Menu) Description() string { |
| 34 |
return "Open a popover dialog." |
| 35 |
} |
| 36 |
|
| 37 |
func (Menu) Context() CommandContext { |
| 38 |
return GLOBAL |
| 39 |
} |
| 40 |
|
| 41 |
func (Menu) Aliases() []string { |
| 42 |
return []string{"menu"} |
| 43 |
} |
| 44 |
|
| 45 |
func (*Menu) CompleteXargs(arg string) []string { |
| 46 |
return FilterList(ActiveCommandNames(), arg, nil) |
| 47 |
} |
| 48 |
|
| 49 |
func (m Menu) Execute([]string) error { |
| 50 |
if m.Command == "" { |
| 51 |
m.Command = config.General().DefaultMenuCmd |
| 52 |
} |
| 53 |
useFallback := m.useFallback() |
| 54 |
if m.Background && useFallback { |
| 55 |
return errors.New("Either -c <command> or " + |
| 56 |
"default-menu-cmd is required to run " + |
| 57 |
"in the background.") |
| 58 |
} |
| 59 |
if _, _, err := ResolveCommand(m.Xargs, nil, nil); err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
lines, err := m.feedLines() |
| 64 |
if err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
|
| 68 |
title := m.Title |
| 69 |
if len(title) == 0 { |
| 70 |
title = " :" + strings.TrimLeft(m.Xargs, ": \t") + " ... " |
| 71 |
} |
| 72 |
|
| 73 |
if useFallback { |
| 74 |
return m.fallback(title, lines) |
| 75 |
} |
| 76 |
|
| 77 |
pick, err := os.CreateTemp(config.General().TempDir, "aerc-menu-*") |
| 78 |
if err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
var proc *exec.Cmd |
| 83 |
if strings.Contains(m.Command, "%f") { |
| 84 |
proc = exec.Command("sh", "-c", |
| 85 |
strings.ReplaceAll(m.Command, "%f", opt.QuoteArg(pick.Name()))) |
| 86 |
} else { |
| 87 |
proc = exec.Command("sh", "-c", m.Command+" >&3") |
| 88 |
proc.ExtraFiles = append(proc.ExtraFiles, pick) |
| 89 |
} |
| 90 |
if len(lines) > 0 { |
| 91 |
proc.Stdin = strings.NewReader(strings.Join(lines, "\n")) |
| 92 |
} |
| 93 |
|
| 94 |
xargs := func(err error) { |
| 95 |
var buf []byte |
| 96 |
if err == nil { |
| 97 |
_, err = pick.Seek(0, io.SeekStart) |
| 98 |
} |
| 99 |
if err == nil { |
| 100 |
buf, err = io.ReadAll(pick) |
| 101 |
} |
| 102 |
pick.Close() |
| 103 |
os.Remove(pick.Name()) |
| 104 |
if err != nil { |
| 105 |
app.PushError("command failed: " + err.Error()) |
| 106 |
return |
| 107 |
} |
| 108 |
if len(buf) == 0 { |
| 109 |
return |
| 110 |
} |
| 111 |
m.runCmd(string(buf)) |
| 112 |
} |
| 113 |
|
| 114 |
if m.Background { |
| 115 |
go func() { |
| 116 |
defer log.PanicHandler() |
| 117 |
xargs(proc.Run()) |
| 118 |
}() |
| 119 |
} else { |
| 120 |
term, err := app.NewTerminal(proc) |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
term.Focus(true) |
| 125 |
term.OnClose = func(err error) { |
| 126 |
app.CloseDialog() |
| 127 |
xargs(err) |
| 128 |
} |
| 129 |
|
| 130 |
widget := ui.NewBox(term, title, "", app.SelectedAccountUiConfig()) |
| 131 |
app.AddDialog(app.DefaultDialog(widget)) |
| 132 |
} |
| 133 |
|
| 134 |
return nil |
| 135 |
} |
| 136 |
|
| 137 |
func (m Menu) useFallback() bool { |
| 138 |
if m.Command == "" || m.Command == "-" { |
| 139 |
return true |
| 140 |
} |
| 141 |
cmd, _, _ := strings.Cut(m.Command, " ") |
| 142 |
_, err := exec.LookPath(cmd) |
| 143 |
if err != nil { |
| 144 |
warnMsg := "command '" + cmd + "' not found in PATH, " + |
| 145 |
"falling back on aerc's picker." |
| 146 |
log.Warnf(warnMsg) |
| 147 |
app.PushWarning(warnMsg) |
| 148 |
return true |
| 149 |
} |
| 150 |
return false |
| 151 |
} |
| 152 |
|
| 153 |
func (m Menu) runCmd(buffer string) { |
| 154 |
var ( |
| 155 |
cmd Command |
| 156 |
cmdline string |
| 157 |
err error |
| 158 |
) |
| 159 |
|
| 160 |
for _, line := range strings.Split(buffer, "\n") { |
| 161 |
line = strings.TrimSpace(line) |
| 162 |
if line == "" { |
| 163 |
continue |
| 164 |
} |
| 165 |
cmdline = m.Xargs + " " + line |
| 166 |
cmdline, cmd, err = ResolveCommand(cmdline, nil, nil) |
| 167 |
if err == nil { |
| 168 |
err = ExecuteCommand(cmd, cmdline) |
| 169 |
} |
| 170 |
if err != nil { |
| 171 |
app.PushError(m.Xargs + ": " + err.Error()) |
| 172 |
if m.ErrExit { |
| 173 |
return |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func (m Menu) fallback(title string, lines []string) error { |
| 180 |
listBox := app.NewListBox( |
| 181 |
title, lines, app.SelectedAccountUiConfig(), |
| 182 |
func(line string) { |
| 183 |
app.CloseDialog() |
| 184 |
if line == "" { |
| 185 |
return |
| 186 |
} |
| 187 |
m.runCmd(line) |
| 188 |
}) |
| 189 |
listBox.SetTextFilter(func(list []string, term string) []string { |
| 190 |
return FilterList(list, term, func(s string) string { return s }) |
| 191 |
}) |
| 192 |
widget := ui.NewBox(listBox, "", "", app.SelectedAccountUiConfig()) |
| 193 |
app.AddDialog(app.DefaultDialog(widget)) |
| 194 |
return nil |
| 195 |
} |
| 196 |
|
| 197 |
func (m Menu) feedLines() ([]string, error) { |
| 198 |
var lines []string |
| 199 |
|
| 200 |
switch { |
| 201 |
case m.Accounts && m.Directories: |
| 202 |
for _, a := range app.AccountNames() { |
| 203 |
account, _ := app.Account(a) |
| 204 |
a = opt.QuoteArg(a) |
| 205 |
for _, d := range account.Directories().List() { |
| 206 |
dir := account.Directories().Directory(d) |
| 207 |
if dir != nil && dir.Role != models.QueryRole { |
| 208 |
d = opt.QuoteArg(d) |
| 209 |
} |
| 210 |
lines = append(lines, a+" "+d) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
case m.Accounts: |
| 215 |
for _, account := range app.AccountNames() { |
| 216 |
lines = append(lines, opt.QuoteArg(account)) |
| 217 |
} |
| 218 |
|
| 219 |
case m.Directories: |
| 220 |
account := app.SelectedAccount() |
| 221 |
if account == nil { |
| 222 |
return nil, errors.New("No account selected.") |
| 223 |
} |
| 224 |
for _, d := range account.Directories().List() { |
| 225 |
dir := account.Directories().Directory(d) |
| 226 |
if dir != nil && dir.Role != models.QueryRole { |
| 227 |
d = opt.QuoteArg(d) |
| 228 |
} |
| 229 |
lines = append(lines, d) |
| 230 |
} |
| 231 |
|
| 232 |
case m.Links: |
| 233 |
switch tab := app.SelectedTabContent().(type) { |
| 234 |
case *app.MessageViewer: |
| 235 |
if tab != nil { |
| 236 |
if p := tab.SelectedMessagePart(); p != nil { |
| 237 |
lines = append(lines, p.Links...) |
| 238 |
} |
| 239 |
} |
| 240 |
default: |
| 241 |
return nil, errors.New("Cannot extract links for current tab.") |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
return lines, nil |
| 246 |
} |