2019-07-16 22:48:25 +02:00
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-09-20 18:16:29 +02:00
|
|
|
"strings"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2019-07-30 21:10:58 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/commands"
|
2019-07-16 22:48:25 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
2020-05-28 16:32:42 +02:00
|
|
|
"github.com/gdamore/tcell"
|
2019-07-16 22:48:25 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Attach struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Attach{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Attach) Aliases() []string {
|
2019-07-16 22:48:25 +02:00
|
|
|
return []string{"attach"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Attach) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-09-20 18:16:29 +02:00
|
|
|
path := strings.Join(args, " ")
|
2019-07-30 21:10:58 +02:00
|
|
|
return commands.CompletePath(path)
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Attach) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-09-20 18:16:29 +02:00
|
|
|
if len(args) == 1 {
|
2019-07-16 22:48:25 +02:00
|
|
|
return fmt.Errorf("Usage: :attach <path>")
|
|
|
|
}
|
|
|
|
|
2019-09-20 18:16:29 +02:00
|
|
|
path := strings.Join(args[1:], " ")
|
2019-07-16 22:48:25 +02:00
|
|
|
|
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(" " + err.Error())
|
2019-07-16 22:48:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(" " + err.Error())
|
2019-07-16 22:48:25 +02:00
|
|
|
return err
|
|
|
|
} else if pathinfo.IsDir() {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError("Attachment must be a file, not a directory")
|
2019-07-16 22:48:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
composer, _ := aerc.SelectedTab().(*widgets.Composer)
|
|
|
|
composer.AddAttachment(path)
|
|
|
|
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushStatus(fmt.Sprintf("Attached %s", pathinfo.Name()), 10*time.Second).
|
|
|
|
Color(tcell.ColorDefault, tcell.ColorGreen)
|
2019-07-16 22:48:25 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|