uilive icon indicating copy to clipboard operation
uilive copied to clipboard

constant scrolling when writing a line of size 2 * columns + 1

Open fkaleo opened this issue 4 years ago • 0 comments

When writing a line of a very specific size, the next line will be written one line too low leading to undesired scrolling. The following code will trigger the bug:

package main

import (
	"fmt"
	"os"
	"runtime"
	"strings"
	"syscall"
	"time"
	"unsafe"

	"github.com/gosuri/uilive"
)

type windowSize struct {
	rows uint16
	cols uint16
}

func getTermSize() (int, int) {
	var out *os.File
	var err error
	var sz windowSize
	if runtime.GOOS == "openbsd" {
		out, err = os.OpenFile("/dev/tty", os.O_RDWR, 0)
		if err != nil {
			return 0, 0
		}

	} else {
		out, err = os.OpenFile("/dev/tty", os.O_WRONLY, 0)
		if err != nil {
			return 0, 0
		}
	}
	_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
		out.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
	return int(sz.cols), int(sz.rows)
}

func main() {
	cols, _ := getTermSize()
	writer := uilive.New()

	for {
		_, _ = fmt.Fprintf(writer, "%s\n", strings.Repeat("b", cols*2+1))
		writer.Flush()
		time.Sleep(1000 * time.Millisecond)
	}
}

fkaleo avatar Apr 24 '20 19:04 fkaleo