Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #1

Merged
merged 5 commits into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ go 1.12
require (
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936
github.com/shirou/gopsutil v2.18.12+incompatible
github.com/tennashi/tapiexec v0.0.0-20190615090639-f295aacd1cae
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 // indirect
)
12 changes: 11 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
github.com/shirou/gopsutil v2.18.12+incompatible h1:1eaJvGomDnH74/5cF4CTmTbLHAriGFsTZppLXDX93OM=
github.com/shirou/gopsutil v2.18.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/tennashi/tapiexec v0.0.0-20190615074702-3ec0f508af08 h1:U1EvUXEYp7csFhbVAQkaHcIVHHDW12VaYad42PK4osA=
github.com/tennashi/tapiexec v0.0.0-20190615074702-3ec0f508af08/go.mod h1:R+3xhELYgNnV/riJA5mmYOJk/8UheKTT/HYgGwV2FRk=
github.com/tennashi/tapiexec v0.0.0-20190615090639-f295aacd1cae h1:NlsfgXlbUWjrEuyeAvqKoGtAS+EDeWGffi9vzZsVzfQ=
github.com/tennashi/tapiexec v0.0.0-20190615090639-f295aacd1cae/go.mod h1:R+3xhELYgNnV/riJA5mmYOJk/8UheKTT/HYgGwV2FRk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872 h1:cGjJzUd8RgBw428LXP65YXni0aiGNA4Bl+ls8SmLOm8=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
116 changes: 89 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/shirou/gopsutil/process"
"github.com/tennashi/tapiexec"
)

var (
Expand All @@ -20,64 +20,126 @@ func main() {
flag.Parse()
args := flag.Args()

var vim *exec.Cmd
vimRuntime := os.Getenv("VIMRUNTIME")
if vimRuntime == "" {
vim = exec.Command("vim", args...)
err := runVim(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}

vimEnv := filepath.Dir(vimRuntime)
switch filepath.Base(vimEnv) {
case "vim":
srvName := os.Getenv("VIM_SERVERNAME")

if srvName == "" {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "open new file is not supported in vim8(server not started)")
err := runVimTapi(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if *tabFlg {
fmt.Fprintln(os.Stderr, "open the file in new tab is not supported in vim8(server not started)")
} else {
err := runVimCS(srvName, args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} else {
vim = exec.Command("echo", "-e", "\x1b]51;[\"drop\",\""+strings.Join(args, " ")+"\"]\x07")
}
break
}
path, err := evalVimPath()

os.Exit(0)

case "nvim":
err := runNvimNVR(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if len(args) == 0 {
if *tabFlg {
args = []string{"--servername", srvName, "--remote-send", `<C-\><C-N>:tabnew<CR>`}
} else {
args = []string{"--servername", srvName, "--remote-send", `<C-\><C-N>:new<CR>`}
}
} else {
if *tabFlg {
args = append([]string{"--servername", srvName, "--remote-tab"}, args...)
} else {
args = append([]string{"--servername", srvName, "--remote"}, args...)
}
os.Exit(0)
}
}

func runVim(args []string) error {
vim := exec.Command("vim", args...)

vim.Stdin = os.Stdin
vim.Stdout = os.Stdout
vim.Stderr = os.Stderr

return vim.Run()
}

func genResID() string {
return "response"
}

func runVimTapi(args []string) error {
resID := genResID()
if *tabFlg {
args = append([]string{"tabnew", resID}, args...)
} else {
args = append([]string{"split", resID}, args...)
}
tapi := tapiexec.CallAPI("Tapi_open_wait", args)

err := tapi.Run()
if err != nil {
return err
}
return tapiexec.WaitMsg(resID, "done")
}

func runVimCS(srvName string, args []string) error {
path, err := evalVimPath()
if err != nil {
return err
}

if len(args) == 0 {
if *tabFlg {
args = []string{"--servername", srvName, "--remote-send", `<C-\><C-N>:tabnew<CR>`}
} else {
args = []string{"--servername", srvName, "--remote-send", `<C-\><C-N>:new<CR>`}
}
} else {
if *tabFlg {
args = append([]string{"--servername", srvName, "--remote-tab-wait"}, args...)
} else {
args = append([]string{"--servername", srvName, "--remote-wait"}, args...)
}
vim = exec.Command(path, args...)

case "nvim":
}
vim := exec.Command(path, args...)
vim.Stdin = os.Stdin
vim.Stdout = os.Stdout
vim.Stderr = os.Stderr

return vim.Run()
}

func runNvimNVR(args []string) error {
if len(args) == 0 {
if *tabFlg {
args = []string{"--remote-send", `<C-\><C-N>:tabnew<CR>`}
} else {
args = []string{"--remote-send", `<C-\><C-N>:new<CR>`}
}
} else {
if *tabFlg {
args = append([]string{"--remote-tab-wait"}, args...)
} else {
args = append([]string{"--remote-wait"}, args...)
}
vim = exec.Command("nvr", args...)
}
vim := exec.Command("nvr", args...)

vim.Stdin = os.Stdin
vim.Stdout = os.Stdout
vim.Stderr = os.Stderr

vim.Run()
return vim.Run()
}

func evalVimPath() (string, error) {
Expand Down