-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (176 loc) · 4.03 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/mitchellh/cli"
"github.com/neovim/go-client/nvim"
)
func main() {
os.Exit(run())
}
func run() int {
addr := os.Getenv("NVIM")
if addr == "" {
addr = os.Getenv("NVIM_LISTEN_ADDRESS")
}
if addr == "" {
fmt.Fprintln(os.Stderr, "NVIM or NVIM_LISTEN_ADDRESS environment variable must be set")
return 1
}
nv, err := nvim.Dial(addr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer nv.Close()
c := cli.NewCLI("nvc", "0.0.1")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
"ex": func() (cli.Command, error) {
return exCommand{
nv: nv,
}, nil
},
"openwin": func() (cli.Command, error) {
return openWinCommand{
nv: nv,
}, nil
},
}
es, err := c.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
return es
}
type exCommand struct {
nv *nvim.Nvim
}
func (exCommand) Synopsis() string {
return "execute command"
}
func (exCommand) Help() string {
return `Execute arguments as NeoVim command. No need to prefix ':'.
EXAMPLES:
$ nvc ex echo \'abc\'
It executes ":echo 'abc'" in NeoVim so that NeoVim shows "abc" in status line.
$ nvc ex e a.txt
It executes ":e a.txt" in NeoVim so that NeoVim opens "a.txt" in current buffer.
`
}
func (c exCommand) Run(args []string) int {
if len(args) < 2 {
fmt.Fprintln(os.Stderr, "missing command to execute as argument")
return 1
}
err := c.nv.Command(strings.Join(args, " "))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}
type openWinCommand struct {
nv *nvim.Nvim
}
func (openWinCommand) Synopsis() string { return "open window" }
func (openWinCommand) Help() string {
return `Open Window. See https://neovim.io/doc/user/api.html#nvim_open_win() for detail.
The width and height should be specified as WxH.
EXAMPLES:
$ nvc openwin 30x5 --relative cursor --row 3 --col 3
`
}
func (c openWinCommand) Run(args []string) int {
fs := flag.NewFlagSet("open-window", flag.ExitOnError)
var bufN, winN int
var enter, focusable, external bool
var relative, anchor string
var row, col int
fs.IntVar(&bufN, "buffer", 0, "")
fs.BoolVar(&enter, "enter", true, "")
fs.StringVar(&relative, "relative", "", "")
fs.IntVar(&winN, "win", 0, "")
fs.StringVar(&anchor, "anchor", "", "")
fs.IntVar(&row, "row", 0, "")
fs.IntVar(&col, "col", 0, "")
fs.BoolVar(&focusable, "focusable", false, "")
fs.BoolVar(&external, "external", false, "")
var pargs []string
for {
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "Invalid argument: %v\n", err)
return 1
}
if fs.NArg() == 0 {
break
}
pargs = append(pargs, args[0])
args = args[1:]
}
var buf nvim.Buffer
if isSet(fs, "buffer") {
var err error
buf, err = c.nv.CurrentBuffer()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not get the current buffer: %v\n", err)
}
} else {
buf = nvim.Buffer(bufN)
}
if len(pargs) < 1 {
fmt.Fprintln(os.Stderr, "Please specify the width and height as WxH")
return 1
}
if len(pargs) > 1 {
fmt.Fprintln(os.Stderr, "Too many positional arguments")
return 1
}
var width, height int
if _, err := fmt.Sscanf(pargs[0], "%dx%d", &width, &height); err != nil {
fmt.Fprintf(os.Stderr, "Could not parse the width and height: %v\n", err)
return 1
}
config := new(nvim.WindowConfig)
config.Width = width
config.Height = height
if isSet(fs, "relative") {
config.Relative = relative
}
if isSet(fs, "win") {
config.Win = nvim.Window(winN)
}
if isSet(fs, "anchor") {
config.Anchor = anchor
}
if isSet(fs, "row") {
config.Row = row
}
if isSet(fs, "col") {
config.Col = col
}
if isSet(fs, "focusable") {
config.Focusable = focusable
}
if isSet(fs, "external") {
config.External = external
}
w, err := c.nv.OpenWindow(buf, enter, config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
fmt.Printf("Window %d is successfully opened.\n", w)
return 0
}
func isSet(fs *flag.FlagSet, name string) bool {
var set bool
fs.Visit(func(f *flag.Flag) {
if f.Name == name {
set = true
}
})
return set
}