-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from 3 commits
8a4ad8f
e693344
7c45563
750654a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"github.com/ActiveState/cli/internal/osutils/user" | ||
"github.com/ActiveState/cli/internal/output" | ||
"github.com/ActiveState/cli/internal/subshell/sscommon" | ||
"github.com/ActiveState/cli/internal/subshell/termecho" | ||
"github.com/ActiveState/cli/pkg/project" | ||
) | ||
|
||
|
@@ -219,3 +220,17 @@ func (v *SubShell) IsAvailable() bool { | |
} | ||
return fileutils.FileExists(rcFile) | ||
} | ||
|
||
func (v *SubShell) TurnOffEcho() { | ||
if runtime.GOOS == "windows" { | ||
return // not supported | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
} | ||
termecho.Off() | ||
} | ||
|
||
func (v *SubShell) TurnOnEcho() { | ||
if runtime.GOOS == "windows" { | ||
return // not supported | ||
} | ||
termecho.Off() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't these, and the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, how embarrassing. Thanks for catching this. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package termecho | ||
|
||
import ( | ||
"github.com/ActiveState/cli/internal/errs" | ||
"github.com/ActiveState/cli/internal/multilog" | ||
) | ||
|
||
func Off() { | ||
err := toggle(false) | ||
if err != nil { | ||
multilog.Error("Unable to turn off terminal echoing: %v", errs.JoinMessage(err)) | ||
} | ||
} | ||
|
||
func On() { | ||
err := toggle(true) | ||
if err != nil { | ||
multilog.Error("Unable to turn off terminal echoing: %v", errs.JoinMessage(err)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package termecho | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
const ioctlReadTermios = unix.TIOCGETA | ||
const ioctlWriteTermios = unix.TIOCSETA |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package termecho | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
const ioctlReadTermios = unix.TCGETS | ||
const ioctlWriteTermios = unix.TCSETS |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
package termecho | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/ActiveState/cli/internal/errs" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func toggle(on bool) error { | ||
fd := int(os.Stdin.Fd()) | ||
termios, err := unix.IoctlGetTermios(fd, ioctlReadTermios) | ||
if err != nil { | ||
return errs.Wrap(err, "Could not get termios") | ||
} | ||
|
||
newState := *termios // copy | ||
if !on { | ||
newState.Lflag &^= unix.ECHO | ||
} else { | ||
newState.Lflag |= unix.ECHO | ||
} | ||
err = unix.IoctlSetTermios(fd, ioctlWriteTermios, &newState) | ||
if err != nil { | ||
return errs.Wrap(err, "Could not set termios") | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package termecho | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/ActiveState/cli/internal/errs" | ||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func toggle(on bool) error { | ||
fd := windows.Handle(os.Stdin.Fd()) | ||
var mode uint32 | ||
err := windows.GetConsoleMode(fd, &mode) | ||
if err != nil { | ||
return errs.Wrap(err, "Error calling GetConsoleMode") | ||
} | ||
|
||
newMode := mode | ||
if !on { | ||
newMode &^= windows.ENABLE_ECHO_INPUT | ||
} else { | ||
newMode |= windows.ENABLE_ECHO_INPUT | ||
} | ||
err = windows.SetConsoleMode(fd, newMode) | ||
if err != nil { | ||
return errs.Wrap(err, "Error calling SetConsoleMode") | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.