golang支持Emacs键绑定和语法高亮的命令行编辑插件go-readline-ny的使用
Golang 支持 Emacs 键绑定和语法高亮的命令行编辑插件 go-readline-ny 的使用
go-readline-ny 是一个为 Go 语言编写的 CUI 应用程序提供单行输入的库。它具有以下特性:
- Emacs 风格的键绑定
- 输入历史记录
- 单词补全(文件名、命令名或给定数组中的任何名称)
- 语法高亮
- 支持平台:Windows 和 Linux
- 完整的 Unicode (UTF8) 支持
简单示例
package main
import (
"context"
"fmt"
"github.com/nyaosorg/go-readline-ny"
)
func main() {
var editor readline.Editor
text, err := editor.ReadLine(context.Background())
if err != nil {
fmt.Printf("ERR=%s\n", err.Error())
} else {
fmt.Printf("TEXT=%s\n", text)
}
}
微型 Shell 示例
这个示例演示了提示符更改、语法高亮、文件名补全、历史记录浏览和操作系统剪贴板访问:
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"github.com/atotto/clipboard"
"github.com/mattn/go-colorable"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/completion"
"github.com/nyaosorg/go-readline-ny/keys"
"github.com/nyaosorg/go-readline-ny/simplehistory"
)
type OSClipboard struct{}
func (OSClipboard) Read() (string, error) {
return clipboard.ReadAll()
}
func (OSClipboard) Write(s string) error {
return clipboard.WriteAll(s)
}
func main() {
history := simplehistory.New()
editor := &readline.Editor{
PromptWriter: func(w io.Writer) (int, error) {
return io.WriteString(w, "\x1B[36;22m$ ") // 用青色打印 `$ `
},
Writer: colorable.NewColorableStdout(),
History: history,
Highlight: []readline.Highlight{
{Pattern: regexp.MustCompile("&"), Sequence: "\x1B[33;49;22m"},
{Pattern: regexp.MustCompile(`"[^"]*"`), Sequence: "\x1B[35;49;22m"},
{Pattern: regexp.MustCompile(`%[^%]*%`), Sequence: "\x1B[36;49;1m"},
{Pattern: regexp.MustCompile("\u3000"), Sequence: "\x1B[37;41;22m"},
},
HistoryCycling: true,
PredictColor: [...]string{"\x1B[3;22;34m", "\x1B[23;39m"},
ResetColor: "\x1B[0m",
DefaultColor: "\x1B[33;49;1m",
Clipboard: OSClipboard{},
}
editor.BindKey(keys.CtrlI, &completion.CmdCompletionOrList2{
// 这里列出的字符将从补全中排除
Delimiter: "&|><;",
// 当候选包含空格时,用这些字符包围
Enclosure: `"'`,
// 当只有一个候选时附加的字符串
Postfix: " ",
// 用于列出候选的函数
Candidates: completion.PathComplete,
})
fmt.Println("Tiny Shell. Type Ctrl-D to quit.")
for {
text, err := editor.ReadLine(context.Background())
if err != nil {
fmt.Printf("ERR=%s\n", err.Error())
return
}
fields := strings.Fields(text)
if len(fields) <= 0 {
continue
}
cmd := exec.Command(fields[0], fields[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Run()
history.Add(text)
}
}
自定义补全示例
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/completion"
"github.com/nyaosorg/go-readline-ny/keys"
)
func mains() error {
var editor readline.Editor
editor.PromptWriter = func(w io.Writer) (int, error) {
return io.WriteString(w, "menu> ")
}
candidates := []string{"list", "say", "pewpew", "help", "exit", "Space Command"}
editor.BindKey(keys.CtrlI, &completion.CmdCompletionOrList2{
// 这里列出的字符将从补全中排除
Delimiter: "&|><;",
// 当候选包含空格时,用这些字符包围
Enclosure: `"'`,
// 当只有一个候选时附加的字符串
Postfix: " ",
// 用于列出候选的函数
Candidates: func(field []string) (forComp []string, forList []string) {
if len(field) <= 1 {
return candidates, candidates
}
return nil, nil
},
})
ctx := context.Background()
for {
line, err := editor.ReadLine(ctx)
if err != nil {
return err
}
fmt.Printf("TEXT=%#v\n", line)
}
return nil
}
func main() {
if err := mains(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
特性截图
许可证
MIT 许可证
更多关于golang支持Emacs键绑定和语法高亮的命令行编辑插件go-readline-ny的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复