github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/pcsliner/clear_windows.go (about)

     1  package pcsliner
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  )
     7  
     8  const (
     9  	std_output_handle = uint32(-11 & 0xFFFFFFFF)
    10  )
    11  
    12  var (
    13  	kernel32 = syscall.NewLazyDLL("kernel32.dll")
    14  
    15  	procGetStdHandle               = kernel32.NewProc("GetStdHandle")
    16  	procSetConsoleCursorPosition   = kernel32.NewProc("SetConsoleCursorPosition")
    17  	procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
    18  	procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
    19  )
    20  
    21  type (
    22  	coord struct {
    23  		x, y int16
    24  	}
    25  	smallRect struct {
    26  		left, top, right, bottom int16
    27  	}
    28  	consoleScreenBufferInfo struct {
    29  		dwSize              coord
    30  		dwCursorPosition    coord
    31  		wAttributes         int16
    32  		srWindow            smallRect
    33  		dwMaximumWindowSize coord
    34  	}
    35  )
    36  
    37  // ClearScreen 清空屏幕
    38  func (pl *PCSLiner) ClearScreen() {
    39  	ClearScreen()
    40  }
    41  
    42  // ClearScreen 清空屏幕
    43  func ClearScreen() {
    44  	out, _, _ := procGetStdHandle.Call(uintptr(std_output_handle))
    45  	hOut := syscall.Handle(out)
    46  
    47  	var sbi consoleScreenBufferInfo
    48  	procGetConsoleScreenBufferInfo.Call(uintptr(hOut), uintptr(unsafe.Pointer(&sbi)))
    49  
    50  	var numWritten uint32
    51  	procFillConsoleOutputCharacter.Call(uintptr(hOut), uintptr(' '),
    52  		uintptr(sbi.dwSize.x)*uintptr(sbi.dwSize.y),
    53  		0,
    54  		uintptr(unsafe.Pointer(&numWritten)))
    55  	procSetConsoleCursorPosition.Call(uintptr(hOut), 0)
    56  }