-
Notifications
You must be signed in to change notification settings - Fork 4
/
process.go
132 lines (108 loc) · 2.2 KB
/
process.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
124
125
126
127
128
129
130
131
132
package nmux
import (
"errors"
"fmt"
"io"
"log"
"os"
"sync"
"github.com/neovim/go-client/nvim"
"github.com/tweekmonster/nmux/screen"
)
const (
MsgRequest = 0
MsgReply
MsgNotification
)
var ErrFirstArgString = errors.New("first item must be a string")
var ErrEmpty = errors.New("empty")
// TODO: Multiple nvim process management.
var procs struct {
mu sync.Mutex
id int
procs []*Process
}
func addProcess(proc *Process) {
procs.mu.Lock()
defer procs.mu.Unlock()
procs.procs = append(procs.procs, proc)
}
func removeProcess(proc *Process) {
procs.mu.Lock()
defer procs.mu.Unlock()
curProcs := procs.procs
procs.procs = procs.procs[0:0]
for _, p := range curProcs {
if p != proc {
procs.procs = append(procs.procs, p)
}
}
}
type Process struct {
*screen.Screen
ID int
Deadman <-chan int
nvim *nvim.Nvim
}
type msg struct {
code uint8
name string
args []interface{}
}
func NewProcess(cwd string, width, height int) (*Process, error) {
n, err := nvim.NewEmbedded(&nvim.EmbedOptions{
// Path: "/home/tallen/tmp/nvim/bin/nvim",
Env: os.Environ(),
Dir: cwd,
Logf: func(msg string, args ...interface{}) {
log.Println("Embedded Log:", msg, args)
},
})
if err != nil {
return nil, err
}
scr := screen.NewScreen(width, height)
n.RegisterHandler("redraw", scr.RedrawHandler)
procs.id++
deadman := make(chan int)
proc := &Process{
ID: procs.id,
nvim: n,
Deadman: deadman,
Screen: scr,
}
addProcess(proc)
go func() {
if err := proc.nvim.Serve(); err != nil {
fmt.Println("RPC Err:", err)
}
proc.nvim = nil
removeProcess(proc)
close(deadman)
fmt.Println("Dead")
}()
if err := n.AttachUI(proc.Size.X, proc.Size.Y, map[string]interface{}{
"rgb": true,
"popupmenu_external": false,
}); err != nil {
return nil, err
}
return proc, nil
}
func (p *Process) Attach(w io.Writer) error {
p.Screen.SetSink(w)
return nil
}
func (p *Process) Detach() error {
p.Screen.SetSink(nil)
return nil
}
func (p *Process) IsRunning() bool {
return p.nvim != nil
}
func (p *Process) Input(keys string) (int, error) {
return p.nvim.Input(keys)
}
func (p *Process) Resize(w, h int) error {
return p.nvim.TryResizeUI(w, h)
}