Skip to content

Commit 8b00a72

Browse files
committed
feat: create flatnvim
1 parent 41dfb6a commit 8b00a72

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
bin

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
# flatnvim
1+
# About
2+
3+
The goal of `flatnvim` is to make it easy to get the most out of [Neovim](https://neovim.io/)'s
4+
[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
6+
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`.
8+
9+
# Usage
10+
11+
### Clone & Build
12+
You'll first need the `flatnvim` binary on you system.
13+
14+
```sh
15+
git clone https://github.com/adamtabrams/flatnvim.git
16+
cd flatnvim
17+
./build.sh
18+
```
19+
20+
### Environment Variables
21+
There are 3 environment variable to use with `flatnvim`
22+
Add them to the config file for your shell (.bashrc, .profile, .zprofile, etc)
23+
24+
##### Required: set path to the actual editor.
25+
```sh
26+
export FLATNVIM_EDITOR="nvim"
27+
```
28+
- When `flatnvim` is called from your regular terminal, it will just pass through to this editor.
29+
30+
##### Recommended: set the path to the `flatnvim` binary as your default terminal editor.
31+
```sh
32+
export EDITOR="$HOME/repos/flatnvim/bin/flatnvim"
33+
```
34+
- 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`
36+
37+
##### Optional: set an extra nvim command to be executed when preventing nested instances.
38+
```sh
39+
export FLATNVIM_EXTRA_COMMAND="if exists(':AirlineRefresh') == 2 | AirlineRefresh | endif"
40+
```
41+
- I use the command above to refresh my status bar.

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
go build -o bin/ || { echo "failure"; exit 1; }
4+
echo "success"
5+
echo "path to the flatnvim binary: $PWD/bin/flatnvim"

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module flatnvim
2+
3+
go 1.17
4+
5+
require github.com/neovim/go-client v1.1.7

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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=

main.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/exec"
7+
8+
"github.com/neovim/go-client/nvim"
9+
)
10+
11+
func main() {
12+
log.SetFlags(0)
13+
14+
addr := os.Getenv("NVIM_LISTEN_ADDRESS")
15+
if addr == "" {
16+
editor := os.Getenv("FLATNVIM_EDITOR")
17+
18+
path, err := exec.LookPath(editor)
19+
if err != nil {
20+
log.Fatalf("command '%v' set with environment variable FLATNVIM_EDITOR is not in $PATH\n", editor)
21+
}
22+
23+
cmd := exec.Command(path, os.Args[1:]...)
24+
cmd.Stdin = os.Stdin
25+
cmd.Stdout = os.Stdout
26+
cmd.Stderr = os.Stderr
27+
28+
if err := cmd.Run(); err != nil {
29+
log.Fatal(err)
30+
}
31+
os.Exit(0)
32+
}
33+
34+
files := os.Args[1:]
35+
if len(files) == 0 {
36+
log.Fatal("no arguments given")
37+
}
38+
39+
v, err := nvim.Dial(addr)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
defer v.Close()
44+
45+
b := v.NewBatch()
46+
for _, file := range files {
47+
b.Command(":e " + file)
48+
}
49+
b.Command(":b term://")
50+
b.Command(":bw!")
51+
52+
extraCmd := os.Getenv("FLATNVIM_EXTRA_COMMAND")
53+
if extraCmd != "" {
54+
b.Command(extraCmd)
55+
}
56+
57+
if err := b.Execute(); err != nil {
58+
log.Fatal(err)
59+
}
60+
}

0 commit comments

Comments
 (0)