Edit

Opens a dotfile in your editor.

var editCmd = &cobra.Command{
	Use:   "edit [FILE]",
	Short: "edit a dotfile",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		c := exec.Command(editor(), freckles.Dir()+"/"+args[0])
		c.Stdin = os.Stdin
		c.Stdout = os.Stdout
		c.Stderr = os.Stderr
		c.Run()
	},
}

Completion is provided with a custom action.

	carapace.Gen(editCmd).PositionalCompletion(
		action.ActionFreckles(),
	)

Action

Custom Actions are simply functions returning Action.

package action

import (
	"path/filepath"

	"github.com/carapace-sh/carapace"
	"github.com/carapace-sh/carapace/pkg/style"
	"github.com/carapace-sh/freckles/pkg/freckles"
)

func ActionFreckles() carapace.Action {
	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
		vals := make([]string, 0)
		freckles.Walk(func(freckle freckles.Freckle) error {
			vals = append(vals, freckle.Path)
			return nil
		})
		return carapace.ActionValues(vals...).MultiParts("/").StyleF(func(s string, sc style.Context) string {
			return style.ForPath(filepath.Join(freckles.Dir(), s), sc)
		})
	})
}

There are two main phases in Carapace:

  1. The creation of the command structure and registration of completions.
  2. The parsing of the command line and invocation of the corresponding Action.
carapace.Gen(initCmd).PositionalCompletion(
	carapace.ActionValues(initCmd.Flag("clone").Value.String()), // (1) completes the default value
	carapace.ActionCallback(func(c carapace.Context) carapace.Action {
		return carapace.ActionValues(initCmd.Flag("clone").Value.String()) // (2) completes the parsed value
	}),
)

Custom Actions should almost always be wrapped in ActionCallback so code is only executed when invoked. The Default Actions do this implicitly.


The Walk function returns dotfiles with their full path within the repository: path/to/file. By modifying the Action with MultiParts the segments get completed separately.


Additionally, style.ForPath highlights them with the style defined by the LS_COLORS environment variable.