aboutsummaryrefslogblamecommitdiffstats
path: root/commands/compose/attach.go
blob: 7501a338f7605ede7746cd37998aff2b0614a3d7 (plain) (tree)
1
2
3
4
5
6
7
8






               
                                           















                                                                      





                                          































                                                                                     
package compose

import (
	"fmt"
	"os"
	"time"

	"git.sr.ht/~sircmpwn/aerc/commands"
	"git.sr.ht/~sircmpwn/aerc/widgets"
	"github.com/gdamore/tcell"
	"github.com/mitchellh/go-homedir"
)

type Attach struct{}

func init() {
	register(Attach{})
}

func (_ Attach) Aliases() []string {
	return []string{"attach"}
}

func (_ Attach) Complete(aerc *widgets.Aerc, args []string) []string {
	path := ""
	if len(args) >= 1 {
		path = args[0]
	}

	return commands.CompletePath(path)
}

func (_ Attach) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) != 2 {
		return fmt.Errorf("Usage: :attach <path>")
	}

	path := args[1]

	path, err := homedir.Expand(path)
	if err != nil {
		aerc.PushError(" " + err.Error())
		return err
	}

	pathinfo, err := os.Stat(path)
	if err != nil {
		aerc.PushError(" " + err.Error())
		return err
	} else if pathinfo.IsDir() {
		aerc.PushError("Attachment must be a file, not a directory")
		return nil
	}

	composer, _ := aerc.SelectedTab().(*widgets.Composer)
	composer.AddAttachment(path)

	aerc.PushStatus(fmt.Sprintf("Attached %s", pathinfo.Name()), 10*time.Second).
		Color(tcell.ColorDefault, tcell.ColorGreen)

	return nil
}