From a65a01e099b2800c207b59a1cc17a269de0e44ea Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 1 Nov 2026 11:06:40 +0530 Subject: [PATCH] Cleanup previous PR Keep edit_loop free of side effects: it now reports whether the editor was started and edit_and_upload decides whether to keep the local copy. Print the recovery path verbatim instead of Go-quoted. --- kittens/remote_file/actions.go | 31 +++++++++++++++++++------------ kittens/remote_file/main.go | 2 +- kittens/remote_file/ssh_test.go | 3 ++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/kittens/remote_file/actions.go b/kittens/remote_file/actions.go index 33e06829b..f51cda03d 100644 --- a/kittens/remote_file/actions.go +++ b/kittens/remote_file/actions.go @@ -162,10 +162,23 @@ func get_editor() []string { return []string{"vim"} } -func edit_loop(master *ControlMaster, editor []string) (err error) { +// edit_and_upload runs the edit loop and, if the final upload fails after the +// editor was started, keeps the local copy so the user's edits are not lost. +func edit_and_upload(master *ControlMaster, editor []string) error { + editor_started, err := edit_loop(master, editor) + if err != nil && editor_started { + master.keep_local_copy = true + return fmt.Errorf("%w\nYour edited file is saved at: %s", err, master.Dest) + } + return err +} + +// edit_loop returns whether the editor was started, so callers can tell +// failures before editing from failures to upload the edits. +func edit_loop(master *ControlMaster, editor []string) (editor_started bool, err error) { st, err := os.Stat(master.Dest) if err != nil { - return err + return false, err } mtime := st.ModTime() // Mirrors main.py handle_action's edit branch: reset the terminal (clear @@ -176,14 +189,8 @@ func edit_loop(master *ControlMaster, editor []string) (err error) { cmd := exec.Command(argv[0], argv[1:]...) cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr if err := cmd.Start(); err != nil { - return err + return false, err } - defer func() { - master.keep_local_copy = err != nil - if err != nil { - err = fmt.Errorf("%w\nYour edited file is saved at: %q", err, master.Dest) - } - }() done := make(chan error, 1) go func() { done <- cmd.Wait() }() for { @@ -192,11 +199,11 @@ func edit_loop(master *ControlMaster, editor []string) (err error) { reset_terminal() if master.IsAlive() { if err := master.Upload(false); err != nil { - return fmt.Errorf("failed to upload %s: %w", master.remote_path, err) + return true, fmt.Errorf("failed to upload %s: %w", master.remote_path, err) } - return nil + return true, nil } - return fmt.Errorf("failed to upload %s, SSH master process died", master.remote_path) + return true, fmt.Errorf("failed to upload %s, SSH master process died", master.remote_path) case <-time.After(100 * time.Millisecond): if st, err := os.Stat(master.Dest); err == nil && st.ModTime().After(mtime) { mtime = st.ModTime() diff --git a/kittens/remote_file/main.go b/kittens/remote_file/main.go index d0a2c047f..4c943da80 100644 --- a/kittens/remote_file/main.go +++ b/kittens/remote_file/main.go @@ -352,7 +352,7 @@ func handle_action(action string, opts *Options) (result string, err error) { master_show_error(master, "Failed to download "+remote_path) return "", nil } - if err := edit_loop(master, get_editor()); err != nil { + if err := edit_and_upload(master, get_editor()); err != nil { master_show_error(master, err.Error()) } case "save": diff --git a/kittens/remote_file/ssh_test.go b/kittens/remote_file/ssh_test.go index d97feee57..7e439aa57 100644 --- a/kittens/remote_file/ssh_test.go +++ b/kittens/remote_file/ssh_test.go @@ -129,6 +129,7 @@ func TestPythonSplitExt(t *testing.T) { } } } + func TestRemoteEditFinalUpload(t *testing.T) { const ssh_script = `#!/bin/sh for arg in "$@"; do @@ -194,7 +195,7 @@ printf '%s' "$1" > "$KITTY_TEST_EDITED_PATH" } err := func() error { defer master.Close() - return edit_loop(master, []string{editor}) + return edit_and_upload(master, []string{editor}) }() if (err != nil) != tc.want_error { t.Fatalf("edit_loop error: %v, want error: %v", err, tc.want_error)