How can i implement autocomplete commands with tab in telnet client (for example puTTy).
I have socket server written in go:
server := tcp_server.New("localhost:9999")
...
server.Listen()
But telnet client puTTy send me char only when i press enter, so i can't check every character to find if it match "\t" and do autocomplete commands.
I think you need something like termbox-go
https://github.com/nsf/termbox-go
package main
import (
"fmt"
termbox "github.com/nsf/termbox-go"
"io/ioutil"
"net/http"
"os"
)
/*
this is a simple cli program, this keeps polling the command line argument
until tab or Esc is pressed,
if TAB is pressed it does a google search of the word previous to that
and exists incase someone presses enter
*/
func main() {
termbox.Init()
defer termbox.Close()
var searchString string
for {
ev := termbox.PollEvent()
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyTab:
meaning := getMeaningFromWeb(searchString)
fmt.Println(meaning)
searchString = ""
continue
case termbox.KeyEsc:
panic(1)
default:
searchString = searchString + string(ev.Ch)
}
case termbox.EventError:
os.Exit(1)
default:
continue
}
}
}
func getMeaningFromWeb(a string) string {
cl := &http.Client{}
req, err := http.NewRequest("GET", "https://www.reddit.com/r/wallpaper/search.json?q="+a, nil)
req.Header.Set("User-Agent", "whitespace")
req.Header.Set("Host", "reddit.com")
resp, err := cl.Do(req)
defer resp.Body.Close()
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Sprintf("%v", response)
}
return fmt.Sprintf("%s", string(response))
}
this is an example of using this in commandline application, if you run this application, it will accept words(on press of tab) and quit(on pressing escape). this application searches a sub reddit for wallpaper. I am printing the response to terminal. Its not perfect, there is some config( I will update as soon as I can) that needs to be set for user input to appear. Plus there is no filter as of yet to use backspace
. You won't see what you type and you can't use backspace, but the rest of functionality does as I described.