Skip to content

Commit

Permalink
Disable terminal echo while State Tool is running.
Browse files Browse the repository at this point in the history
The exception is for prompts and subshells (which temporarily re-enable echoing).
  • Loading branch information
mitchell-as committed Nov 13, 2023
1 parent e693344 commit 5dbd4bf
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 27 deletions.
7 changes: 7 additions & 0 deletions cmd/state/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ func run(args []string, isInteractive bool, cfg *config.Instance, out output.Out

// Set up conditional, which accesses a lot of primer data
sshell := subshell.New(cfg)
if isInteractive {
// 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()
}

conditional := constraints.NewPrimeConditional(auth, pj, sshell.Shell())
project.RegisterConditional(conditional)
Expand Down
9 changes: 0 additions & 9 deletions internal/osutils/termecho/termecho.go

This file was deleted.

2 changes: 2 additions & 0 deletions internal/runbits/activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func ActivateAndWait(
if err := ss.Activate(proj, cfg, out); err != nil {
return locale.WrapError(err, "error_could_not_activate_subshell", "Could not activate a new subshell.")
}
ss.TurnOnEcho() // temporarily re-enable echo while the subshell is active
defer ss.TurnOffEcho()

a, err := process.NewActivation(cfg, os.Getpid())
if err != nil {
Expand Down
17 changes: 4 additions & 13 deletions internal/runbits/runtime/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"sync"
"time"

"github.com/go-openapi/strfmt"
"github.com/vbauerster/mpb/v7"
"golang.org/x/net/context"

"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/internal/osutils/termecho"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/pkg/platform/runtime/artifact"
"github.com/ActiveState/cli/pkg/platform/runtime/setup/events"
"github.com/go-openapi/strfmt"
"github.com/vbauerster/mpb/v7"
"golang.org/x/net/context"
)

type step struct {
Expand Down Expand Up @@ -89,10 +89,6 @@ type ProgressDigester struct {
}

func NewProgressIndicator(w io.Writer, out output.Outputer) *ProgressDigester {
err := termecho.Off()
if err != nil {
multilog.Error("Unable to turn off terminal echoing: %v", errs.JoinMessage(err))
}
ctx, cancel := context.WithCancel(context.Background())
return &ProgressDigester{
mainProgress: mpb.NewWithContext(
Expand Down Expand Up @@ -384,10 +380,5 @@ Still expecting:
// Blank line to separate progress from rest of output
p.out.Notice("")

err := termecho.On()
if err != nil {
multilog.Error("Unable to turn terminal echoing back on: %v", errs.JoinMessage(err))
}

return nil
}
9 changes: 9 additions & 0 deletions internal/subshell/bash/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -219,3 +220,11 @@ func (v *SubShell) IsAvailable() bool {
}
return fileutils.FileExists(rcFile)
}

func (v *SubShell) TurnOffEcho() {
termecho.Off()
}

func (v *SubShell) TurnOnEcho() {
termecho.Off()
}
9 changes: 9 additions & 0 deletions internal/subshell/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ActiveState/cli/internal/osutils"
"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"
)

Expand Down Expand Up @@ -203,3 +204,11 @@ func (v *SubShell) IsActive() bool {
func (v *SubShell) IsAvailable() bool {
return runtime.GOOS == "windows"
}

func (v *SubShell) TurnOffEcho() {
termecho.Off()
}

func (v *SubShell) TurnOnEcho() {
termecho.Off()
}
9 changes: 9 additions & 0 deletions internal/subshell/fish/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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"
)

Expand Down Expand Up @@ -195,3 +196,11 @@ func (v *SubShell) IsAvailable() bool {
}
return fileutils.FileExists(rcFile)
}

func (v *SubShell) TurnOffEcho() {
termecho.Off()
}

func (v *SubShell) TurnOnEcho() {
termecho.Off()
}
6 changes: 6 additions & 0 deletions internal/subshell/subshell.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ type SubShell interface {

// IsAvailable returns whether the shell is available on the system
IsAvailable() bool

// TurnOffEcho turns off input echoing.
TurnOffEcho()

// TurnOnEcho turns on input echoing.
TurnOnEcho()
}

// New returns the subshell relevant to the current process, but does not activate it
Expand Down
9 changes: 9 additions & 0 deletions internal/subshell/tcsh/tcsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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"
)

Expand Down Expand Up @@ -189,3 +190,11 @@ func (v *SubShell) IsAvailable() bool {
}
return fileutils.FileExists(rcFile)
}

func (v *SubShell) TurnOffEcho() {
termecho.Off()
}

func (v *SubShell) TurnOnEcho() {
termecho.Off()
}
20 changes: 20 additions & 0 deletions internal/subshell/termecho/termecho.go
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))
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"

"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/logging"
"golang.org/x/sys/windows"
)

Expand All @@ -13,10 +12,6 @@ func toggle(on bool) error {
var mode uint32
err := windows.GetConsoleMode(fd, &mode)
if err != nil {
if shell := os.Getenv("SHELL"); shell != "" {
logging.Debug("Cannot turn off terminal echo in %s", shell)
return nil
}
return errs.Wrap(err, "Error calling GetConsoleMode")
}

Expand Down
9 changes: 9 additions & 0 deletions internal/subshell/zsh/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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"
)

Expand Down Expand Up @@ -239,3 +240,11 @@ func (v *SubShell) IsAvailable() bool {
}
return fileutils.FileExists(rcFile)
}

func (v *SubShell) TurnOffEcho() {
termecho.Off()
}

func (v *SubShell) TurnOnEcho() {
termecho.Off()
}

0 comments on commit 5dbd4bf

Please sign in to comment.