how many terminal lines does a long line of text take?

27 Views Asked by At

I am writing a program that acts as tail -f, but retains only the last X lines of terminal.

It is straightforward enough, except to handle long lines. If all lines are short, the main loop looks like (pseudo code):

last := readLine(stdin)
if lines.count > X then lines.removeFirst()
lines.addLast(last)
clearPrintedLines(count=X)
printLines()

My problem is, if last is long, the count of lines becomes wrong and more than X lines get printed.

Also, I want to preserve the commands output before the program runs, so clearing the whole screen is not an option (it would be simpler that clearing the last X lines, but less useful).

0

There are 0 best solutions below