From 9de57bdcf5246827e9b1a57c905203e2edf6edf4 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Wed, 10 May 2017 07:28:43 +0300 Subject: Enable completion and executable be the same command Fixes #6 --- run.go | 75 ------------------------------------------------------------------ 1 file changed, 75 deletions(-) delete mode 100644 run.go (limited to 'run.go') diff --git a/run.go b/run.go deleted file mode 100644 index 5d9706f..0000000 --- a/run.go +++ /dev/null @@ -1,75 +0,0 @@ -// Package complete provides a tool for bash writing bash completion in go. -// -// Writing bash completion scripts is a hard work. This package provides an easy way -// to create bash completion scripts for any command, and also an easy way to install/uninstall -// the completion of the command. -package complete - -import ( - "fmt" - "os" - "strings" - - "github.com/posener/complete/cmd" -) - -const ( - envComplete = "COMP_LINE" - envDebug = "COMP_DEBUG" -) - -// Run get a command, get the typed arguments from environment -// variable, and print out the complete options -// name is the name of command we want to auto complete. -// IMPORTANT: it must be the same name - if the auto complete -// completes the 'go' command, name must be equal to "go". -func Run(name string, c Command) { - args, ok := getLine() - if !ok { - cmd.Run(name) - return - } - Log("Completing args: %s", args) - - options := complete(c, args) - - Log("Completion: %s", options) - output(options) -} - -// complete get a command an command line arguments and returns -// matching completion options -func complete(c Command, args []string) (matching []string) { - options, _ := c.options(args[:len(args)-1]) - - // choose only matching options - l := last(args) - for _, option := range options { - if option.Match(l) { - matching = append(matching, option.String()) - } - } - return -} - -func getLine() ([]string, bool) { - line := os.Getenv(envComplete) - if line == "" { - return nil, false - } - return strings.Split(line, " "), true -} - -func last(args []string) (last string) { - if len(args) > 0 { - last = args[len(args)-1] - } - return -} - -func output(options []string) { - // stdout of program defines the complete options - for _, option := range options { - fmt.Println(option) - } -} -- cgit v1.2.3