Skip to content

Commit

Permalink
Use golang.org/x/sys/windows.GetConsoleScreenBufferInfo
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed May 16, 2020
1 parent eae3dbf commit 145a546
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions internal/dotwriter/writer_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,18 @@ import (
var kernel32 = syscall.NewLazyDLL("kernel32.dll")

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

// clear the line and move the cursor up
var clear = fmt.Sprintf("%c[%dA%c[2K\r", ESC, 0, ESC)

type short int16
type dword uint32
type word uint16

type coord struct {
x short
y short
}

type smallRect struct {
left short
top short
right short
bottom short
}

type consoleScreenBufferInfo struct {
size coord
cursorPosition coord
attributes word
window smallRect
maximumWindowSize coord
x int16
y int16
}

type fdWriter interface {
Expand All @@ -62,20 +44,23 @@ func (w *Writer) clearLines(count int) {
return
}
fd := f.Fd()
var csbi consoleScreenBufferInfo
_, _, _ = procGetConsoleScreenBufferInfo.Call(fd, uintptr(unsafe.Pointer(&csbi)))

var csbi windows.ConsoleScreenBufferInfo
if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &csbi); err != nil {
return
}

for i := 0; i < count; i++ {
// move the cursor up
csbi.cursorPosition.y--
_, _, _ = procSetConsoleCursorPosition.Call(fd, uintptr(*(*int32)(unsafe.Pointer(&csbi.cursorPosition))))
csbi.CursorPosition.Y--
_, _, _ = procSetConsoleCursorPosition.Call(fd, uintptr(*(*int32)(unsafe.Pointer(&csbi.CursorPosition))))
// clear the line
cursor := coord{
x: csbi.window.left,
y: csbi.window.top + csbi.cursorPosition.y,
x: csbi.Window.Left,
y: csbi.Window.Top + csbi.CursorPosition.Y,
}
var count, w dword
count = dword(csbi.size.x)
count = dword(csbi.Size.X)
_, _, _ = procFillConsoleOutputCharacter.Call(fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w)))
}
}
Expand Down

0 comments on commit 145a546

Please sign in to comment.