Debian Codegraph @

commands/patch/list.go

raw
1 package patch
2
3 import (
4 "bufio"
5 "fmt"
6 "io"
7 "os/exec"
8 "time"
9
10 "git.sr.ht/~rjarry/aerc/app"
11 "git.sr.ht/~rjarry/aerc/commands"
12 "git.sr.ht/~rjarry/aerc/config"
13 "git.sr.ht/~rjarry/aerc/lib/pama"
14 "git.sr.ht/~rjarry/aerc/lib/pama/models"
15 "git.sr.ht/~rjarry/aerc/lib/ui"
16 "git.sr.ht/~rjarry/go-opt/v2"
17 "go.rockorager.dev/vaxis"
18 )
19
20 type List struct {
21 All bool `opt:"-a" desc:"List all projects."`
22 }
23
24 func init() {
25 register(List{})
26 }
27
28 func (List) Description() string {
29 return "List the current project with the tracked patch sets."
30 }
31
32 func (List) Context() commands.CommandContext {
33 return commands.GLOBAL
34 }
35
36 func (List) Aliases() []string {
37 return []string{"list", "ls"}
38 }
39
40 func (l List) Execute(args []string) error {
41 m := pama.New()
42 current, err := m.CurrentProject()
43 if err != nil {
44 return err
45 }
46
47 projects := []models.Project{current}
48 if l.All {
49 projects, err = m.Projects("")
50 if err != nil {
51 return err
52 }
53 }
54
55 app.PushStatus(fmt.Sprintf("Current project: %s", current.Name), 30*time.Second)
56
57 createWidget := func(r io.Reader) (ui.DrawableInteractive, error) {
58 pagerCmd, err := app.CmdFallbackSearch(config.PagerCmds(), true)
59 if err != nil {
60 return nil, err
61 }
62
63 cmd := opt.SplitArgs(pagerCmd)
64 pager := exec.Command(cmd[0], cmd[1:]...)
65 pager.Stdin = r
66
67 term, err := app.NewTerminal(pager)
68 if err != nil {
69 return nil, err
70 }
71 start := time.Now()
72 term.OnClose = func(err error) {
73 if time.Since(start) > 250*time.Millisecond {
74 app.CloseDialog()
75 return
76 }
77 term.OnEvent = func(_ vaxis.Event) bool {
78 app.CloseDialog()
79 return true
80 }
81 }
82 return term, nil
83 }
84
85 viewer, err := createWidget(m.NewReader(projects))
86 if err != nil {
87 viewer = app.NewListBox(
88 "Press <Esc> or <Enter> to close. "+
89 "Start typing to filter.",
90 numerify(m.NewReader(projects)), app.SelectedAccountUiConfig(),
91 func(_ string) { app.CloseDialog() },
92 )
93 }
94
95 app.AddDialog(app.DefaultDialog(
96 ui.NewBox(viewer, "Patch Management", "",
97 app.SelectedAccountUiConfig(),
98 ),
99 ))
100
101 return nil
102 }
103
104 func numerify(r io.Reader) []string {
105 var lines []string
106 nr := 1
107 scanner := bufio.NewScanner(r)
108 for scanner.Scan() {
109 s := scanner.Text()
110 lines = append(lines, fmt.Sprintf("%3d %s", nr, s))
111 nr++
112 }
113 return lines
114 }