Files
aerc-fork-mirror/commands/help.go
Moritz Poldrack fc5b6896ff chore: switch to using stdlib helper functions
Go has evolved significantly over the years and has introduced some
handy helper functions that make the code easier to read.

Use helper functions like slices.Contains, map.Copy, and
strings.CutPrefix, when appropriate.

Signed-off-by: Moritz Poldrack <git@moritz.sh>
Acked-by: Robin Jarry <robin@jarry.cc>
2025-08-04 12:47:05 +02:00

81 lines
1.4 KiB
Go

package commands
import (
"fmt"
"slices"
"git.sr.ht/~rjarry/aerc/app"
)
type Help struct {
Topic string `opt:"topic" action:"ParseTopic" default:"aerc" complete:"CompleteTopic" desc:"Help topic."`
}
var pages = []string{
"aerc",
"accounts",
"binds",
"config",
"imap",
"jmap",
"notmuch",
"search",
"sendmail",
"smtp",
"stylesets",
"templates",
"tutorial",
"patch",
"keys",
}
func init() {
Register(Help{})
}
func (Help) Description() string {
return "Display one of aerc's man pages in the embedded terminal."
}
func (Help) Context() CommandContext {
return GLOBAL
}
func (Help) Aliases() []string {
return []string{"help", "man"}
}
func (*Help) CompleteTopic(arg string) []string {
return FilterList(pages, arg, nil)
}
func (h *Help) ParseTopic(arg string) error {
if slices.Contains(pages, arg) {
if arg != "aerc" {
arg = "aerc-" + arg
}
h.Topic = arg
return nil
}
return fmt.Errorf("unknown topic %q", arg)
}
func (h Help) Execute(args []string) error {
if h.Topic == "aerc-keys" {
app.AddDialog(app.DefaultDialog(
app.NewListBox(
"Bindings: Press <Esc> or <Enter> to close. "+
"Start typing to filter bindings.",
app.HumanReadableBindings(),
app.SelectedAccountUiConfig(),
func(_ string) {
app.CloseDialog()
},
),
))
return nil
}
term := Term{Cmd: []string{"man", h.Topic}}
return term.Execute(args)
}