mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-29 21:29:24 +00:00
Fix Windows terminal handling
Fix some issues with Windows terminal handling with respect to TTYs that came up as part of the code that adds support for terminal resizing.
This commit is contained in:
@@ -32,10 +32,11 @@ type Size struct {
|
||||
// GetSize returns the current size of the user's terminal. If it isn't a terminal,
|
||||
// nil is returned.
|
||||
func (t TTY) GetSize() *Size {
|
||||
if !t.IsTerminalOut() {
|
||||
outFd, isTerminal := term.GetFdInfo(t.Out)
|
||||
if !isTerminal {
|
||||
return nil
|
||||
}
|
||||
return GetSize(t.Out.(fd).Fd())
|
||||
return GetSize(outFd)
|
||||
}
|
||||
|
||||
// GetSize returns the current size of the terminal associated with fd.
|
||||
@@ -57,7 +58,8 @@ func SetSize(fd uintptr, size Size) error {
|
||||
// MonitorSize monitors the terminal's size. It returns a TerminalSizeQueue primed with
|
||||
// initialSizes, or nil if there's no TTY present.
|
||||
func (t *TTY) MonitorSize(initialSizes ...*Size) TerminalSizeQueue {
|
||||
if !t.IsTerminalOut() {
|
||||
outFd, isTerminal := term.GetFdInfo(t.Out)
|
||||
if !isTerminal {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -69,7 +71,7 @@ func (t *TTY) MonitorSize(initialSizes ...*Size) TerminalSizeQueue {
|
||||
stopResizing: make(chan struct{}),
|
||||
}
|
||||
|
||||
t.sizeQueue.monitorSize(initialSizes...)
|
||||
t.sizeQueue.monitorSize(outFd, initialSizes...)
|
||||
|
||||
return t.sizeQueue
|
||||
}
|
||||
@@ -94,7 +96,7 @@ var _ TerminalSizeQueue = &sizeQueue{}
|
||||
|
||||
// monitorSize primes resizeChan with initialSizes and then monitors for resize events. With each
|
||||
// new event, it sends the current terminal size to resizeChan.
|
||||
func (s *sizeQueue) monitorSize(initialSizes ...*Size) {
|
||||
func (s *sizeQueue) monitorSize(outFd uintptr, initialSizes ...*Size) {
|
||||
// send the initial sizes
|
||||
for i := range initialSizes {
|
||||
if initialSizes[i] != nil {
|
||||
@@ -104,7 +106,7 @@ func (s *sizeQueue) monitorSize(initialSizes ...*Size) {
|
||||
|
||||
resizeEvents := make(chan Size, 1)
|
||||
|
||||
monitorResizeEvents(s.t.Out.(fd).Fd(), resizeEvents, s.stopResizing)
|
||||
monitorResizeEvents(outFd, resizeEvents, s.stopResizing)
|
||||
|
||||
// listen for resize events in the background
|
||||
go func() {
|
||||
|
||||
@@ -29,7 +29,11 @@ func monitorResizeEvents(fd uintptr, resizeEvents chan<- Size, stop chan struct{
|
||||
go func() {
|
||||
defer runtime.HandleCrash()
|
||||
|
||||
var lastSize Size
|
||||
size := GetSize(fd)
|
||||
if size == nil {
|
||||
return
|
||||
}
|
||||
lastSize := *size
|
||||
|
||||
for {
|
||||
// see if we need to stop running
|
||||
|
||||
@@ -52,11 +52,6 @@ type TTY struct {
|
||||
sizeQueue *sizeQueue
|
||||
}
|
||||
|
||||
// fd returns a file descriptor for a given object.
|
||||
type fd interface {
|
||||
Fd() uintptr
|
||||
}
|
||||
|
||||
// IsTerminalIn returns true if t.In is a terminal. Does not check /dev/tty
|
||||
// even if TryDev is set.
|
||||
func (t TTY) IsTerminalIn() bool {
|
||||
@@ -71,8 +66,8 @@ func (t TTY) IsTerminalOut() bool {
|
||||
|
||||
// IsTerminal returns whether the passed object is a terminal or not
|
||||
func IsTerminal(i interface{}) bool {
|
||||
file, ok := i.(fd)
|
||||
return ok && term.IsTerminal(file.Fd())
|
||||
_, terminal := term.GetFdInfo(i)
|
||||
return terminal
|
||||
}
|
||||
|
||||
// Safe invokes the provided function and will attempt to ensure that when the
|
||||
@@ -82,22 +77,16 @@ func IsTerminal(i interface{}) bool {
|
||||
// If the input file descriptor is not a TTY and TryDev is true, the /dev/tty file
|
||||
// will be opened (if available).
|
||||
func (t TTY) Safe(fn SafeFunc) error {
|
||||
in := t.In
|
||||
inFd, isTerminal := term.GetFdInfo(t.In)
|
||||
|
||||
var hasFd bool
|
||||
var inFd uintptr
|
||||
if desc, ok := in.(fd); ok && in != nil {
|
||||
inFd = desc.Fd()
|
||||
hasFd = true
|
||||
}
|
||||
if t.TryDev && (!hasFd || !term.IsTerminal(inFd)) {
|
||||
if !isTerminal && t.TryDev {
|
||||
if f, err := os.Open("/dev/tty"); err == nil {
|
||||
defer f.Close()
|
||||
inFd = f.Fd()
|
||||
hasFd = true
|
||||
isTerminal = term.IsTerminal(inFd)
|
||||
}
|
||||
}
|
||||
if !hasFd || !term.IsTerminal(inFd) {
|
||||
if !isTerminal {
|
||||
return fn()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user