如何在telnet客户端中使用tab实现自动完成命令(例如puTTy)。
我有写入的套接字服务器:
server := tcp_server.New("localhost:9999")
...
server.Listen()
但telnet客户端puTTy仅在我输入时才发送给我char,因此我无法检查每个字符是否与“\ t”匹配并执行自动完成命令。
我想你需要像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))
}
这是在命令行应用程序中使用它的一个示例,如果您运行此应用程序,它将接受单词(按下选项卡)并退出(按下转义)。此应用程序在子reddit中搜索壁纸。我正在打印终端的响应。它不完美,有一些配置(我会尽快更新),需要设置用户输入才能显示。此外,还没有使用backspace
的过滤器。您将看不到键入的内容,也无法使用退格键,但其余功能也如我所述。