github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/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  type winSize struct {
    35  	row, col       uint16
    36  	xpixel, ypixel uint16
    37  }
    38  
    39  func (s *State) getColumns() {
    40  	var ws winSize
    41  	ok, _, _ := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdout),
    42  		syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws)))
    43  	if ok < 0 {
    44  		s.columns = 80
    45  	}
    46  	s.columns = int(ws.col)
    47  }
    48  
    49  func (s *State) checkOutput() {
    50  	// xterm is known to support CHA
    51  	if strings.Contains(strings.ToLower(os.Getenv("TERM")), "xterm") {
    52  		s.useCHA = true
    53  		return
    54  	}
    55  
    56  	// The test for functional ANSI CHA is unreliable (eg the Windows
    57  	// telnet command does not support reading the cursor position with
    58  	// an ANSI DSR request, despite setting TERM=ansi)
    59  
    60  	// Assume CHA isn't supported (which should be safe, although it
    61  	// does result in occasional visible cursor jitter)
    62  	s.useCHA = false
    63  }