gpt4 book ai didi

windows - 终端原始模式不支持 Windows 上的箭头

转载 作者:行者123 更新时间:2023-12-01 20:25:44 25 4
gpt4 key购买 nike

我正在尝试使我的控制台原始(在 Windows 上),并且我正在使用 ssh/terminal 包:

package main

import (
"fmt"
"os"

"golang.org/x/crypto/ssh/terminal"
)

type sh struct{}

func (sh *sh) Read(b []byte) (int, error) {
return os.Stdin.Read(b)
}

func (sh *sh) Write(b []byte) (int, error) {
return os.Stdout.Write(b)
}

func main() {
oldstate, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}

defer terminal.Restore(int(os.Stdin.Fd()), oldstate)

term := terminal.NewTerminal(&sh{}, "")
term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
fmt.Println("callback:", line, pos, key)

return "", 0, false
}

line, err := term.ReadLine()
fmt.Println("result:", line, err)
}

效果很好,我可以捕获 Ctrl-C 和其他特殊键,但我不能使用箭头键。

我希望箭头键移动光标或至少调用 AutoCompleteCallback 我可以自己移动光标。

最佳答案

使用 github.com/containerd/console 包来制作原始终端,现在它就像一个魅力。

新代码:

package main

import (
"fmt"
"os"

"github.com/containerd/console"
"golang.org/x/crypto/ssh/terminal"
)

func main() {
current := console.Current()
defer current.Reset()

if err := current.SetRaw(); err != nil {
panic(err)
}

term := terminal.NewTerminal(current, "")
term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
// fmt.Println("callback:", line, pos, key)

return "", 0, false
}

line, err := term.ReadLine()
fmt.Println("result:", line, err)
}

关于windows - 终端原始模式不支持 Windows 上的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58237670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com