-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
123 lines (104 loc) · 2.48 KB
/
dialer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package nvim
import (
"bytes"
"fmt"
"io"
"log"
"net"
"net/rpc"
"os"
"os/exec"
"reflect"
"github.com/davecgh/go-spew/spew"
"github.com/ugorji/go/codec"
)
type connType int
const (
CONN_NET connType = iota
CONN_STD
)
// WRCloser r/w wrapper
type WRCloser struct {
r io.ReadCloser
w io.WriteCloser
}
func NewWRCloser(r io.ReadCloser, w io.WriteCloser) *WRCloser {
return &WRCloser{r, w}
}
func (b *WRCloser) Read(p []byte) (n int, err error) {
// log.Printf("reading: %#v", p)
fmt.Printf("read: %#+v", p)
spew.Dump("reading", p)
return b.r.Read(p)
}
func (b *WRCloser) Write(data []byte) (n int, err error) {
// log.Printf("writing: %#v", data)
fmt.Printf("read: %#+v", data)
spew.Dump("writing: ", data)
return b.w.Write(data)
}
func (b *WRCloser) Close() error {
err := b.r.Close()
if err != nil {
return err
}
err = b.w.Close()
if err != nil {
return err
}
return nil
}
type buffer struct {
bytes.Buffer
}
// Add a Close method to our buffer so that we satisfy io.ReadWriteCloser.
func (b *buffer) Close() error {
b.Buffer.Reset()
return nil
}
func Dial(network connType, addr string) (*Vim, error) {
var conn io.ReadWriteCloser
var err error
switch network {
case CONN_NET:
conn, err = net.Dial("unix", addr)
if err != nil {
// return nil, errors.New(fmt.Sprintf("Fail connecting to Nvim: %s", err))
log.Fatalf("Fail connecting to Nvim: %s", err)
}
case CONN_STD:
cmd := exec.Command("nvim", "--embed")
stdin, err := cmd.StdinPipe()
if err != nil {
// return nil, errors.New(fmt.Sprintf("Fail getting stdin: %s", err))
log.Fatalf("Fail getting stdin: %s", err)
}
// defer stdin.Close() // do i need to close it here?
stdout, err := cmd.StdoutPipe()
if err != nil {
// return nil, errors.New(fmt.Sprintf("Fail getting stdout: %s", err))
log.Fatalf("Fail getting stdout: %s", err)
}
// defer stdout.Close()
cmd.Stderr = os.Stderr
conn = NewWRCloser(stdout, stdin)
// conn = &buffer{}
// TODO check this
err = cmd.Start()
if err != nil {
log.Fatal("cmd Start: ", err)
}
}
var h codec.MsgpackHandle
h.RawToString = true
h.WriteExt = true
h.SetExt(reflect.TypeOf(BufferIdentifier{}), Buffer_TAG, &nvimExt{})
h.SetExt(reflect.TypeOf(WindowIdentifier{}), Window_TAG, &nvimExt{})
h.SetExt(reflect.TypeOf(TabpageIdentifier{}), Tabpage_TAG, &nvimExt{})
rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, &h)
client := rpc.NewClientWithCodec(rpcCodec)
// TODO handle errors correctly
return &Vim{
client,
}, nil
}