-
Notifications
You must be signed in to change notification settings - Fork 14
Do not use terminal escape sequences in non-interactive terminals or the Windows command prompt. #2327
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
Merged
Merged
Do not use terminal escape sequences in non-interactive terminals or the Windows command prompt. #2327
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ae4daf
Do not use terminal escape sequences in non-interactive terminals or …
mitchell-as 2bca7c2
Support interactive progress in cmd.exe.
mitchell-as 44da254
Handle terminals other than cmd.exe on Windows.
mitchell-as 261efb0
Fixed failing unit tests.
mitchell-as a0d6a0a
Move shell detection into its own function.
mitchell-as a8d6d94
Added comments about error handling.
mitchell-as 920975d
Handle unsupported shells in `subshell.DetectShell()`.
mitchell-as 2ac1fc0
Report first instance of a spinner error to rollbar.
mitchell-as cde3aad
Fixed windows build error.
mitchell-as File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,4 +90,5 @@ type Config struct { | |
| ErrWriter io.Writer | ||
| Colored bool | ||
| Interactive bool | ||
| ShellName string | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //go:build linux || darwin | ||
| // +build linux darwin | ||
|
|
||
| package output | ||
|
|
||
| import ( | ||
| "github.com/ActiveState/cli/internal/rollbar" | ||
| ) | ||
|
|
||
| func (d *Spinner) moveCaretBackInCommandPrompt(n int) { | ||
| if !d.reportedError { | ||
| rollbar.Error("Incorrectly detected Windows command prompt in Unix environment") | ||
| d.reportedError = true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //go:build windows | ||
| // +build windows | ||
|
|
||
| package output | ||
|
|
||
| import ( | ||
| "os" | ||
| "syscall" | ||
| "unsafe" | ||
|
|
||
| "github.com/ActiveState/cli/internal/rollbar" | ||
| ) | ||
|
|
||
| var kernel32 = syscall.NewLazyDLL("kernel32.dll") | ||
| var procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo") | ||
| var procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition") | ||
|
|
||
| type coord struct { | ||
| x short | ||
| y short | ||
| } | ||
|
|
||
| type short int16 | ||
| type word uint16 | ||
|
|
||
| type smallRect struct { | ||
| bottom short | ||
| left short | ||
| right short | ||
| top short | ||
| } | ||
|
|
||
| type consoleScreenBufferInfo struct { | ||
| size coord | ||
| cursorPosition coord | ||
| attributes word | ||
| window smallRect | ||
| maximumWindowSize coord | ||
| } | ||
|
|
||
| func (d *Spinner) moveCaretBackInCommandPrompt(n int) { | ||
| handle := syscall.Handle(os.Stdout.Fd()) | ||
|
|
||
| var csbi consoleScreenBufferInfo | ||
| if _, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi))); err != nil { | ||
| var cursor coord | ||
| cursor.x = csbi.cursorPosition.x + short(-n) | ||
| cursor.y = csbi.cursorPosition.y | ||
|
|
||
| _, _, err2 := procSetConsoleCursorPosition.Call(uintptr(handle), uintptr(*(*int32)(unsafe.Pointer(&cursor)))) | ||
| if err2 != nil && !d.reportedError { | ||
| rollbar.Error("Error calling SetConsoleCursorPosition: %v", err2) | ||
| d.reportedError = true | ||
| } | ||
| } else if !d.reportedError { | ||
| rollbar.Error("Error calling GetConsoleScreenBufferInfo: %v", err) | ||
| d.reportedError = true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,49 +88,34 @@ type SubShell interface { | |
|
|
||
| // New returns the subshell relevant to the current process, but does not activate it | ||
| func New(cfg sscommon.Configurable) SubShell { | ||
| binary := resolveBinaryPath(DetectShellBinary(cfg)) | ||
|
|
||
| name := filepath.Base(binary) | ||
| name = strings.TrimSuffix(name, filepath.Ext(name)) | ||
| logging.Debug("Detected SHELL: %s", name) | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| // For some reason Go or MSYS doesn't translate paths with spaces correctly, so we have to strip out the | ||
| // invalid escape characters for spaces | ||
| binary = strings.ReplaceAll(binary, `\ `, ` `) | ||
| } | ||
| name, path := DetectShell(cfg) | ||
|
|
||
| var subs SubShell | ||
| switch name { | ||
| case "bash": | ||
| case bash.Name: | ||
| subs = &bash.SubShell{} | ||
| case "zsh": | ||
| case zsh.Name: | ||
| subs = &zsh.SubShell{} | ||
| case "tcsh": | ||
| case tcsh.Name: | ||
| subs = &tcsh.SubShell{} | ||
| case "fish": | ||
| case fish.Name: | ||
| subs = &fish.SubShell{} | ||
| case "cmd": | ||
| case cmd.Name: | ||
| subs = &cmd.SubShell{} | ||
| default: | ||
| logging.Debug("Unsupported shell: %s, defaulting to OS default.", name) | ||
| rollbar.Error("Unsupported shell: %s", name) // we just want to know what this person is using | ||
| rollbar.Error("subshell.DetectShell did not return a known name: %s", name) | ||
| switch runtime.GOOS { | ||
| case "windows": | ||
| binary = "cmd.exe" | ||
| subs = &cmd.SubShell{} | ||
| case "darwin": | ||
| binary = "zsh" | ||
| subs = &zsh.SubShell{} | ||
| default: | ||
| binary = "bash" | ||
| subs = &bash.SubShell{} | ||
| } | ||
| binary = resolveBinaryPath(binary) | ||
| } | ||
|
Contributor
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. This should be part of the shell detection. |
||
|
|
||
| logging.Debug("Using binary: %s", binary) | ||
| subs.SetBinary(binary) | ||
| logging.Debug("Using binary: %s", path) | ||
| subs.SetBinary(path) | ||
|
|
||
| env := funk.FilterString(os.Environ(), func(s string) bool { | ||
| return !strings.HasPrefix(s, constants.ProjectEnvVarName) | ||
|
|
@@ -182,8 +167,10 @@ func ConfigureAvailableShells(shell SubShell, cfg sscommon.Configurable, env map | |
| return nil | ||
| } | ||
|
|
||
| func DetectShellBinary(cfg sscommon.Configurable) (binary string) { | ||
| // DetectShell detects the shell relevant to the current process and returns its name and path. | ||
| func DetectShell(cfg sscommon.Configurable) (string, string) { | ||
| configured := cfg.GetString(ConfigKeyShell) | ||
| var binary string | ||
| defer func() { | ||
| // do not re-write shell binary to config, if the value did not change. | ||
| if configured == binary { | ||
|
|
@@ -196,25 +183,56 @@ func DetectShellBinary(cfg sscommon.Configurable) (binary string) { | |
| } | ||
| }() | ||
|
|
||
| if binary := os.Getenv("SHELL"); binary != "" { | ||
| return binary | ||
| } | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| binary = os.Getenv("SHELL") | ||
| if binary == "" && runtime.GOOS == "windows" { | ||
| binary = os.Getenv("ComSpec") | ||
| if binary != "" { | ||
| return binary | ||
| } | ||
| } | ||
|
|
||
| fallback := configured | ||
| if fallback == "" { | ||
| if binary == "" { | ||
| binary = configured | ||
| } | ||
| if binary == "" { | ||
| if runtime.GOOS == "windows" { | ||
| fallback = "cmd.exe" | ||
| binary = "cmd.exe" | ||
| } else { | ||
| fallback = "bash" | ||
| binary = "bash" | ||
| } | ||
| } | ||
|
|
||
| path := resolveBinaryPath(binary) | ||
|
|
||
| name := filepath.Base(path) | ||
| name = strings.TrimSuffix(name, filepath.Ext(name)) | ||
| logging.Debug("Detected SHELL: %s", name) | ||
|
|
||
| if runtime.GOOS == "windows" { | ||
| // For some reason Go or MSYS doesn't translate paths with spaces correctly, so we have to strip out the | ||
| // invalid escape characters for spaces | ||
| path = strings.ReplaceAll(path, `\ `, ` `) | ||
| } | ||
|
|
||
| isKnownShell := false | ||
| for _, ssName := range []string{bash.Name, cmd.Name, fish.Name, tcsh.Name, zsh.Name} { | ||
| if name == ssName { | ||
| isKnownShell = true | ||
| break | ||
| } | ||
| } | ||
| if !isKnownShell { | ||
| logging.Debug("Unsupported shell: %s, defaulting to OS default.", name) | ||
| rollbar.Error("Unsupported shell: %s", name) // we just want to know what this person is using | ||
| switch runtime.GOOS { | ||
| case "windows": | ||
| name = cmd.Name | ||
| path = resolveBinaryPath("cmd.exe") | ||
| case "darwin": | ||
| name = zsh.Name | ||
| path = resolveBinaryPath("zsh") | ||
| default: | ||
| name = bash.Name | ||
| path = resolveBinaryPath("bash") | ||
| } | ||
| } | ||
|
|
||
| return fallback | ||
| return name, path | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.