You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a timeout to cursor_pos and warn if terminal does not respond to cursor position query #674
If terminal emulator does not respond to cursor position query "\e[6n", Reline will wait forever with ▽ displayed in terminal.
Reline can add a timeout to at least work in such case. Warning message will help debugging.
Example:
Forgot to set TERM=dumb in terminal emulator that does not support cursor position report
changed the title [-]Add a timeout to `cursor_pos` and warn if terminal does not respond to cursor position report[/-][+]Add a timeout to `cursor_pos` and warn if terminal does not respond to cursor position query[/+]on Apr 7, 2024
$ podman run --rm -ti -v $PWD:/app ruby:3.2 ruby /app/bug.rb "Readline\r\n"Readline"Readline\r\n\e[?2004hPrompt: "ReadlinePrompt: "Readline\r\n\e[?2004hPrompt: hello\r\n\e[?2004l\rHELLO\r\n"ReadlinePrompt: helloHELLOOutput:ReadlinePrompt: helloHELLO
$ podman run --rm -ti -v $PWD:/app ruby:3.3 ruby /app/bug.rb "Reline\r\n"Reline"Reline\r\n\e[1G\xE2\x96\xBD\e[6n"Reline▽^[[37;2R^C/app/bug.rb:14:in `readpartial': Interrupt from /app/bug.rb:14:in `block (2 levels) in <main>' from <internal:kernel>:187:in `loop' from /app/bug.rb:13:in `block in <main>' from /app/bug.rb:12:in `spawn' from /app/bug.rb:12:in `<main>'
As you mentioned, we can see "\e[6n", then we can even see the result from the terminal ^[[37;2R, meaning line 37, col 2, but it's not read (readable?) by Reline.
Using TERM=dumb shows a different bug where the output is written multiple times, but at least it works:
@tompng Do you know if there's a way to work around this without using TERM=dumb? I don't really understand how terminals work deeply, is this a bug from PTY.spawn? Is there a way to get PTY.spawn to write to the PTY what Reline expects at the other end of the device?
In my understanding, using PTY means that the program is expected to act like a terminal emulator.
PTY.spawn("ruby",__FILE__,"prompt")do |read,write,pid|
# Spawned process can check if it is executed in terminal emulator by `IO#tty?`. When spawned by PTY, it returns true.loopdos=read.readpartial(4096)ifs.include?("\e[6n")# If terminal emulator(==this program) receives "\e[6n", terminal emulator should report cursor position# Since this program is not a full terminal emulator, report dummy cursor positioncursor_col,cursor_row=1,1# Or you can report real cursor position retrieved from current terminal, virtual terminal library, another process, from remote, whatever. PTY doesn't support this part.write.write"\e[#{cursor_row};#{cursor_col}R"endoutput += sendend
This is too hard, so there is TERM=dumb to avoid it.
If you don't need to provide tty to the spawned process, you can use IO.popen(command, 'r+') instead.
Activity
[-]Add a timeout to `cursor_pos` and warn if terminal does not respond to cursor position report[/-][+]Add a timeout to `cursor_pos` and warn if terminal does not respond to cursor position query[/+]etiennebarrie commentedon Apr 10, 2024
Ah I was about to create an issue for this. Here's a repro script:
You can test this with a real terminal (podman can be replaced by docker):
But if we use
PTY.spawn, it waits forever:As you mentioned, we can see "\e[6n", then we can even see the result from the terminal
^[[37;2R, meaning line 37, col 2, but it's not read (readable?) by Reline.Using
TERM=dumbshows a different bug where the output is written multiple times, but at least it works:@tompng Do you know if there's a way to work around this without using
TERM=dumb? I don't really understand how terminals work deeply, is this a bug fromPTY.spawn? Is there a way to getPTY.spawnto write to the PTY what Reline expects at the other end of the device?tompng commentedon Apr 10, 2024
In my understanding, using PTY means that the program is expected to act like a terminal emulator.
This is too hard, so there is
TERM=dumbto avoid it.If you don't need to provide tty to the spawned process, you can use
IO.popen(command, 'r+')instead.Fix readline EOF: tcflush stdin before each Readline() call (#334)