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 { func (ChangeFolder) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) < 2 { if len(args) == 1 {
return errors.New("Usage: cf <folder>") return errors.New("Usage: cf <folder>")
} }
acct := aerc.SelectedAccount() acct := aerc.SelectedAccount()
@ -36,17 +36,15 @@ func (ChangeFolder) Execute(aerc *widgets.Aerc, args []string) error {
return errors.New("No account selected") return errors.New("No account selected")
} }
previous := acct.Directories().Selected() previous := acct.Directories().Selected()
if args[1] == "-" { joinedArgs := strings.Join(args[1:], " ")
if joinedArgs == "-" {
if dir, ok := history[acct.Name()]; ok { if dir, ok := history[acct.Name()]; ok {
acct.Directories().Select(dir) acct.Directories().Select(dir)
} else { } else {
return errors.New("No previous folder to return to") return errors.New("No previous folder to return to")
} }
} else { } else {
if len(args) > 2 { acct.Directories().Select(joinedArgs)
args[1] = strings.Join(args[1:], " ")
}
acct.Directories().Select(args[1])
} }
history[acct.Name()] = previous history[acct.Name()] = previous

View File

@ -2,6 +2,7 @@ package account
import ( import (
"errors" "errors"
"strings"
"time" "time"
"github.com/gdamore/tcell" "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 { func (MakeDir) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 { if len(args) == 0 {
return errors.New("Usage: :mkdir <name>") return errors.New("Usage: :mkdir <name>")
} }
acct := aerc.SelectedAccount() acct := aerc.SelectedAccount()
if acct == nil { if acct == nil {
return errors.New("No account selected") return errors.New("No account selected")
} }
name := args[1] name := strings.Join(args[1:], " ")
acct.Worker().PostAction(&types.CreateDirectory{ acct.Worker().PostAction(&types.CreateDirectory{
Directory: name, Directory: name,
}, func(msg types.WorkerMessage) { }, func(msg types.WorkerMessage) {

View File

@ -24,11 +24,7 @@ func (ChangeDirectory) Aliases() []string {
} }
func (ChangeDirectory) Complete(aerc *widgets.Aerc, args []string) []string { func (ChangeDirectory) Complete(aerc *widgets.Aerc, args []string) []string {
path := "" path := strings.Join(args, " ")
if len(args) >= 1 {
path = args[0]
}
completions := CompletePath(path) completions := CompletePath(path)
var dirs []string 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 { 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]") return errors.New("Usage: cd [directory]")
} }
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
return err return err
} }
var target string target := strings.Join(args[1:], " ")
if len(args) == 1 { if target == "" {
target = "~" target = "~"
} else if args[1] == "-" { } else if target == "-" {
if previousDir == "" { if previousDir == "" {
return errors.New("No previous folder to return to") return errors.New("No previous folder to return to")
} else { } else {
target = previousDir target = previousDir
} }
} else {
target = args[1]
} }
target, err = homedir.Expand(target) target, err = homedir.Expand(target)
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package compose
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/commands" "git.sr.ht/~sircmpwn/aerc/commands"
@ -22,20 +23,16 @@ func (Attach) Aliases() []string {
} }
func (Attach) Complete(aerc *widgets.Aerc, args []string) []string { func (Attach) Complete(aerc *widgets.Aerc, args []string) []string {
path := "" path := strings.Join(args, " ")
if len(args) >= 1 {
path = args[0]
}
return commands.CompletePath(path) return commands.CompletePath(path)
} }
func (Attach) Execute(aerc *widgets.Aerc, args []string) error { func (Attach) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 { if len(args) == 1 {
return fmt.Errorf("Usage: :attach <path>") return fmt.Errorf("Usage: :attach <path>")
} }
path := args[1] path := strings.Join(args[1:], " ")
path, err := homedir.Expand(path) path, err := homedir.Expand(path)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package compose
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
@ -20,7 +21,6 @@ func (Detach) Aliases() []string {
func (Detach) Complete(aerc *widgets.Aerc, args []string) []string { func (Detach) Complete(aerc *widgets.Aerc, args []string) []string {
composer, _ := aerc.SelectedTab().(*widgets.Composer) composer, _ := aerc.SelectedTab().(*widgets.Composer)
return composer.GetAttachments() return composer.GetAttachments()
} }
@ -28,12 +28,8 @@ func (Detach) Execute(aerc *widgets.Aerc, args []string) error {
var path string var path string
composer, _ := aerc.SelectedTab().(*widgets.Composer) composer, _ := aerc.SelectedTab().(*widgets.Composer)
if len(args) > 2 { if len(args) > 1 {
return fmt.Errorf("Usage: :detach [path]") path = strings.Join(args[1:], " ")
}
if len(args) == 2 {
path = args[1]
} else { } else {
// if no attachment is specified, delete the first in the list // if no attachment is specified, delete the first in the list
atts := composer.GetAttachments() atts := composer.GetAttachments()

View File

@ -23,9 +23,10 @@ func (ChangeTab) Complete(aerc *widgets.Aerc, args []string) []string {
if len(args) == 0 { if len(args) == 0 {
return aerc.TabNames() return aerc.TabNames()
} }
joinedArgs := strings.Join(args, " ")
out := make([]string, 0) out := make([]string, 0)
for _, tab := range aerc.TabNames() { for _, tab := range aerc.TabNames() {
if strings.HasPrefix(tab, args[0]) { if strings.HasPrefix(tab, joinedArgs) {
out = append(out, tab) 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 { 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]) return fmt.Errorf("Usage: %s <tab>", args[0])
} }
if args[1] == "-" { joinedArgs := strings.Join(args[1:], " ")
if joinedArgs == "-" {
ok := aerc.SelectPreviousTab() ok := aerc.SelectPreviousTab()
if !ok { if !ok {
return errors.New("No previous tab to return to") return errors.New("No previous tab to return to")
} }
} else { } else {
n, err := strconv.Atoi(args[1]) n, err := strconv.Atoi(joinedArgs)
if err == nil { if err == nil {
if strings.HasPrefix(args[1], "+") { if strings.HasPrefix(joinedArgs, "+") {
for ; n > 0; n-- { for ; n > 0; n-- {
aerc.NextTab() aerc.NextTab()
} }
} else if strings.HasPrefix(args[1], "-") { } else if strings.HasPrefix(joinedArgs, "-") {
for ; n < 0; n++ { for ; n < 0; n++ {
aerc.PrevTab() aerc.PrevTab()
} }
@ -60,7 +62,7 @@ func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
} }
} }
} else { } else {
ok := aerc.SelectTab(args[1]) ok := aerc.SelectTab(joinedArgs)
if !ok { if !ok {
return errors.New("No tab with that name") return errors.New("No tab with that name")
} }

View File

@ -2,6 +2,7 @@ package msg
import ( import (
"errors" "errors"
"strings"
"time" "time"
"git.sr.ht/~sircmpwn/getopt" "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 { 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") opts, optind, err := getopt.Getopts(args, "p")
if err != nil { if err != nil {
return err return err
} }
if optind != len(args)-1 {
return errors.New("Usage: cp [-p] <folder>")
}
var ( var (
createParents bool createParents bool
) )
@ -53,16 +54,17 @@ func (Copy) Execute(aerc *widgets.Aerc, args []string) error {
if err != nil { if err != nil {
return err return err
} }
store.Copy([]uint32{msg.Uid}, args[optind], createParents, func( store.Copy([]uint32{msg.Uid}, strings.Join(args[optind:], " "),
msg types.WorkerMessage) { createParents, func(
msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: case *types.Done:
aerc.PushStatus("Messages copied.", 10*time.Second) aerc.PushStatus("Messages copied.", 10*time.Second)
case *types.Error: case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second). aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed) Color(tcell.ColorDefault, tcell.ColorRed)
} }
}) })
return nil return nil
} }

View File

@ -2,6 +2,7 @@ package msg
import ( import (
"errors" "errors"
"strings"
"time" "time"
"git.sr.ht/~sircmpwn/getopt" "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 { 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") opts, optind, err := getopt.Getopts(args, "p")
if err != nil { if err != nil {
return err return err
} }
if optind != len(args)-1 {
return errors.New("Usage: mv [-p] <folder>")
}
var ( var (
createParents bool createParents bool
) )
@ -63,12 +64,13 @@ func (Move) Execute(aerc *widgets.Aerc, args []string) error {
} }
store.Next() store.Next()
acct.Messages().Scroll() 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) { msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: 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: case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second). aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed) Color(tcell.ColorDefault, tcell.ColorRed)

View File

@ -11,9 +11,11 @@ import (
"strings" "strings"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/getopt" "git.sr.ht/~sircmpwn/getopt"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"git.sr.ht/~sircmpwn/aerc/commands"
"git.sr.ht/~sircmpwn/aerc/widgets"
) )
type Save struct{} type Save struct{}
@ -27,10 +29,14 @@ func (Save) Aliases() []string {
} }
func (Save) Complete(aerc *widgets.Aerc, args []string) []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 { 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") opts, optind, err := getopt.Getopts(args, "p")
if err != nil { if err != nil {
return err return err
@ -38,7 +44,7 @@ func (Save) Execute(aerc *widgets.Aerc, args []string) error {
var ( var (
mkdirs bool mkdirs bool
path string path string = strings.Join(args[optind:], " ")
) )
for _, opt := range opts { for _, opt := range opts {
@ -47,12 +53,8 @@ func (Save) Execute(aerc *widgets.Aerc, args []string) error {
mkdirs = true mkdirs = true
} }
} }
if len(args) == optind+1 { if defaultPath := aerc.Config().General.DefaultSavePath; defaultPath != "" {
path = args[optind]
} else if defaultPath := aerc.Config().General.DefaultSavePath; defaultPath != "" {
path = defaultPath path = defaultPath
} else {
return errors.New("Usage: :save [-p] <path>")
} }
mv := aerc.SelectedTab().(*widgets.MessageViewer) mv := aerc.SelectedTab().(*widgets.MessageViewer)