zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.39k stars 1.16k forks source link

RunTermEmulator function doesn't seem to respect the "wait" flag #3243

Open dakennedyd opened 2 months ago

dakennedyd commented 2 months ago

When using the fzf plugin to open a file the plugin displays an annoying "Press enter to close" message before closing the internal terminal. The fzf plugin uses the "RunTermEmulator" function with the "wait" flag set to "false" which I assume it should make it close the internal terminal immediately but instead displays a message that says "Press enter to close" before closing.

Specifications

Version: 2.0.14-dev.131 Commit hash: acb0d763 Compiled on April 10, 2024 OS: Ubuntu 22.04 Terminal: xterm

dmaluka commented 2 months ago

I can reproduce this. From micro's code I see that the Press enter to close message actually indicates that an error occurred. So this message seems misleading, and moreover, it doesn't tell us which error occurred.

I've instrumented the code so it says which error is it:

diff --git a/internal/shell/terminal.go b/internal/shell/terminal.go
index 6fb1ce7f..5712efbd 100644
--- a/internal/shell/terminal.go
+++ b/internal/shell/terminal.go
@@ -2,6 +2,7 @@ package shell

 import (
    "bytes"
+   "fmt"
    "os/exec"
    "strconv"

@@ -97,7 +98,7 @@ func (t *Terminal) Start(execCmd []string, getOutput bool, wait bool, callback f
        for {
            err := Term.Parse()
            if err != nil {
-               Term.Write([]byte("Press enter to close"))
+               Term.Write([]byte(fmt.Sprintf("Press enter to close: %v", err)))
                screen.Redraw()
                break
            }

and I can see the error is:

Press enter to close: read /dev/ptmx: input/output error                                                                                                                                                           

RunTermEmulator() uses the https://github.com/zyedidia/terminal library, which opens /dev/ptmx using the https://github.com/creack/pty library, and reads characters from it. Apparently, when finishing running the fzf job, reading from /dev/ptmx fails with EIO for some reason.

We can try to debug it further.

Quick googling: https://www.google.com/search?q=%2Fdev%2Fptmx+eio

finds some possibly useful links: