Debian Codegraph @

commands/patch/switch.go

raw
1 package patch
2
3 import (
4 "fmt"
5 "time"
6
7 "git.sr.ht/~rjarry/aerc/app"
8 "git.sr.ht/~rjarry/aerc/commands"
9 "git.sr.ht/~rjarry/aerc/lib/log"
10 "git.sr.ht/~rjarry/aerc/lib/pama"
11 )
12
13 type Switch struct {
14 Project string `opt:"project" complete:"Complete" desc:"Project name."`
15 }
16
17 func init() {
18 register(Switch{})
19 }
20
21 func (Switch) Description() string {
22 return "Switch context to the specified project."
23 }
24
25 func (Switch) Context() commands.CommandContext {
26 return commands.GLOBAL
27 }
28
29 func (Switch) Aliases() []string {
30 return []string{"switch"}
31 }
32
33 func (s Switch) Complete(arg string) []string {
34 m := pama.New()
35 names, err := m.Names()
36 if err != nil {
37 log.Errorf("failed to get completion: %v", err)
38 return nil
39 }
40 cur, err := m.CurrentProject()
41 if err == nil {
42 i := 0
43 for ; i < len(names); i++ {
44 if cur.Name == names[i] {
45 names = append(names[:i], names[i+1:]...)
46 break
47 }
48 }
49 }
50 return commands.FilterList(names, arg, nil)
51 }
52
53 func (s Switch) Execute(_ []string) error {
54 name := s.Project
55 err := pama.New().SwitchProject(name)
56 if err != nil {
57 return err
58 }
59 app.PushStatus(fmt.Sprintf("Project switched to '%s'", name),
60 10*time.Second)
61 return nil
62 }