aboutsummaryrefslogtreecommitdiffstats
path: root/commands/help.go
blob: 079b2fd5479e8359876f7a41d4875478b77157e6 (plain) (blame)
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
package commands

import (
	"fmt"

	"git.sr.ht/~rjarry/aerc/app"
)

type Help struct {
	Topic string `opt:"topic" action:"ParseTopic" default:"aerc" complete:"CompleteTopic"`
}

var pages = []string{
	"aerc",
	"accounts",
	"binds",
	"config",
	"imap",
	"jmap",
	"notmuch",
	"search",
	"sendmail",
	"smtp",
	"stylesets",
	"templates",
	"tutorial",
	"patch",
	"keys",
}

func init() {
	Register(Help{})
}

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 {
	for _, page := range pages {
		if arg == page {
			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)
}