<commit_before>// +build linux darwin openbsd freebsd netbsd

package liner

import (
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
	"syscall"
	"unsafe"
)

func (s *State) cursorPos(x int) {
	if s.useCHA {
		// 'G' is "Cursor Character Absolute (CHA)"
		fmt.Printf("\x1b[%dG", x+1)
	} else {
		// 'C' is "Cursor Forward (CUF)"
		fmt.Print("\r")
		if x > 0 {
			fmt.Printf("\x1b[%dC", x)
		}
	}
}

func (s *State) eraseLine() {
	fmt.Print("\x1b[0K")
}

func (s *State) eraseScreen() {
	fmt.Print("\x1b[H\x1b[2J")
}

type winSize struct {
	row, col       uint16
	xpixel, ypixel uint16
}

func (s *State) getColumns() {
	var ws winSize
	ok, _, _ := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdout),
		syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws)))
	if ok < 0 {
		s.columns = 80
	}
	s.columns = int(ws.col)
}

func (s *State) getPos() (x, y int64, err error) {
	fmt.Print("\x1b[6n")
	var reply []rune
	for {
		var r rune
		r, _, err = s.r.ReadRune()
		if err != nil {
			return
		}
		if r == 'R' {
			break
		}
		if len(reply) == 0 && r != 0x1b {
			return 0, 0, io.ErrUnexpectedEOF
		}
		if len(reply) == 1 && r != '[' {
			return 0, 0, io.ErrUnexpectedEOF
		}
		reply = append(reply, r)
	}

	if len(reply) < 3 {
		return 0, 0, io.ErrUnexpectedEOF
	}
	num := strings.Split(string(reply[2:]), ";")
	if len(num) != 2 {
		return 0, 0, io.ErrUnexpectedEOF
	}
	y, err = strconv.ParseInt(num[0], 10, 32)
	if err != nil {
		return
	}
	x, err = strconv.ParseInt(num[1], 10, 32)
	return
}

func (s *State) checkOutput() {
	// xterm is known to support CHA
	if strings.Contains(strings.ToLower(os.Getenv("TERM")), "xterm") {
		s.useCHA = true
		return
	}

	// test for functional ANSI CHA
	xOrig, _, err := s.getPos()
	if err != nil {
		return
	}

	// Move using CHA
	fmt.Printf("\x1b[%dG", xOrig+1%2)
	x, _, err := s.getPos()
	if err != nil {
		return
	}
	if x == xOrig {
		return
	}

	// X moved, CHA is functional
	s.useCHA = true
	s.cursorPos(int(xOrig - 1))
}<commit_msg>Don't try to detect CHA support with DSR/CPR<commit_after>// +build linux darwin openbsd freebsd netbsd

package liner

import (
	"fmt"
	"os"
	"strings"
	"syscall"
	"unsafe"
)

func (s *State) cursorPos(x int) {
	if s.useCHA {
		// 'G' is "Cursor Character Absolute (CHA)"
		fmt.Printf("\x1b[%dG", x+1)
	} else {
		// 'C' is "Cursor Forward (CUF)"
		fmt.Print("\r")
		if x > 0 {
			fmt.Printf("\x1b[%dC", x)
		}
	}
}

func (s *State) eraseLine() {
	fmt.Print("\x1b[0K")
}

func (s *State) eraseScreen() {
	fmt.Print("\x1b[H\x1b[2J")
}

type winSize struct {
	row, col       uint16
	xpixel, ypixel uint16
}

func (s *State) getColumns() {
	var ws winSize
	ok, _, _ := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdout),
		syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws)))
	if ok < 0 {
		s.columns = 80
	}
	s.columns = int(ws.col)
}

func (s *State) checkOutput() {
	// xterm is known to support CHA
	if strings.Contains(strings.ToLower(os.Getenv("TERM")), "xterm") {
		s.useCHA = true
		return
	}

	// The test for functional ANSI CHA is unreliable (eg the Windows
	// telnet command does not support reading the cursor position with
	// an ANSI DSR request, despite setting TERM=ansi)

	// Assume CHA isn't supported (which should be safe, although it
	// does result in occasional visible cursor jitter)
	s.useCHA = false
}
{'fraction_non_alphanumeric': 0.16621253405994552, 'fraction_numerical': 0.019679079624583713, 'mean_word_length': 3.1455457967377667, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2420375', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1368, 'n_words': 364}