Skip to content

Commit 1dce015

Browse files
committed
Make ain respect VISUAL first and EDITOR second
So apparently $EDITOR is second in command and $VISUAL is first. Aincent stuff, but let's do what most programs do and respect the first over the second. Test plan: * Have $EDITOR set to vim. export VISUAL=code --wait and run ain template.ain!. Verify code opens and waits for code to close. * Unset VISUAL and export EDITOR=code --wait. Run ain with the same template and verify code opens and waits for editing. * Unset both VISUAL and EDITOR and verify vim opens as the default with the test-case above.
1 parent 6a54cb2 commit 1dce015

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,15 @@ Anything after a pound sign (#) is a comment and will be ignored.
209209

210210
Ain accepts one or more template-file(s) as a mandatory parameter. As sections combine or overwrite where it makes sense you can better organize API-calls into hierarchical structures with increasing specificity. An example would be setting the [[Headers]](#Headers), [[Backend]](#backend) and [[BackendOptions]](#BackendOptions) in a base template file and then specifying the specific [[Host]](#Host), [[Method]](#Method) and [[Body]](#Body) in several template files, one for each API-endpoint. You can even use an `alias` for things you will always set.
211211

212-
Adding an exclamation-mark (!) at the end of the template file name makes ain open the file in your `$EDITOR` (or vim if not set) so you can edit the template file. Any changes are not stored back into the template file and used only this invocation.
212+
Adding an exclamation-mark (!) at the end of the template file name makes ain open the file in your `$VISUAL` or `$EDITOR` or defaults to vim editor in that order so you can edit the template file. Any changes are not stored back into the template file and used only this invocation.
213213

214214
Example:
215215
```
216216
ain templates/get-blog-post.ain!
217217
```
218218

219-
Ain waits for the `$EDITOR` command to exit. Any terminal editor such as vim, emacs, nano etc will be fine. If your editor of choice forks (such as [vscode](https://code.visualstudio.com/) does by default) check if there's a flag stopping it from forking. For example to stop vscode from forking use the `--wait` [flag](https://code.visualstudio.com/docs/editor/command-line#_core-cli-options):
219+
Ain waits for the editor command to exit. Any terminal editor such as vim, emacs, nano etc will be fine. If your editor of choice forks (such as [vscode](https://code.visualstudio.com/) does by default) check if there's a flag stopping it from forking. For example to stop vscode from forking use the `--wait` [flag](https://code.visualstudio.com/docs/editor/command-line#_core-cli-options):
220+
220221
```
221222
export EDITOR="code --wait"
222223
```

internal/pkg/disk/read.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ import (
1414
const editFileSuffix = "!"
1515

1616
func captureEditorOutput(tempFile *os.File) (string, error) {
17-
editorEnvStr := os.Getenv("EDITOR")
17+
editorEnvVarName := "VISUAL"
18+
editorEnvStr := os.Getenv(editorEnvVarName)
19+
20+
if editorEnvStr == "" {
21+
editorEnvVarName = "EDITOR"
22+
editorEnvStr = os.Getenv(editorEnvVarName)
23+
}
24+
1825
if editorEnvStr == "" {
26+
editorEnvVarName = "vim"
1927
editorEnvStr = "vim"
2028
}
2129

2230
editorCmdAndArgs, err := utils.TokenizeLine(editorEnvStr)
2331
if err != nil {
24-
return "", errors.Wrap(err, "Cannot parse EDITOR environment variable")
32+
return "", errors.Wrapf(err, "Cannot parse $%s environment variable", editorEnvVarName)
2533
}
2634

2735
editorArgs := append(editorCmdAndArgs[1:], tempFile.Name())
@@ -38,7 +46,7 @@ func captureEditorOutput(tempFile *os.File) (string, error) {
3846

3947
err = cmd.Run()
4048
if err != nil {
41-
return "", errors.Wrapf(err, "Error running $EDITOR %s", cmd.String())
49+
return "", errors.Wrapf(err, "Error running $%s %s", editorEnvVarName, cmd.String())
4250
}
4351

4452
_, err = tempFile.Seek(0, 0)

0 commit comments

Comments
 (0)