windows support
Vladimir Bauer
9 years ago
| 3 | 3 | |
| 4 | 4 | import ( |
| 5 | 5 | "fmt" |
| 6 | "io" | |
| 6 | 7 | "syscall" |
| 7 | 8 | "unsafe" |
| 8 | 9 | |
| 42 | 43 | maximumWindowSize coord |
| 43 | 44 | } |
| 44 | 45 | |
| 46 | // FdWriter is a writer with a file descriptor. | |
| 47 | type FdWriter interface { | |
| 48 | io.Writer | |
| 49 | Fd() uintptr | |
| 50 | } | |
| 51 | ||
| 45 | 52 | func (w *Writer) clearLines() { |
| 46 | f, ok := w.Out.(FdWriter) | |
| 53 | f, ok := w.out.(FdWriter) | |
| 47 | 54 | if ok && !isatty.IsTerminal(f.Fd()) { |
| 48 | 55 | ok = false |
| 49 | 56 | } |
| 50 | 57 | if !ok { |
| 51 | 58 | for i := 0; i < w.lineCount; i++ { |
| 52 | fmt.Fprintf(w.Out, "%c[%dA", ESC, 0) // move the cursor up | |
| 53 | fmt.Fprintf(w.Out, "%c[2K\r", ESC) // clear the line | |
| 59 | fmt.Fprintf(w.out, "%c[%dA", ESC, 0) // move the cursor up | |
| 60 | fmt.Fprintf(w.out, "%c[2K\r", ESC) // clear the line | |
| 54 | 61 | } |
| 55 | 62 | return |
| 56 | 63 | } |
| 72 | 79 | procFillConsoleOutputCharacter.Call(fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w))) |
| 73 | 80 | } |
| 74 | 81 | } |
| 82 | ||
| 83 | // TerminalWidth returns width of the terminal. | |
| 84 | func TerminalWidth() (int, error) { | |
| 85 | var info consoleScreenBufferInfo | |
| 86 | _, _, errno := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&info)), 0) | |
| 87 | if errno != 0 { | |
| 88 | return 0, errno | |
| 89 | } | |
| 90 | return int(info.size.x) - 1, nil | |
| 91 | } | |