| 1 | package mainscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/" |
| 5 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 6 | "net/urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 8 | "syncscip-go gomod github.com/golang/go/src go1.18 sync/" |
| 9 | |
| 10 | "github.com/gdamore/tcell/v2scip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/" |
| 11 | "github.com/darkhz/tviewscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/" |
| 12 | ) |
| 13 | |
| 14 | type companyscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/company# struct { |
| 15 | Namescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/company#Name. string `json:"name"` |
| 16 | } |
| 17 | |
| 18 | func mainscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/main().() { |
| 19 | app := tviewscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/.NewApplicationscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/NewApplication().() |
| 20 | inputField := tviewscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/.NewInputFieldscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/NewInputField().(). |
| 21 | SetLabelscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/InputField#SetLabel().("Enter a company name: "). |
| 22 | SetFieldWidthscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/InputField#SetFieldWidth().(30). |
| 23 | SetDoneFuncscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/InputField#SetDoneFunc().(func(key tcellscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/.Keyscip-go gomod github.com/gdamore/tcell/v2 v2.7.4 `github.com/gdamore/tcell/v2`/Key#) { |
| 24 | app.Stopscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Application#Stop().() |
| 25 | }) |
| 26 | |
| 27 | // Set up autocomplete function. |
| 28 | var mutex syncscip-go gomod github.com/golang/go/src go1.18 sync/.Mutexscip-go gomod github.com/golang/go/src go1.18 sync/Mutex# |
| 29 | prefixMap := make(map[string][]string) |
| 30 | inputField.SetAutocompleteFuncscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/InputField#SetAutocompleteFunc().(func(currentText string) []string { |
| 31 | // Ignore empty text. |
| 32 | prefix := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.18 strings/TrimSpace().(stringsscip-go gomod github.com/golang/go/src go1.18 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.18 strings/ToLower().(currentText)) |
| 33 | if prefix == "" { |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | // Do we have entries for this text already? |
| 38 | mutex.Lockscip-go gomod github.com/golang/go/src go1.18 sync/Mutex#Lock().() |
| 39 | defer mutex.Unlockscip-go gomod github.com/golang/go/src go1.18 sync/Mutex#Unlock().() |
| 40 | entries, ok := prefixMap[prefix] |
| 41 | if ok { |
| 42 | return entries |
| 43 | } |
| 44 | |
| 45 | // No entries yet. Issue a request to the API in a goroutine. |
| 46 | go func() { |
| 47 | // Ignore errors in this demo. |
| 48 | url := "https://autocomplete.clearbit.com/v1/companies/suggest?query=" + urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.18 `net/url`/QueryEscape().(prefix) |
| 49 | res, err := httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Getscip-go gomod github.com/golang/go/src go1.18 `net/http`/Get().(url) |
| 50 | if err != nil { |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | // Store the result in the prefix map. |
| 55 | var companies []*companyscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/company# |
| 56 | dec := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/NewDecoder().(res.Bodyscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Body.) |
| 57 | if err := dec.Decodescip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Decoder#Decode().(&companies); err != nil { |
| 58 | return |
| 59 | } |
| 60 | entries := make([]string, 0, len(companies)) |
| 61 | OuterLoop: |
| 62 | for _, c := range companies { |
| 63 | for _, entry := range entries { // Eliminate duplicates. |
| 64 | if stringsscip-go gomod github.com/golang/go/src go1.18 strings/.EqualFoldscip-go gomod github.com/golang/go/src go1.18 strings/EqualFold().(entry, c.Namescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/company#Name.) { |
| 65 | continue OuterLoop |
| 66 | } |
| 67 | } |
| 68 | entries = append(entries, c.Namescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview/demos/inputfield/autocompleteasync`/company#Name.) |
| 69 | } |
| 70 | mutex.Lockscip-go gomod github.com/golang/go/src go1.18 sync/Mutex#Lock().() |
| 71 | prefixMap[prefix] = entries |
| 72 | mutex.Unlockscip-go gomod github.com/golang/go/src go1.18 sync/Mutex#Unlock().() |
| 73 | |
| 74 | // Trigger an update to the input field. |
| 75 | inputField.Autocompletescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/InputField#Autocomplete().() |
| 76 | |
| 77 | // Also redraw the screen. |
| 78 | app.Drawscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Application#Draw().() |
| 79 | }() |
| 80 | |
| 81 | return nil |
| 82 | }) |
| 83 | |
| 84 | if err := app.EnableMousescip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Application#EnableMouse().(true).SetRootscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Application#SetRoot().(inputField, true).Runscip-go gomod github.com/darkhz/tview . `github.com/darkhz/tview`/Application#Run().(); err != nil { |
| 85 | panic(err) |
| 86 | } |
| 87 | } |