带选项卡的Golang telnet自动完成命令

时间:2018-08-28 09:25:37

标签: sockets go telnet

我该如何在telnet客户端(例如puTTy)中使用Tab实现自动完成命令。

我用go编写了套接字服务器:

server := tcp_server.New("localhost:9999")
...
server.Listen()

但是telnet客户端仅在按回车键时才发送char给我,所以我无法检查每个字符是否都匹配“ \ t”并执行自动完成命令。

1 个答案:

答案 0 :(得分:2)

我认为您需要类似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))
}

这是在命令行应用程序中使用此示例,如果您运行此应用程序,它将接受单词(按Tab键)和quit(按Escape键)。此应用程序在子Reddit中搜索墙纸。我正在将响应打印到终端。它不是很完美,需要设置一些config(我将尽快更新),以便出现用户输入。另外,还没有使用backspace的过滤器。您将看不到键入的内容,也无法使用退格键,但是其余功能如我所述。