Comments on How to overwrite each line of STDOUT with the next one?
Parent
How to overwrite each line of STDOUT with the next one?
I wrote a filter program to overwrite each line with the next one.
$ cat /usr/local/bin/ovr
#!/bin/sh
sed '2,$s/^/\x1B[1A\x1B[K/';
Here's the behavior:
$ echo | ovr
$ echo 'foo' | ovr
foo
$ echo -e 'foo\nbar' | ovr
bar
$ echo -e 'foo\nbar\nbaz' | ovr
baz
$ echo -e 'foo\nbar\nbaz' | ovr | wc -l
3
It is useful for example when compiling a large program, where you might not want to pollute your screen with so many lines. For example, in build systems like the Linux kerenl one, for each file that's compiled, one line goes to stdout, of the form CC some/file.o
.
I usually compile it with make -j24 | ts -s
, to know for how long it's been running, from which I can guess how much is remaining. But I don't really need to see all lines; just the last one. So I do the following.
$ time make -j24 | ts -s | ovr
00:00:04 CC drivers/pinctrl/intel/pinctrl-sunrisepoint.o
The line will be updating all the time.
This program has a some flaw, which I'd like to fix.
If the line is long (longer than the terminal width), the overwriting doesn't work well, and only overwrites the part of the lines that has been written in the last "visual" line in the terminal.
For an 80-col terminal, this is an example:
$ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr
123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789ifoo
How can this bug be fixed? Is it possible to fix it?
Post
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
alx | (no comment) | Oct 2, 2023 at 23:49 |
In Bash, you could use the $COLUMNS
environment variable to detect the width of your terminal and truncate each line to that length in your sed script. Something like this should work:
sed "s/^\(.\{,$COLUMNS\}\).*$/\1/;2,\$s/^/\x1B[1A\x1B[K/"
For a sh-compatible alternative, replace $COLUMNS
with $(tput cols)
.
0 comment threads