-
Notifications
You must be signed in to change notification settings - Fork 3
/
example4.go
83 lines (73 loc) · 1.69 KB
/
example4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//go:build run
// +build run
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"github.com/mattn/go-colorable"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/keys"
)
func startEditor(source string) (string, error) {
textEditor, ok := os.LookupEnv("EDITOR")
if !ok {
return source, errors.New("$EDITOR is not defined")
}
fd, err := os.CreateTemp("", "example3")
if err != nil {
return source, err
}
io.WriteString(fd, source)
fd.Close()
fname := fd.Name()
defer os.Remove(fname)
cmd := exec.Command(textEditor, fname)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return source, err
}
update, err := os.ReadFile(fname)
if err != nil {
return source, err
}
update = bytes.TrimSuffix(update, []byte{'\n'})
update = bytes.TrimSuffix(update, []byte{'\r'})
return string(update), nil
}
func cmdStartEditor(ctx context.Context, B *readline.Buffer) readline.Result {
result, err := startEditor(B.String())
if err != nil {
return readline.CONTINUE
}
B.Buffer = B.Buffer[:0]
B.InsertString(0, result)
B.Cursor = len(B.Buffer)
B.RepaintAll()
return readline.CONTINUE
}
func main() {
var editor readline.Editor
editor.Writer = colorable.NewColorableStdout()
editor.PromptWriter = func(w io.Writer) (int, error) {
return io.WriteString(w, "\r> ")
}
enter_status := 0
editor.BindKey(keys.Escape, &readline.GoCommand{
Name: "START_EDITOR",
Func: cmdStartEditor,
})
text, err := editor.ReadLine(context.Background())
fmt.Printf("ENTER_STATUS=%d\n", enter_status)
if err != nil {
fmt.Printf("ERR=%s\n", err.Error())
} else {
fmt.Printf("TEXT=%s\n", text)
}
}