summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-09-20 17:16:29 +0100
committerDrew DeVault <sir@cmpwn.com>2019-09-20 15:06:34 -0400
commit39307a6fa7e96641b822ed0a9acb75021dcf7fe9 (patch)
tree683aec09eb86e2077148a53429681aa39776449b
parent3ec9fd216d9e3b38d1d5abb5fba24199185f7054 (diff)
downloadaerc-39307a6fa7e96641b822ed0a9acb75021dcf7fe9.tar.gz
Make commands join args with spaces
This patch ensures the following commands join their arguments with
spaces to make it easier to interact with:

- cf
- mkdir
- cd
- attach
- detach
- ct
- copy
- move
- save
-rw-r--r--commands/account/cf.go10
-rw-r--r--commands/account/mkdir.go5
-rw-r--r--commands/cd.go16
-rw-r--r--commands/compose/attach.go11
-rw-r--r--commands/compose/detach.go10
-rw-r--r--commands/ct.go16
-rw-r--r--commands/msg/copy.go28
-rw-r--r--commands/msg/move.go12
-rw-r--r--commands/msgview/save.go18
9 files changed, 60 insertions, 66 deletions
diff --git a/commands/account/cf.go b/commands/account/cf.go
index cbef308..65b8810 100644
--- a/commands/account/cf.go
+++ b/commands/account/cf.go
@@ -28,7 +28,7 @@ func (ChangeFolder) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (ChangeFolder) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) < 2 {
+	if len(args) == 1 {
 		return errors.New("Usage: cf <folder>")
 	}
 	acct := aerc.SelectedAccount()
@@ -36,17 +36,15 @@ func (ChangeFolder) Execute(aerc *widgets.Aerc, args []string) error {
 		return errors.New("No account selected")
 	}
 	previous := acct.Directories().Selected()
-	if args[1] == "-" {
+	joinedArgs := strings.Join(args[1:], " ")
+	if joinedArgs == "-" {
 		if dir, ok := history[acct.Name()]; ok {
 			acct.Directories().Select(dir)
 		} else {
 			return errors.New("No previous folder to return to")
 		}
 	} else {
-		if len(args) > 2 {
-			args[1] = strings.Join(args[1:], " ")
-		}
-		acct.Directories().Select(args[1])
+		acct.Directories().Select(joinedArgs)
 	}
 	history[acct.Name()] = previous
 
diff --git a/commands/account/mkdir.go b/commands/account/mkdir.go
index d42928e..bb7e38a 100644
--- a/commands/account/mkdir.go
+++ b/commands/account/mkdir.go
@@ -2,6 +2,7 @@ package account
 
 import (
 	"errors"
+	"strings"
 	"time"
 
 	"github.com/gdamore/tcell"
@@ -25,14 +26,14 @@ func (MakeDir) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (MakeDir) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) != 2 {
+	if len(args) == 0 {
 		return errors.New("Usage: :mkdir <name>")
 	}
 	acct := aerc.SelectedAccount()
 	if acct == nil {
 		return errors.New("No account selected")
 	}
-	name := args[1]
+	name := strings.Join(args[1:], " ")
 	acct.Worker().PostAction(&types.CreateDirectory{
 		Directory: name,
 	}, func(msg types.WorkerMessage) {
diff --git a/commands/cd.go b/commands/cd.go
index 1d033e4..fa487e7 100644
--- a/commands/cd.go
+++ b/commands/cd.go
@@ -24,11 +24,7 @@ func (ChangeDirectory) Aliases() []string {
 }
 
 func (ChangeDirectory) Complete(aerc *widgets.Aerc, args []string) []string {
-	path := ""
-	if len(args) >= 1 {
-		path = args[0]
-	}
-
+	path := strings.Join(args, " ")
 	completions := CompletePath(path)
 
 	var dirs []string
@@ -43,24 +39,22 @@ func (ChangeDirectory) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (ChangeDirectory) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) < 1 || len(args) > 2 {
+	if len(args) < 1 {
 		return errors.New("Usage: cd [directory]")
 	}
 	cwd, err := os.Getwd()
 	if err != nil {
 		return err
 	}
-	var target string
-	if len(args) == 1 {
+	target := strings.Join(args[1:], " ")
+	if target == "" {
 		target = "~"
-	} else if args[1] == "-" {
+	} else if target == "-" {
 		if previousDir == "" {
 			return errors.New("No previous folder to return to")
 		} else {
 			target = previousDir
 		}
-	} else {
-		target = args[1]
 	}
 	target, err = homedir.Expand(target)
 	if err != nil {
diff --git a/commands/compose/attach.go b/commands/compose/attach.go
index 969d12e..2b633dc 100644
--- a/commands/compose/attach.go
+++ b/commands/compose/attach.go
@@ -3,6 +3,7 @@ package compose
 import (
 	"fmt"
 	"os"
+	"strings"
 	"time"
 
 	"git.sr.ht/~sircmpwn/aerc/commands"
@@ -22,20 +23,16 @@ func (Attach) Aliases() []string {
 }
 
 func (Attach) Complete(aerc *widgets.Aerc, args []string) []string {
-	path := ""
-	if len(args) >= 1 {
-		path = args[0]
-	}
-
+	path := strings.Join(args, " ")
 	return commands.CompletePath(path)
 }
 
 func (Attach) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) != 2 {
+	if len(args) == 1 {
 		return fmt.Errorf("Usage: :attach <path>")
 	}
 
-	path := args[1]
+	path := strings.Join(args[1:], " ")
 
 	path, err := homedir.Expand(path)
 	if err != nil {
diff --git a/commands/compose/detach.go b/commands/compose/detach.go
index dc70ff9..e8b07ed 100644
--- a/commands/compose/detach.go
+++ b/commands/compose/detach.go
@@ -2,6 +2,7 @@ package compose
 
 import (
 	"fmt"
+	"strings"
 	"time"
 
 	"git.sr.ht/~sircmpwn/aerc/widgets"
@@ -20,7 +21,6 @@ func (Detach) Aliases() []string {
 
 func (Detach) Complete(aerc *widgets.Aerc, args []string) []string {
 	composer, _ := aerc.SelectedTab().(*widgets.Composer)
-
 	return composer.GetAttachments()
 }
 
@@ -28,12 +28,8 @@ func (Detach) Execute(aerc *widgets.Aerc, args []string) error {
 	var path string
 	composer, _ := aerc.SelectedTab().(*widgets.Composer)
 
-	if len(args) > 2 {
-		return fmt.Errorf("Usage: :detach [path]")
-	}
-
-	if len(args) == 2 {
-		path = args[1]
+	if len(args) > 1 {
+		path = strings.Join(args[1:], " ")
 	} else {
 		// if no attachment is specified, delete the first in the list
 		atts := composer.GetAttachments()
diff --git a/commands/ct.go b/commands/ct.go
index 3cae0bd..8b6e8a8 100644
--- a/commands/ct.go
+++ b/commands/ct.go
@@ -23,9 +23,10 @@ func (ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
 	if len(args) == 0 {
 		return aerc.TabNames()
 	}
+	joinedArgs := strings.Join(args, " ")
 	out := make([]string, 0)
 	for _, tab := range aerc.TabNames() {
-		if strings.HasPrefix(tab, args[0]) {
+		if strings.HasPrefix(tab, joinedArgs) {
 			out = append(out, tab)
 		}
 	}
@@ -33,22 +34,23 @@ func (ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
-	if len(args) != 2 {
+	if len(args) == 1 {
 		return fmt.Errorf("Usage: %s <tab>", args[0])
 	}
-	if args[1] == "-" {
+	joinedArgs := strings.Join(args[1:], " ")
+	if joinedArgs == "-" {
 		ok := aerc.SelectPreviousTab()
 		if !ok {
 			return errors.New("No previous tab to return to")
 		}
 	} else {
-		n, err := strconv.Atoi(args[1])
+		n, err := strconv.Atoi(joinedArgs)
 		if err == nil {
-			if strings.HasPrefix(args[1], "+") {
+			if strings.HasPrefix(joinedArgs, "+") {
 				for ; n > 0; n-- {
 					aerc.NextTab()
 				}
-			} else if strings.HasPrefix(args[1], "-") {
+			} else if strings.HasPrefix(joinedArgs, "-") {
 				for ; n < 0; n++ {
 					aerc.PrevTab()
 				}
@@ -60,7 +62,7 @@ func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
 				}
 			}
 		} else {
-			ok := aerc.SelectTab(args[1])
+			ok := aerc.SelectTab(joinedArgs)
 			if !ok {
 				return errors.New("No tab with that name")
 			}
diff --git a/commands/msg/copy.go b/commands/msg/copy.go
index d6b78d3..48b296c 100644
--- a/commands/msg/copy.go
+++ b/commands/msg/copy.go
@@ -2,6 +2,7 @@ package msg
 
 import (
 	"errors"
+	"strings"
 	"time"
 
 	"git.sr.ht/~sircmpwn/getopt"
@@ -27,13 +28,13 @@ func (Copy) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (Copy) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) == 1 {
+		return errors.New("Usage: cp [-p] <folder>")
+	}
 	opts, optind, err := getopt.Getopts(args, "p")
 	if err != nil {
 		return err
 	}
-	if optind != len(args)-1 {
-		return errors.New("Usage: cp [-p] <folder>")
-	}
 	var (
 		createParents bool
 	)
@@ -53,16 +54,17 @@ func (Copy) Execute(aerc *widgets.Aerc, args []string) error {
 	if err != nil {
 		return err
 	}
-	store.Copy([]uint32{msg.Uid}, args[optind], createParents, func(
-		msg types.WorkerMessage) {
+	store.Copy([]uint32{msg.Uid}, strings.Join(args[optind:], " "),
+		createParents, func(
+			msg types.WorkerMessage) {
 
-		switch msg := msg.(type) {
-		case *types.Done:
-			aerc.PushStatus("Messages copied.", 10*time.Second)
-		case *types.Error:
-			aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
-				Color(tcell.ColorDefault, tcell.ColorRed)
-		}
-	})
+			switch msg := msg.(type) {
+			case *types.Done:
+				aerc.PushStatus("Messages copied.", 10*time.Second)
+			case *types.Error:
+				aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
+					Color(tcell.ColorDefault, tcell.ColorRed)
+			}
+		})
 	return nil
 }
diff --git a/commands/msg/move.go b/commands/msg/move.go
index 2f8c61e..d7e1ea9 100644
--- a/commands/msg/move.go
+++ b/commands/msg/move.go
@@ -2,6 +2,7 @@ package msg
 
 import (
 	"errors"
+	"strings"
 	"time"
 
 	"git.sr.ht/~sircmpwn/getopt"
@@ -27,13 +28,13 @@ func (Move) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (Move) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) == 1 {
+		return errors.New("Usage: mv [-p] <folder>")
+	}
 	opts, optind, err := getopt.Getopts(args, "p")
 	if err != nil {
 		return err
 	}
-	if optind != len(args)-1 {
-		return errors.New("Usage: mv [-p] <folder>")
-	}
 	var (
 		createParents bool
 	)
@@ -63,12 +64,13 @@ func (Move) Execute(aerc *widgets.Aerc, args []string) error {
 	}
 	store.Next()
 	acct.Messages().Scroll()
-	store.Move([]uint32{msg.Uid}, args[optind], createParents, func(
+	joinedArgs := strings.Join(args[optind:], " ")
+	store.Move([]uint32{msg.Uid}, joinedArgs, createParents, func(
 		msg types.WorkerMessage) {
 
 		switch msg := msg.(type) {
 		case *types.Done:
-			aerc.PushStatus("Message moved to "+args[optind], 10*time.Second)
+			aerc.PushStatus("Message moved to "+joinedArgs, 10*time.Second)
 		case *types.Error:
 			aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
 				Color(tcell.ColorDefault, tcell.ColorRed)
diff --git a/commands/msgview/save.go b/commands/msgview/save.go
index 3b38ec7..33cd45f 100644
--- a/commands/msgview/save.go
+++ b/commands/msgview/save.go
@@ -11,9 +11,11 @@ import (
 	"strings"
 	"time"
 
-	"git.sr.ht/~sircmpwn/aerc/widgets"
 	"git.sr.ht/~sircmpwn/getopt"
 	"github.com/mitchellh/go-homedir"
+
+	"git.sr.ht/~sircmpwn/aerc/commands"
+	"git.sr.ht/~sircmpwn/aerc/widgets"
 )
 
 type Save struct{}
@@ -27,10 +29,14 @@ func (Save) Aliases() []string {
 }
 
 func (Save) Complete(aerc *widgets.Aerc, args []string) []string {
-	return nil
+	path := strings.Join(args, " ")
+	return commands.CompletePath(path)
 }
 
 func (Save) Execute(aerc *widgets.Aerc, args []string) error {
+	if len(args) == 1 {
+		return errors.New("Usage: :save [-p] <path>")
+	}
 	opts, optind, err := getopt.Getopts(args, "p")
 	if err != nil {
 		return err
@@ -38,7 +44,7 @@ func (Save) Execute(aerc *widgets.Aerc, args []string) error {
 
 	var (
 		mkdirs bool
-		path   string
+		path   string = strings.Join(args[optind:], " ")
 	)
 
 	for _, opt := range opts {
@@ -47,12 +53,8 @@ func (Save) Execute(aerc *widgets.Aerc, args []string) error {
 			mkdirs = true
 		}
 	}
-	if len(args) == optind+1 {
-		path = args[optind]
-	} else if defaultPath := aerc.Config().General.DefaultSavePath; defaultPath != "" {
+	if defaultPath := aerc.Config().General.DefaultSavePath; defaultPath != "" {
 		path = defaultPath
-	} else {
-		return errors.New("Usage: :save [-p] <path>")
 	}
 
 	mv := aerc.SelectedTab().(*widgets.MessageViewer)