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

example with help #68

Closed
keith6014 opened this issue Apr 26, 2018 · 1 comment
Closed

example with help #68

keith6014 opened this issue Apr 26, 2018 · 1 comment

Comments

@keith6014
Copy link

Do you have an example with help command?

So, for example, if I do

help
i would my shell to show all valid commands

help commandA
i would like to show help message for "commandA"

or

help commandA subcommandA
would show help command for commandA subcommandA (i think you get the point)

Is that possible, if so do you have some example code?

@c-bata
Copy link
Owner

c-bata commented Jun 23, 2018

go-prompt just provides prompt.Document object to completer functions.
And you can access all buffer information via it.

When you input a following text(period means the cursor position):

help commandA
        . <- cursor position

In this case,

  • document.TextBeforeCursor() returns help comm
  • document.GetWordBeforeCursor() returns comm

So you can implement that like:

func completer(d prompt.Document) []prompt.Suggest {
    tbc := d.TextBeforeCursor()
    args := strings.Split(tbc, " ")
    depth := len()

    switch depth {
    case 1:
         suggests := []prompt.Suggest{
             {Text: "help", "i would my shell to show all valid commands"},
             {Text: "commandA", "..."},
         }
         return prompt.FilterHasPrefix(suggests, d.GetWordBeforeCursor())
    case 2:
        switch args[0] {
        case "help":
            suggests := []prompt.Suggest{
                {Text: "commandA", "i would like to show help message for "commandA""},
                {Text: "commandB", "..."},
            }
            return prompt.FilterHasPrefix(suggests, d.GetWordBeforeCursor())
        case "commandA":
             // return subcommand suggestions of commandA
        }
    case 3:
        switch strings.Join(args[:1], " ") {
        case "help commandA":
            suggests := []prompt.Suggest{
                {Text: "subcommandA", "would show help command for commandA subcommandA"},
                {Text: "subcommandB", "..."},
            }
            return suggests
        }
    }
}

@c-bata c-bata closed this as completed Jun 24, 2018
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

No branches or pull requests

2 participants