github.com/dolfly/pty@v1.2.1/winsize_windows.go (about)

     1  package pty
     2  
     3  import (
     4  	"os"
     5  	"unsafe"
     6  )
     7  
     8  // types from golang.org/x/sys/windows
     9  type (
    10  	// copy of https://pkg.go.dev/golang.org/x/sys/windows#Coord
    11  	windowsCoord struct {
    12  		X int16
    13  		Y int16
    14  	}
    15  
    16  	// copy of https://pkg.go.dev/golang.org/x/sys/windows#SmallRect
    17  	windowsSmallRect struct {
    18  		Left   int16
    19  		Top    int16
    20  		Right  int16
    21  		Bottom int16
    22  	}
    23  
    24  	// copy of https://pkg.go.dev/golang.org/x/sys/windows#ConsoleScreenBufferInfo
    25  	windowsConsoleScreenBufferInfo struct {
    26  		Size              windowsCoord
    27  		CursorPosition    windowsCoord
    28  		Attributes        uint16
    29  		Window            windowsSmallRect
    30  		MaximumWindowSize windowsCoord
    31  	}
    32  )
    33  
    34  func (c windowsCoord) Pack() uintptr {
    35  	return uintptr((int32(c.Y) << 16) | int32(c.X))
    36  }
    37  
    38  // Setsize resizes t to ws.
    39  func Setsize(t FdHolder, ws *Winsize) error {
    40  	err := resizePseudoConsole.Find()
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	r1, _, err := resizePseudoConsole.Call(
    46  		t.Fd(),
    47  		(windowsCoord{X: int16(ws.Cols), Y: int16(ws.Rows)}).Pack(),
    48  	)
    49  	if r1 != 0 {
    50  		// S_OK: 0
    51  		return os.NewSyscallError("ResizePseudoConsole", err)
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // GetsizeFull returns the full terminal size description.
    58  func GetsizeFull(t FdHolder) (size *Winsize, err error) {
    59  	err = getConsoleScreenBufferInfo.Find()
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	var info windowsConsoleScreenBufferInfo
    65  	r1, _, err := getConsoleScreenBufferInfo.Call(t.Fd(), uintptr(unsafe.Pointer(&info)))
    66  	if r1 != 0 {
    67  		// S_OK: 0
    68  		return nil, os.NewSyscallError("GetConsoleScreenBufferInfo", err)
    69  	}
    70  
    71  	return &Winsize{
    72  		Rows: uint16(info.Window.Bottom - info.Window.Top + 1),
    73  		Cols: uint16(info.Window.Right - info.Window.Left + 1),
    74  	}, nil
    75  }