From 14bf4d507c3a47a5fca0600b31720acf2a704445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Sat, 20 Feb 2021 21:45:36 +0100 Subject: [PATCH] Add support of ZK_EDITOR to set the editor --- core/note/edit.go | 3 ++- core/note/edit_test.go | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/core/note/edit.go b/core/note/edit.go index ea7182d..3f4aa12 100644 --- a/core/note/edit.go +++ b/core/note/edit.go @@ -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")) } diff --git a/core/note/edit_test.go b/core/note/edit_test.go index 462291a..12a48ee 100644 --- a/core/note/edit_test.go +++ b/core/note/edit_test.go @@ -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{}