Skip to content

Commit e64e2c1

Browse files
authored
feat!: support latest version of neovim (#1)
* feat: support logfile * feat: improve path handling * fix: buffer deleting * test: add unit tests BREAKING CHANGE: neovim now uses env var `NVIM` instead of `NVIM_LISTEN_ADDRESS`
1 parent 398e0bf commit e64e2c1

File tree

6 files changed

+289
-28
lines changed

6 files changed

+289
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616

1717
bin/
1818
testing/
19+
20+
.DS_Store
21+
log.txt

README.md

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# About
22

3-
The goal of `flatnvim` is to make it easy to get the most out of [Neovim](https://neovim.io/)'s
3+
The goal of `flatnvim` is to make it easy to use [Neovim](https://neovim.io/)'s
44
[terminal emulator](https://neovim.io/doc/user/nvim_terminal_emulator.html).
5-
When you open files from within the `Neovim` terminal, `flatnvim` will automatically add them
5+
When you open files from within the Neovim terminal, `flatnvim` will automatically add them
66
to the current instance instead of creating a new nested one.
7-
This makes it easy to use all your favorite programs naturally from within `Neovim`.
7+
Now you can easily use all your favorite command line programs inside Neovim.
88

99
# Usage
1010

@@ -18,34 +18,69 @@ cd flatnvim
1818
```
1919

2020
## Environment Variables
21-
There are 3 environment variable to use with `flatnvim`.
21+
There are 4 environment variables `flatnvim` can use.
2222
Add them to the config file for your shell (.bashrc, .profile, .zprofile, etc).
2323

2424
### Required: set the path to the actual editor.
2525
```sh
2626
export FLATNVIM_EDITOR="nvim"
2727
```
28-
- When `flatnvim` is called from your regular terminal, it will just pass through to this editor.
28+
- When `flatnvim` is called from your regular terminal, it will just pass arguments to this editor.
2929

3030
### Recommended: set the path to the `flatnvim` binary as your default terminal editor.
3131
```sh
3232
export EDITOR="$HOME/repos/flatnvim/bin/flatnvim"
3333
```
3434
- If you don't know what this should be, use the path printed by the build.sh script.
35-
- In addition, I personally make aliases to this: `alias vim="$EDITOR`
35+
- In addition, you can make an alias to `flatnvim`: `alias vim="$EDITOR`
36+
37+
### Optional: set a log file for `flatnvim` to use.
38+
```sh
39+
export FLATNVIM_LOGFILE="$HOME/repos/flatnvim/log.txt"
40+
```
41+
- If this is not set, any error messages are just printed.
3642

3743
### Optional: set an extra Neovim command to be executed when preventing nested instances.
3844
```sh
39-
export FLATNVIM_EXTRA_COMMAND="if exists(':AirlineRefresh') == 2 | AirlineRefresh | endif"
45+
export FLATNVIM_EXTRA_COMMAND="echo 'it works' | sleep"
4046
```
41-
- I use the command above to refresh my status bar.
47+
- Just in case you want `flatnvim` to do something extra before it opens files in the parent instance.
4248

4349

4450
## Neovim Configs
4551

4652
### Recommended: disable status line when in terminal mode.
47-
```sh
53+
```viml
4854
autocmd TermOpen * setlocal laststatus=0 noshowmode noruler
4955
\| autocmd TermClose * setlocal laststatus=2 showmode ruler
5056
5157
```
58+
59+
### Recommended: skip exit prompt if terminal commands were successful.
60+
```viml
61+
autocmd TermClose * if !v:event.status | exe 'bdelete! '..expand('<abuf>') | endif
62+
```
63+
64+
### Optional: fix issue with Airline that occurs when window focus is lost.
65+
```viml
66+
autocmd TermLeave * AirlineRefresh
67+
```
68+
69+
70+
## Neovim Function
71+
72+
Adding this function to your Neovim config makes it easier to access the terminal and specific command line programs.
73+
74+
```viml
75+
function! TempTerm(...)
76+
let command = get(a:, 1)
77+
exe "terminal ".command
78+
return ""
79+
endfunction
80+
```
81+
82+
```viml
83+
nnoremap <silent> gt :call TempTerm(" ")<CR>
84+
nnoremap <silent> gL :call TempTerm("lazygit")<CR>
85+
nnoremap <silent> gl :call TempTerm("lf")<CR>
86+
```

go.mod

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module flatnvim
22

3-
go 1.17
3+
go 1.19
44

5-
require github.com/neovim/go-client v1.1.7
5+
require (
6+
github.com/neovim/go-client v1.2.1
7+
github.com/stretchr/testify v1.8.1
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

go.sum

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
github.com/neovim/go-client v1.1.3 h1:RrjcL8ZdFNtI+w77cp0uWyHZBu2X9fHTvztipdyoZB8=
2-
github.com/neovim/go-client v1.1.3/go.mod h1:R9QUduDri8OKS78u/rAvFZmaw6pPfdb+MiKq0JYnZ+c=
3-
github.com/neovim/go-client v1.1.7 h1:nQY7XwQwCQQ4DtUkXcTsxAlCFgS4D6mi0KMqrHvY0ew=
4-
github.com/neovim/go-client v1.1.7/go.mod h1:lec+fTkbKMMQEcbP9SdiCteZeAhdchUsw6I1MGcqBkc=
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/neovim/go-client v1.2.1 h1:kl3PgYgbnBfvaIoGYi3ojyXH0ouY6dJY/rYUCssZKqI=
5+
github.com/neovim/go-client v1.2.1/go.mod h1:EeqCP3z1vJd70JTaH/KXz9RMZ/nIgEFveX83hYnh/7c=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
11+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
13+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
14+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@ import (
44
"log"
55
"os"
66
"os/exec"
7+
"path/filepath"
8+
"strings"
79

810
"github.com/neovim/go-client/nvim"
911
)
1012

1113
func main() {
12-
log.SetFlags(0)
13-
1414
const version string = "v0.2.1"
15+
16+
log.SetFlags(log.Ldate | log.Ltime)
17+
log.SetPrefix(version + " ")
18+
19+
logfile := os.Getenv("FLATNVIM_LOGFILE")
20+
if logfile != "" {
21+
if f, err := os.OpenFile(logfile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0o664); err == nil {
22+
defer f.Close()
23+
log.SetOutput(f)
24+
} else {
25+
log.Printf("FLATNVIM_LOGFILE is set to %v but cannot be opened: %v\n", logfile, err)
26+
}
27+
}
28+
1529
err := os.Setenv("FLATNVIM_VERSION", version)
1630
if err != nil {
17-
log.Fatalf("%v: unable to set FLATNVIM_VERSION environment variable\n", version)
31+
log.Printf("unable to set FLATNVIM_VERSION environment variable: %v\n", err)
1832
}
1933

20-
addr := os.Getenv("NVIM_LISTEN_ADDRESS")
34+
addr := os.Getenv("NVIM")
2135
if addr == "" {
2236
editor := os.Getenv("FLATNVIM_EDITOR")
2337

2438
path, err := exec.LookPath(editor)
2539
if err != nil {
26-
log.Fatalf("%v: command '%v' from FLATNVIM_EDITOR is not in $PATH\n", version, editor)
40+
log.Panicf("command '%v' from FLATNVIM_EDITOR is not in $PATH: %v\n", editor, err)
2741
}
2842

2943
cmd := exec.Command(path, os.Args[1:]...)
@@ -32,35 +46,81 @@ func main() {
3246
cmd.Stderr = os.Stderr
3347

3448
if err := cmd.Run(); err != nil {
35-
log.Fatalf("%v: %v\n", version, err)
49+
log.Panicln(err)
3650
}
37-
os.Exit(0)
51+
52+
return
3853
}
3954

4055
files := os.Args[1:]
4156
if len(files) == 0 {
42-
log.Fatalf("%v: no arguments given\n", version)
57+
log.Panicln("no arguments given")
4358
}
4459

4560
v, err := nvim.Dial(addr)
4661
if err != nil {
47-
log.Fatalf("%v: %v\n", version, err)
62+
log.Panicf("unable to connect to parent nvim instance: %v\n", err)
4863
}
4964
defer v.Close()
5065

5166
b := v.NewBatch()
67+
b.Command("let flatnvim_buf=bufname()")
68+
69+
var curDir string
70+
if dir, err := os.Getwd(); err == nil {
71+
curDir = dir + string(os.PathSeparator)
72+
} else {
73+
log.Printf("could not get current directory: %v\n", err)
74+
}
75+
76+
var vimDir string
77+
if dir, err := v.Exec("pwd", true); err == nil {
78+
vimDir = dir + string(os.PathSeparator)
79+
} else {
80+
log.Printf("could not get directory of nvim: %v\n", err)
81+
}
82+
83+
homeDir, err := os.UserHomeDir()
84+
if err != nil {
85+
log.Printf("unable to get user home directory: %v\n", err)
86+
}
87+
5288
for _, file := range files {
53-
b.Command(":e " + file)
89+
if file == "--" {
90+
continue
91+
}
92+
93+
path := trimPath(file, curDir, vimDir, homeDir)
94+
b.Command("e " + path)
5495
}
55-
b.Command(":b term://")
56-
b.Command(":bw!")
96+
97+
b.Command("exe 'bd! '.flatnvim_buf")
5798

5899
extraCmd := os.Getenv("FLATNVIM_EXTRA_COMMAND")
59100
if extraCmd != "" {
60101
b.Command(extraCmd)
61102
}
62103

63104
if err := b.Execute(); err != nil {
64-
log.Fatalf("%v: %v\n", version, err)
105+
log.Panicf("unable to execute nvim commands: %v\n", err)
106+
}
107+
}
108+
109+
func trimPath(path, curDir, vimDir, homeDir string) string {
110+
if len(curDir) > 0 && !filepath.IsAbs(path) {
111+
path = curDir + path
65112
}
113+
114+
if len(vimDir) > 0 {
115+
path = strings.TrimPrefix(path, vimDir)
116+
}
117+
118+
if len(homeDir) > 0 && strings.HasPrefix(path, homeDir) {
119+
path = strings.Replace(path, homeDir, "~", 1)
120+
}
121+
122+
path = strings.ReplaceAll(path, "/./", "/")
123+
path = strings.TrimPrefix(path, "./")
124+
125+
return path
66126
}

0 commit comments

Comments
 (0)