- Notifications
You must be signed in to change notification settings - Fork928
fix: make terminal raw in ssh command on windows#12990
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
22 changes: 14 additions & 8 deletionscli/ssh.go
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
31 changes: 31 additions & 0 deletionspty/terminal.go
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,31 @@ | ||
package pty | ||
// TerminalState differs per-platform. | ||
type TerminalState struct { | ||
state terminalState | ||
} | ||
// MakeInputRaw calls term.MakeRaw on non-Windows platforms. On Windows it sets | ||
// special terminal modes that enable VT100 emulation as well as setting the | ||
// same modes that term.MakeRaw sets. | ||
// | ||
//nolint:revive | ||
func MakeInputRaw(fd uintptr) (*TerminalState, error) { | ||
return makeInputRaw(fd) | ||
} | ||
// MakeOutputRaw does nothing on non-Windows platforms. On Windows it sets | ||
// special terminal modes that enable VT100 emulation as well as setting the | ||
// same modes that term.MakeRaw sets. | ||
// | ||
//nolint:revive | ||
func MakeOutputRaw(fd uintptr) (*TerminalState, error) { | ||
return makeOutputRaw(fd) | ||
} | ||
// RestoreTerminal restores the terminal back to its original state. | ||
// | ||
//nolint:revive | ||
func RestoreTerminal(fd uintptr, state *TerminalState) error { | ||
return restoreTerminal(fd, state) | ||
} |
36 changes: 36 additions & 0 deletionspty/terminal_other.go
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,36 @@ | ||
//go:build !windows | ||
// +build !windows | ||
package pty | ||
import "golang.org/x/term" | ||
type terminalState *term.State | ||
//nolint:revive | ||
func makeInputRaw(fd uintptr) (*TerminalState, error) { | ||
s, err := term.MakeRaw(int(fd)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &TerminalState{ | ||
state: s, | ||
}, nil | ||
} | ||
//nolint:revive | ||
func makeOutputRaw(_ uintptr) (*TerminalState, error) { | ||
// Does nothing. makeInputRaw does enough for both input and output. | ||
return &TerminalState{ | ||
state: nil, | ||
}, nil | ||
} | ||
//nolint:revive | ||
func restoreTerminal(fd uintptr, state *TerminalState) error { | ||
if state == nil || state.state == nil { | ||
return nil | ||
} | ||
return term.Restore(int(fd), state.state) | ||
} |
65 changes: 65 additions & 0 deletionspty/terminal_windows.go
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,65 @@ | ||
//go:build windows | ||
// +build windows | ||
package pty | ||
import "golang.org/x/sys/windows" | ||
type terminalState uint32 | ||
// This is adapted from term.MakeRaw, but adds | ||
// ENABLE_VIRTUAL_TERMINAL_PROCESSING to the output mode and | ||
// ENABLE_VIRTUAL_TERMINAL_INPUT to the input mode. | ||
// | ||
// See: https://github.com/golang/term/blob/5b15d269ba1f54e8da86c8aa5574253aea0c2198/term_windows.go#L23 | ||
// | ||
// Copyright 2019 The Go Authors. BSD-3-Clause license. See: | ||
// https://github.com/golang/term/blob/master/LICENSE | ||
func makeRaw(handle windows.Handle, input bool) (uint32, error) { | ||
var prevState uint32 | ||
if err := windows.GetConsoleMode(handle, &prevState); err != nil { | ||
return 0, err | ||
} | ||
var raw uint32 | ||
if input { | ||
raw = prevState &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT) | ||
raw |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT | ||
} else { | ||
raw = prevState | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING | ||
} | ||
if err := windows.SetConsoleMode(handle, raw); err != nil { | ||
return 0, err | ||
} | ||
return prevState, nil | ||
} | ||
//nolint:revive | ||
func makeInputRaw(handle uintptr) (*TerminalState, error) { | ||
prevState, err := makeRaw(windows.Handle(handle), true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &TerminalState{ | ||
state: terminalState(prevState), | ||
}, nil | ||
} | ||
//nolint:revive | ||
func makeOutputRaw(handle uintptr) (*TerminalState, error) { | ||
prevState, err := makeRaw(windows.Handle(handle), false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &TerminalState{ | ||
state: terminalState(prevState), | ||
}, nil | ||
} | ||
//nolint:revive | ||
func restoreTerminal(handle uintptr, state *TerminalState) error { | ||
return windows.SetConsoleMode(windows.Handle(handle), uint32(state.state)) | ||
} |
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.