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

Disable terminal echo while State Tool is running. #2882

Merged
merged 4 commits into from
Nov 15, 2023

Conversation

mitchell-as
Copy link
Contributor

@mitchell-as mitchell-as commented Nov 13, 2023

TaskDX-2214 As a user I can't interfere with State Tool by pressing keys during read-only actions

The exception is for prompts and subshells (which temporarily re-enable echoing).

Note: the survey package does this automatically, so no changes to the prompt package are required.

The exception is for prompts and subshells (which temporarily re-enable echoing).

func (v *SubShell) TurnOffEcho() {
if runtime.GOOS == "windows" {
return // not supported
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows does not have access to the termios functions needed to disable echo in emulated shells like Git Bash (which is not interactive anyway).

@mitchell-as mitchell-as requested a review from MDrakos November 13, 2023 21:16
@mitchell-as mitchell-as marked this pull request as ready for review November 13, 2023 21:16
Copy link
Member

@MDrakos MDrakos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that's not obvious in this PR is how the echo is re-enabled for prompts... how is that handled?

if runtime.GOOS == "windows" {
return // not supported
}
termecho.Off()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these, and the other TurnOnEcho functions be calling termecho.On()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, how embarrassing. Thanks for catching this.

Comment on lines +211 to +215
// Disable terminal echo while State Tool is running.
// Other than in prompts and subshells (which temporarily re-enable echo), user typing should
// not interfere with output (e.g. runtime progress bars).
sshell.TurnOffEcho()
defer sshell.TurnOnEcho()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also disable ctrl+c and other similar inputs? I don't believe that's something we want to disallow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it possible that if the state tool errors mid-execution, we could leave the user's shell in a state where they cannot echo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't see this, but in my original PR (#2768), I noted that echo is restored after ^C, so no problem there. The defer does its job.

If there is a hard crash, echo will probably be lost, but this is the case for any terminal program that messes with termios, so the only resolution is for the user to exit this terminal and start up a new one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the defer will run even on a panic as long as it's on the function call stack so we should be good even in the event of a crash as this defer is placed ahead of the actual command logic.

@mitchell-as
Copy link
Contributor Author

As mentioned in the PR description, the survey library automatically handles the flipping on/off of terminal echo. I don't know exactly how it does it, but during testing it did.

@mitchell-as mitchell-as requested a review from MDrakos November 14, 2023 22:15
@mitchell-as mitchell-as merged commit 137a5d7 into version/0-43-0-RC1 Nov 15, 2023
7 checks passed
@mitchell-as mitchell-as deleted the mitchell/dx-2214-2 branch November 15, 2023 17:32
mitchell-as added a commit that referenced this pull request Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants