Skip to content

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

Open
@fkaleo

Description

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)
	}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions