github.com/axw/llgo@v0.0.0-20160805011314-95b5fe4dca20/third_party/liner/output.go (about) 1 // +build linux darwin openbsd freebsd netbsd 2 3 package liner 4 5 import ( 6 "fmt" 7 "os" 8 "strings" 9 "syscall" 10 "unsafe" 11 ) 12 13 func (s *State) cursorPos(x int) { 14 if s.useCHA { 15 // 'G' is "Cursor Character Absolute (CHA)" 16 fmt.Printf("\x1b[%dG", x+1) 17 } else { 18 // 'C' is "Cursor Forward (CUF)" 19 fmt.Print("\r") 20 if x > 0 { 21 fmt.Printf("\x1b[%dC", x) 22 } 23 } 24 } 25 26 func (s *State) eraseLine() { 27 fmt.Print("\x1b[0K") 28 } 29 30 func (s *State) eraseScreen() { 31 fmt.Print("\x1b[H\x1b[2J") 32 } 33 34 func (s *State) moveUp(lines int) { 35 fmt.Printf("\x1b[%dA", lines) 36 } 37 38 func (s *State) moveDown(lines int) { 39 fmt.Printf("\x1b[%dB", lines) 40 } 41 42 func (s *State) emitNewLine() { 43 fmt.Print("\n") 44 } 45 46 type winSize struct { 47 row, col uint16 48 xpixel, ypixel uint16 49 } 50 51 func (s *State) getColumns() { 52 var ws winSize 53 ok, _, _ := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdout), 54 syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws))) 55 if ok < 0 { 56 s.columns = 80 57 } 58 s.columns = int(ws.col) 59 } 60 61 func (s *State) checkOutput() { 62 // xterm is known to support CHA 63 if strings.Contains(strings.ToLower(os.Getenv("TERM")), "xterm") { 64 s.useCHA = true 65 return 66 } 67 68 // The test for functional ANSI CHA is unreliable (eg the Windows 69 // telnet command does not support reading the cursor position with 70 // an ANSI DSR request, despite setting TERM=ansi) 71 72 // Assume CHA isn't supported (which should be safe, although it 73 // does result in occasional visible cursor jitter) 74 s.useCHA = false 75 }