better windows clearLines
Vladimir Bauer
7 years ago
| 16 | 16 | procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute") |
| 17 | 17 | ) |
| 18 | 18 | |
| 19 | type short int16 | |
| 20 | type dword uint32 | |
| 21 | type word uint16 | |
| 22 | ||
| 23 | 19 | type coord struct { |
| 24 | x short | |
| 25 | y short | |
| 20 | x int16 | |
| 21 | y int16 | |
| 26 | 22 | } |
| 27 | 23 | |
| 28 | 24 | type smallRect struct { |
| 29 | left short | |
| 30 | top short | |
| 31 | right short | |
| 32 | bottom short | |
| 25 | left int16 | |
| 26 | top int16 | |
| 27 | right int16 | |
| 28 | bottom int16 | |
| 33 | 29 | } |
| 34 | 30 | |
| 35 | 31 | type consoleScreenBufferInfo struct { |
| 36 | 32 | size coord |
| 37 | 33 | cursorPosition coord |
| 38 | attributes word | |
| 34 | attributes uint16 | |
| 39 | 35 | window smallRect |
| 40 | 36 | maximumWindowSize coord |
| 41 | 37 | } |
| 47 | 43 | var info consoleScreenBufferInfo |
| 48 | 44 | procGetConsoleScreenBufferInfo.Call(w.fd, uintptr(unsafe.Pointer(&info))) |
| 49 | 45 | |
| 50 | for i := 0; i < w.lineCount; i++ { | |
| 51 | // move the cursor up | |
| 52 | info.cursorPosition.y-- | |
| 53 | procSetConsoleCursorPosition.Call(w.fd, uintptr(*(*int32)(unsafe.Pointer(&info.cursorPosition)))) | |
| 54 | // clear the line | |
| 55 | cursor := coord{ | |
| 56 | x: info.window.left, | |
| 57 | y: info.window.top + info.cursorPosition.y, | |
| 58 | } | |
| 59 | var count, dw dword | |
| 60 | count = dword(info.size.x) | |
| 61 | procFillConsoleOutputCharacter.Call(w.fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&dw))) | |
| 46 | info.cursorPosition.y -= int16(w.lineCount) | |
| 47 | if info.cursorPosition.y < 0 { | |
| 48 | info.cursorPosition.y = 0 | |
| 62 | 49 | } |
| 50 | procSetConsoleCursorPosition.Call(w.fd, uintptr(uint32(uint16(info.cursorPosition.y))<<16|uint32(uint16(info.cursorPosition.x)))) | |
| 51 | ||
| 52 | // clear the lines | |
| 53 | cursor := coord{ | |
| 54 | x: info.window.left, | |
| 55 | y: info.cursorPosition.y, | |
| 56 | } | |
| 57 | count := uint32(info.size.x) * uint32(w.lineCount) | |
| 58 | procFillConsoleOutputCharacter.Call(w.fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(new(uint32)))) | |
| 63 | 59 | } |