Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to set the "hasPrefix" function in Complete() #99

Closed
wants to merge 2 commits into from

Conversation

WillAbides
Copy link

This PR comes from my desire to have case- and diacritic-insensitive completions. I have a command that takes usernames as arguments. When I type "wi" for the username argument I want to see "WillAbides" listed along with the usernames that start with lowercase "wi". The code currently in master doesn't allow for this because it always applies strings.HasPrefix(option, a.Last).

The simplest place I found to add this without affecting the other usage was as a variadic argument to Complete(). The down side to making it an argument to Complete() is that means all arguments will use the same hasPrefix func.

In case it helps to understand the use case, this is a simplified version of how I implemented case-insensitive completions:

package main

import (
	"github.com/posener/complete"
	"golang.org/x/text/language"
	"golang.org/x/text/search"
)

var insensitiveSearch = search.New(language.Und, search.IgnoreCase, search.IgnoreDiacritics, search.IgnoreWidth)

func hasPrefix(in, prefix string) bool {
	if len(prefix) == 0 {
		return true
	}
	if len(prefix) > len(in) {
		return false
	}
	return insensitiveSearch.Equal([]byte(in[0:len(prefix)]), []byte(prefix))
}

func main() {
	cmp := complete.New("my_command", complete.Command{
	//	config removed
	})
	if cmp.Complete(complete.WithMatcher(hasPrefix)) {
		return
	}
	
	// do other stuff
}

@codecov
Copy link

codecov bot commented Jul 6, 2019

Codecov Report

Merging #99 into master will increase coverage by 0.08%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #99      +/-   ##
==========================================
+ Coverage   89.76%   89.84%   +0.08%     
==========================================
  Files          12       12              
  Lines         879      886       +7     
==========================================
+ Hits          789      796       +7     
  Misses         79       79              
  Partials       11       11
Impacted Files Coverage Δ
complete.go 76.08% <100%> (+4.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2f2ff27...31f280e. Read the comment docs.

@WillAbides
Copy link
Author

closing this in favor of #100

@WillAbides WillAbides closed this Jul 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant