Skip to content

[Feature Request] Possibility to select option with tab #25

Open
@cork

Description

@cork

Feature Request

I'm trying to replace an old cli with go-prompt, in that implementation tab was used to accept options. As far as I've been able to find only way to select an option is space in go-prompt?

Something like this:

package main

import (
	"fmt"
	"os"
	"strings"

	prompt "github.com/elk-language/go-prompt"
	istrings "github.com/elk-language/go-prompt/strings"
)

func completer(in prompt.Document) ([]prompt.Suggest, istrings.RuneNumber, istrings.RuneNumber) {
	line := in.TextBeforeCursor()

	words := strings.Split(line, " ")

	if len(words) < 2 {
		endIndex := in.CurrentRuneIndex()
		startIndex := endIndex - istrings.RuneCountInString(words[0])
		return prompt.FilterHasPrefix([]prompt.Suggest{
			{"run ", "run things"},
			{"test ", "test things"},
			{"call ", "call things"},
		}, words[0], true), startIndex, endIndex
	}

	endIndex := in.CurrentRuneIndex()
	startIndex := endIndex - istrings.RuneCountInString(in.GetWordBeforeCursor())

	switch words[0] {
	case "run":
		return prompt.FilterHasPrefix([]prompt.Suggest{
			{"-b", "background"},
			{"-f", "forground"},
			{"-r", "remote"},
		}, in.GetWordBeforeCursor(), true), startIndex, endIndex
	case "test":
		return nil, 0, 0
	case "call":
		return prompt.FilterHasPrefix([]prompt.Suggest{
			{"-l", "local"},
			{"-r", "regional"},
			{"-x", "cross country"},
		}, in.GetWordBeforeCursor(), true), startIndex, endIndex
	default:
		return nil, 0, 0
	}
}

func executor(line string) {
	fmt.Println(line)
	os.Exit(0)
}

func main() {
	p := prompt.New(
		executor,
		prompt.WithPrefix("run > "),
		prompt.WithCompleter(completer),
	)
	p.Run()
}

I've tried to use WithKeyBind but it isn't called during completion. Am i missing something or isn't this supported for the moment?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions