c-bata / go-prompt

Building powerful interactive prompts in Go, inspired by python-prompt-toolkit.
https://godoc.org/github.com/c-bata/go-prompt
MIT License
5.29k stars 348 forks source link

Fix broken spacing for each row of completion area #243

Open raccoonback opened 3 years ago

raccoonback commented 3 years ago

We tried to use Emoji, but we found that each row in the completion area was pushed back as shown in the following issue.

Related issue: https://github.com/c-bata/go-prompt/issues/209

# Error case
md > 
           šŸŽØ    Store the username and age             
                āš”ļø  Store the article text posted by user  
                   šŸ”„    Store the text commented to articles   
                        šŸ›    Store the text commented to articles   
                             āœØ    Store the text commented to articles   
                                 šŸ“    Store the text commented to articles   

# Solved case
md > šŸš§
      šŸŽ‰    Store the text commented to articles   
      āœ…    Store the text commented to articles   
      šŸ”’    Store the text commented to articles   
      šŸ”–    Store the text commented to articles   
      šŸšØ    Store the text commented to articles   
      šŸš§    Store the text commented to articles   

Therefore, I applied carriage return to specify the initial cursor of all rows at the column interval of the initial cursor.

In addition, we applied carriage return before moving the prefix to the current cursor column, so that the prefix does not look redundant.

# init state
abcdefghi > 
             šŸŽ‰    Store the text commented to articles   
             āœ…    Store the text commented to articles   
             šŸ”’    Store the text commented to articles   
             šŸ”–    Store the text commented to articles   
             šŸšØ    Store the text commented to articles   
             šŸš§    Store the text commented to articles   

# error case
aabcdefghi > šŸŒ±        // The 'a' of old prefix  remains.
             šŸ„…    Store the text commented to articles   
             šŸš©    Store the text commented to articles   
             šŸŒ±    Store the text commented to articles   
             šŸ·     Store the text commented to articles   
             šŸ”    Store the text commented to articles   
             āš—     Store the text commented to articles   
raccoonback commented 3 years ago

You can test it with the code below.

package main

import (
    "fmt"
    "github.com/c-bata/go-prompt"
)

func completer(d prompt.Document) []prompt.Suggest {
    s := []prompt.Suggest{
        {Text: "šŸŽØ", Description: "Store the username and age"},
        {Text: "āš”ļø", Description: "Store the article text posted by user"},
        {Text: "šŸ”„", Description: "Store the text commented to articles"},
        {Text: "šŸ›", Description: "Store the text commented to articles"},
        {Text: "āœØ", Description: "Store the text commented to articles"},
        {Text: "šŸ“", Description: "Store the text commented to articles"},
        {Text: "šŸš€", Description: "Store the text commented to articles"},
        {Text: "šŸ’„", Description: "Store the text commented to articles"},
        {Text: "šŸŽ‰", Description: "Store the text commented to articles"},
        {Text: "āœ…", Description: "Store the text commented to articles"},
        {Text: "šŸ”’", Description: "Store the text commented to articles"},
        {Text: "šŸ”–", Description: "Store the text commented to articles"},
        {Text: "šŸšØ", Description: "Store the text commented to articles"},
        {Text: "šŸš§", Description: "Store the text commented to articles"},
        {Text: "šŸ’š", Description: "Store the text commented to articles"},
        {Text: "ā¬‡", Description: "Store the text commented to articles"},
        {Text: "ā¬†", Description: "Store the text commented to articles"},
        {Text: "šŸ“Œ", Description: "Store the text commented to articles"},
        {Text: "šŸ‘·", Description: "Store the text commented to articles"},
        {Text: "šŸ“ˆ", Description: "Store the text commented to articles"},
        {Text: "ā™»", Description: "Store the text commented to articles"},
        {Text: "āž•", Description: "Store the text commented to articles"},
        {Text: "āž–", Description: "Store the text commented to articles"},
        {Text: "šŸ”§", Description: "Store the text commented to articles"},
        {Text: "šŸŒ", Description: "Store the text commented to articles"},
        //
        {Text: "āœļø", Description: "Store the text commented to articles"},
        {Text: "šŸ’©", Description: "Store the text commented to articles"},
        {Text: "āŖ", Description: "Store the text commented to articles"},
        {Text: "šŸ“¦", Description: "Store the text commented to articles"},
        {Text: "šŸ‘½", Description: "Store the text commented to articles"},
        {Text: "šŸšš", Description: "Store the text commented to articles"},
        {Text: "šŸ’„", Description: "Store the text commented to articles"},
        {Text: "ā™æ", Description: "Store the text commented to articles"},
        {Text: "šŸ±", Description: "Store the text commented to articles"},
        {Text: "šŸ’”", Description: "Store the text commented to articles"},
        {Text: "šŸ’¬", Description: "Store the text commented to articles"},
        {Text: "šŸ—‘ļø", Description: "Store the text commented to articles"},
        {Text: "šŸ’«", Description: "Store the text commented to articles"},
        {Text: "šŸ„…", Description: "Store the text commented to articles"},
        {Text: "šŸš©", Description: "Store the text commented to articles"},
        {Text: "šŸŒ±", Description: "Store the text commented to articles"},
        {Text: "šŸ·", Description: "Store the text commented to articles"},
        {Text: "šŸ”", Description: "Store the text commented to articles"},
        {Text: "āš—", Description: "Store the text commented to articles"},
        {Text: "šŸ“ø", Description: "Store the text commented to articles"},
        {Text: "šŸ™ˆ", Description: "Store the text commented to articles"},
        {Text: "šŸ„š", Description: "Store the text commented to articles"},
        {Text: "šŸ¤”", Description: "Store the text commented to articles"},
        {Text: "šŸ“±", Description: "Store the text commented to articles"},
        {Text: "šŸ—", Description: "Store the text commented to articles"},
        {Text: "šŸšø", Description: "Store the text commented to articles"},
        {Text: "šŸ‘„", Description: "Store the text commented to articles"},
        {Text: "šŸ”‡", Description: "Store the text commented to articles"},
        {Text: "šŸ”Š", Description: "Store the text commented to articles"},
        {Text: "šŸ—ƒ", Description: "Store the text commented to articles"},
        {Text: "šŸ’¬", Description: "Store the text commented to articles"},
        {Text: "šŸ»", Description: "Store the text commented to articles"},
        {Text: "šŸ’”", Description: "Store the text commented to articles"},
        {Text: "ā™æ", Description: "Store the text commented to articles"},
        {Text: "šŸ“„", Description: "Store the text commented to articles"},
    }
    return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}

func main() {
    fmt.Println("Please select table.")
    t := prompt.Input("abcdefgi > ", completer, prompt.OptionShowCompletionAtStart(),
        prompt.OptionCompletionOnDown(),)
    fmt.Println("You selected " + t)
}