Add support of ZK_EDITOR to set the editor

pull/6/head
Mickaël Menu 3 years ago
parent ca56b4b550
commit 14bf4d507c
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -30,7 +30,8 @@ func Edit(zk *zk.Zk, paths ...string) error {
// editor returns the editor command to use to edit a note.
func editor(zk *zk.Zk) opt.String {
return zk.Config.Tool.Editor.
return osutil.GetOptEnv("ZK_EDITOR").
Or(zk.Config.Tool.Editor).
Or(osutil.GetOptEnv("VISUAL")).
Or(osutil.GetOptEnv("EDITOR"))
}

@ -9,8 +9,23 @@ import (
"github.com/mickael-menu/zk/util/test/assert"
)
func TestEditorUsesUserConfigFirst(t *testing.T) {
os.Setenv("VISUAL", "editor")
func TestEditorUsesZkEditorFirst(t *testing.T) {
os.Setenv("ZK_EDITOR", "zk-editor")
os.Setenv("VISUAL", "visual")
os.Setenv("EDITOR", "editor")
zk := zk.Zk{Config: zk.Config{
Tool: zk.ToolConfig{
Editor: opt.NewString("custom-editor"),
},
}}
assert.Equal(t, editor(&zk), opt.NewString("zk-editor"))
}
func TestEditorFallsbackOnUserConfig(t *testing.T) {
os.Unsetenv("ZK_EDITOR")
os.Setenv("VISUAL", "visual")
os.Setenv("EDITOR", "editor")
zk := zk.Zk{Config: zk.Config{
Tool: zk.ToolConfig{
Editor: opt.NewString("custom-editor"),
@ -21,6 +36,7 @@ func TestEditorUsesUserConfigFirst(t *testing.T) {
}
func TestEditorFallsbackOnVisual(t *testing.T) {
os.Unsetenv("ZK_EDITOR")
os.Setenv("VISUAL", "visual")
os.Setenv("EDITOR", "editor")
zk := zk.Zk{}
@ -29,6 +45,7 @@ func TestEditorFallsbackOnVisual(t *testing.T) {
}
func TestEditorFallsbackOnEditor(t *testing.T) {
os.Unsetenv("ZK_EDITOR")
os.Unsetenv("VISUAL")
os.Setenv("EDITOR", "editor")
zk := zk.Zk{}
@ -37,6 +54,7 @@ func TestEditorFallsbackOnEditor(t *testing.T) {
}
func TestEditorWhenUnset(t *testing.T) {
os.Unsetenv("ZK_EDITOR")
os.Unsetenv("VISUAL")
os.Unsetenv("EDITOR")
zk := zk.Zk{}

Loading…
Cancel
Save