Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot backspace during interactive stdin #33

Open
glacials opened this issue Aug 15, 2023 · 1 comment
Open

Cannot backspace during interactive stdin #33

glacials opened this issue Aug 15, 2023 · 1 comment

Comments

@glacials
Copy link

glacials commented Aug 15, 2023

Hi, thanks for all your hard work on gow! It is an amazingly simple tool and it has saved me so many development hours during the time that I've used it.

I have an issue where my program has an interactive flow where it prompts the user for input at certain points, and I'm trying to run this flow using gow when doing manual testing.

Everything works perfectly, except while inputting text I cannot backspace. See video:

Screen.Recording.2023-08-15.at.13.32.38.mov

I'm on macOS 13.5 and have tried this in iTerm and Terminal.app. As seen in the video it's not just visual, the text ultimately entered has whitespace added onto it instead of characters removed from it. Is this intentional?

Thanks for any help!

EDIT: For convenience, here's the code used in the screen recording:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	fmt.Print("Enter some text: ")
	in := bufio.NewReader(os.Stdin)
	line, err := in.ReadString('\n')
	if err != nil {
		panic(fmt.Errorf("cannot read input: %w", err))
	}
	fmt.Println("You inputted:", line)
}
@mitranim
Copy link
Owner

mitranim commented Aug 17, 2023

See the following changes: 87df6e4...478cc66.

To summarize, there were multiple issues:

  • By default, gow switches the terminal from "cooked mode" to "raw mode", violating many common assumptions about terminal input, such as line buffering. This can be disabled in gow with -r=false.

  • In raw mode, gow does not support some editing functionality such as character deletion.

  • In non-raw mode, everything should have worked fine, but using your code sample with some modifications, I found that piping stdin to the sub-process did not work properly. The latest of the linked commits seems to address this problem.

Raw mode allows gow to support hotkeys, but it also makes gow responsible for handling ASCII control codes. Currently it handles control codes related to process state, such as various forms of shutdown and restart, but does not handle the ASCII delete code. It also doesn't handle various ANSI escape codes necessary for "proper" text editing such as cursor movement and more.

When the terminal operates in default "cooked mode" (-r=false in gow), the terminal itself handles common editing operations such as deleting characters before sending the resulting line to the sub-process. When using raw mode (default in gow), the program is responsible for interpreting ASCII delete by removing characters from its own text buffer or whatever, which also requires maintaining the cursor position and interpreting ANSI escape codes for cursor movement. When using gow, the choice of cooked vs raw is going to depend on the specific program. In your case, it seems that you want -r=false, which should work now. Let me know if you run into any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants