aboutsummaryrefslogblamecommitdiffstats
path: root/commands/ct.go
blob: 1b5659c74ee24455336e3b1849e393a0a8b00dcd (plain) (tree)
1
2
3
4
5
6
7
8
9




                
                 

                 
                                    







                             
                                     


                                           
                                                   
                           
                                     
         
                                             
                                                                                                      

 
                                               
                           
                                                             
         

                                                 
                                             



                                                                         
                                                  
                               

                                                                
                                                  
                                                     
                                 
                                                                
                                                  
                                                     
                                 
                                
                                                           





                                                                         
                                                       


                                                                          



                  
package commands

import (
	"errors"
	"fmt"
	"strconv"
	"strings"

	"git.sr.ht/~rjarry/aerc/app"
)

type ChangeTab struct{}

func init() {
	register(ChangeTab{})
}

func (ChangeTab) Aliases() []string {
	return []string{"ct", "change-tab"}
}

func (ChangeTab) Complete(args []string) []string {
	if len(args) == 0 {
		return app.TabNames()
	}
	joinedArgs := strings.Join(args, " ")
	return FilterList(app.TabNames(), joinedArgs, "", app.SelectedAccountUiConfig().FuzzyComplete)
}

func (ChangeTab) Execute(args []string) error {
	if len(args) == 1 {
		return fmt.Errorf("Usage: %s <tab>", args[0])
	}
	joinedArgs := strings.Join(args[1:], " ")
	if joinedArgs == "-" {
		ok := app.SelectPreviousTab()
		if !ok {
			return errors.New("No previous tab to return to")
		}
	} else {
		n, err := strconv.Atoi(joinedArgs)
		if err == nil {
			switch {
			case strings.HasPrefix(joinedArgs, "+"):
				for ; n > 0; n-- {
					app.NextTab()
				}
			case strings.HasPrefix(joinedArgs, "-"):
				for ; n < 0; n++ {
					app.PrevTab()
				}
			default:
				ok := app.SelectTabIndex(n)
				if !ok {
					return errors.New(
						"No tab with that index")
				}
			}
		} else {
			ok := app.SelectTab(joinedArgs)
			if !ok {
				return errors.New("No tab with that name")
			}
		}
	}
	return nil
}