Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions internal/adapter/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/kballard/go-shellquote"
"github.com/mattn/go-isatty"
executil "github.com/zk-org/zk/internal/util/exec"
"github.com/zk-org/zk/internal/util/opt"
osutil "github.com/zk-org/zk/internal/util/os"
Expand Down Expand Up @@ -35,11 +36,18 @@ func NewEditor(editor opt.String, shell string) (*Editor, error) {

// Open launches the editor with the notes at given paths.
func (e *Editor) Open(paths ...string) error {
// /dev/tty is restored as stdin, in case the user used a pipe to feed
// initial note content to `zk new`. Without this, Vim doesn't work
// properly in this case.
// See https://github.com/zk-org/zk/issues/4
cmd := executil.CommandFromString(e.shell, e.editor+" "+shellquote.Join(paths...)+CMD_SUFFIX)
// If stdin is a pipe, pipe it to the editor, so it can consume the output from
// that pipe. Allows providing input to commands like `zk new` when using Vim.
//
// Don't redirect a tty, since that messes up rendering of some TUI editors.
//
// See: https://github.com/zk-org/zk/issues/4
// See: https://github.com/zk-org/zk/pull/693
suffix := CMD_SUFFIX
if isatty.IsTerminal(os.Stdin.Fd()) {
suffix = ""
}
cmd := executil.CommandFromString(e.shell, e.editor+" "+shellquote.Join(paths...)+suffix)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
Loading