diff options
author | Koni Marti <koni.marti@gmail.com> | 2023-11-24 16:03:03 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-12-30 15:42:09 +0100 |
commit | cf47763e5582563f712b4a40a9b299378aba9003 (patch) | |
tree | af83b863a644a90a69eef891a4ce06023224a213 /commands | |
parent | fdd9f7991aa50bd99d21c178a2816fc075eead6b (diff) | |
download | aerc-cf47763e5582563f712b4a40a9b299378aba9003.tar.gz |
patch/list: add list sub-cmd
Implement the :patch list command. List the the current project and add
a flag to list all saved projects. Use the pager to display the data and
extract the pager commands and move them into the config package.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/patch/list.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/commands/patch/list.go b/commands/patch/list.go new file mode 100644 index 00000000..6dcc06e8 --- /dev/null +++ b/commands/patch/list.go @@ -0,0 +1,113 @@ +package patch + +import ( + "bufio" + "fmt" + "io" + "os/exec" + "time" + + "git.sr.ht/~rjarry/aerc/app" + "git.sr.ht/~rjarry/aerc/config" + "git.sr.ht/~rjarry/aerc/lib/pama" + "git.sr.ht/~rjarry/aerc/lib/pama/models" + "git.sr.ht/~rjarry/aerc/lib/ui" + "git.sr.ht/~rjarry/go-opt" + "github.com/gdamore/tcell/v2" +) + +type List struct { + All bool `opt:"-a"` +} + +func init() { + register(List{}) +} + +func (List) Aliases() []string { + return []string{"list", "ls"} +} + +func (l List) Execute(args []string) error { + m := pama.New() + current, err := m.CurrentProject() + if err != nil { + return err + } + + projects := []models.Project{current} + if l.All { + projects, err = m.Projects("") + if err != nil { + return err + } + } + + app.PushStatus(fmt.Sprintf("Current project: %s", current.Name), 30*time.Second) + + createWidget := func(r io.Reader) (ui.DrawableInteractive, error) { + pagerCmd, err := app.CmdFallbackSearch(config.PagerCmds(), true) + if err != nil { + return nil, err + } + + cmd := opt.SplitArgs(pagerCmd) + pager := exec.Command(cmd[0], cmd[1:]...) + pager.Stdin = r + + term, err := app.NewTerminal(pager) + if err != nil { + return nil, err + } + start := time.Now() + term.OnClose = func(err error) { + if time.Since(start) > 250*time.Millisecond { + app.CloseDialog() + return + } + term.OnEvent = func(_ tcell.Event) bool { + app.CloseDialog() + return true + } + } + return term, nil + } + + viewer, err := createWidget(m.NewReader(projects)) + if err != nil { + viewer = app.NewListBox( + "Press <Esc> or <Enter> to close. "+ + "Start typing to filter.", + numerify(m.NewReader(projects)), app.SelectedAccountUiConfig(), + func(_ string) { app.CloseDialog() }, + ) + } + + app.AddDialog(app.NewDialog( + ui.NewBox(viewer, "Patch Management", "", + app.SelectedAccountUiConfig(), + ), + // start pos on screen + func(h int) int { + return h / 8 + }, + // dialog height + func(h int) int { + return h - 2*h/8 + }, + )) + + return nil +} + +func numerify(r io.Reader) []string { + var lines []string + nr := 1 + scanner := bufio.NewScanner(r) + for scanner.Scan() { + s := scanner.Text() + lines = append(lines, fmt.Sprintf("%3d %s", nr, s)) + nr++ + } + return lines +} |