about summary refs log tree commit diff stats
path: root/html/042new.cc.html
Commit message (Expand)AuthorAgeFilesLines
* 1291Kartik K. Agaram2015-05-061-1/+1
* 1279 - colorized rendering of the source filesKartik K. Agaram2015-05-061-0/+195
45'>45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
package commands

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

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

type ChangeTab struct{}

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

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

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, joinedArgs) {
			out = append(out, tab)
		}
	}
	return out
}

func (ChangeTab) Execute(aerc *widgets.Aerc, args []string) error {
	if len(args) == 1 {
		return fmt.Errorf("Usage: %s <tab>", args[0])
	}
	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(joinedArgs)
		if err == nil {
			if strings.HasPrefix(joinedArgs, "+") {
				for ; n > 0; n-- {
					aerc.NextTab()
				}
			} else if strings.HasPrefix(joinedArgs, "-") {
				for ; n < 0; n++ {
					aerc.PrevTab()
				}
			} else {
				ok := aerc.SelectTabIndex(n)
				if !ok {
					return errors.New(
						"No tab with that index")
				}
			}
		} else {
			ok := aerc.SelectTab(joinedArgs)
			if !ok {
				return errors.New("No tab with that name")
			}
		}
	}
	return nil
}