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
This commit is contained in:
Jeffas 2019-09-20 17:16:29 +01:00 committed by Drew DeVault
parent 3ec9fd216d
commit 39307a6fa7
9 changed files with 60 additions and 66 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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 {

View File

@ -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 {

View File

@ -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()

View File

@ -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")
}

View File

@ -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
}

View File

@ -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)

View File

@ -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)