Skip to content

Commit

Permalink
fix #790, 修复windows下编译失败
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed Oct 19, 2019
1 parent 5deb491 commit a2b8717
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions pcsliner/clear_windows.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
package pcsliner

import (
"github.com/peterh/liner"
_ "unsafe" // for go:linkname
"syscall"
"unsafe"
)

//go:linkname eraseScreen github.com/iikira/BaiduPCS-Go/vendor/github.com/peterh/liner.(*State).eraseScreen
func eraseScreen(s *liner.State)
const (
std_output_handle = uint32(-11 & 0xFFFFFFFF)
)

var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")

procGetStdHandle = kernel32.NewProc("GetStdHandle")
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
)

type (
coord struct {
x, y int16
}
smallRect struct {
left, top, right, bottom int16
}
consoleScreenBufferInfo struct {
dwSize coord
dwCursorPosition coord
wAttributes int16
srWindow smallRect
dwMaximumWindowSize coord
}
)

// ClearScreen 清空屏幕
func (pl *PCSLiner) ClearScreen() {
eraseScreen(pl.State)
ClearScreen()
}

// ClearScreen 清空屏幕
func ClearScreen() {
liner := NewLiner()
liner.ClearScreen()
liner.Close()
out, _, _ := procGetStdHandle.Call(uintptr(std_output_handle))
hOut := syscall.Handle(out)

var sbi consoleScreenBufferInfo
procGetConsoleScreenBufferInfo.Call(uintptr(hOut), uintptr(unsafe.Pointer(&sbi)))

var numWritten uint32
procFillConsoleOutputCharacter.Call(uintptr(hOut), uintptr(' '),
uintptr(sbi.dwSize.x)*uintptr(sbi.dwSize.y),
0,
uintptr(unsafe.Pointer(&numWritten)))
procSetConsoleCursorPosition.Call(uintptr(hOut), 0)
}

0 comments on commit a2b8717

Please sign in to comment.