Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

50%
+0 −0
Q&A How to overwrite each line of STDOUT with the next one?

Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them. This has the benefit that s...

posted 1y ago by alx‭  ·  edited 1y ago by alx‭

Answer
#20: Post edited by user avatar alx‭ · 2023-10-10T03:08:09Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr --no-alt
  • foo
  • $ echo '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o' | ovr --no-alt
  • 123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789o
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. If that's wanted, you can maybe pipe with pee(1):
  • ```sh
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | pee "ovr --no-alt" "tail -n2"
  • e
  • xxxxx
  • bz
  • bar
  • bz
  • ```
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr --no-alt
  • foo
  • $ echo '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o' | ovr --no-alt
  • 123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789o
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. If that's wanted, you can pipe also to tail(1) with pee(1):
  • ```sh
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | pee "ovr --no-alt" "tail -n2"
  • e
  • xxxxx
  • bz
  • bar
  • bz
  • ```
#19: Post edited by user avatar alx‭ · 2023-10-10T03:07:34Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr --no-alt
  • foo
  • $ echo '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o' | ovr --no-alt
  • 123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789o
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr --no-alt
  • foo
  • $ echo '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o' | ovr --no-alt
  • 123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789o
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. If that's wanted, you can maybe pipe with pee(1):
  • ```sh
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | pee "ovr --no-alt" "tail -n2"
  • e
  • xxxxx
  • bz
  • bar
  • bz
  • ```
#18: Post edited by user avatar alx‭ · 2023-10-10T03:02:44Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr --no-alt
  • foo
  • $ echo '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o' | ovr --no-alt
  • 123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789o
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#17: Post edited by user avatar alx‭ · 2023-10-10T03:00:50Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o
  • foo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o
  • foo' | ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#16: Post edited by user avatar alx‭ · 2023-10-10T02:54:08Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This already includes the improvements from [this answer](https://linux.codidact.com/posts/289869/289875#answer-289875).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This uses `setterm --linewrap off` to reliably trim long lines, as learnt from [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#15: Post edited by user avatar alx‭ · 2023-10-10T02:51:59Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This already includes the improvements from [this answer](https://linux.codidact.com/posts/289869/289875#answer-289875).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This already includes the improvements from [this answer](https://linux.codidact.com/posts/289869/289875#answer-289875).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#14: Post edited by user avatar alx‭ · 2023-10-10T02:51:46Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • This already includes the improvements from [this answer](https://linux.codidact.com/posts/289869/289875#answer-289875).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • setterm --linewrap off;
  • sed -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • setterm --linewrap on;
  • ```
  • This already includes the improvements from [this answer](https://linux.codidact.com/posts/289869/289875#answer-289875).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
#13: Post edited by user avatar alx‭ · 2023-10-10T02:48:01Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • This already includes the improvements from [this answer](https://linux.codidact.com/posts/289869/289875#answer-289875).
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
#12: Post edited by user avatar alx‭ · 2023-10-10T02:47:15Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen, so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen as in [this other answer](https://linux.codidact.com/posts/289869/289899#answer-289899), so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
#11: Post edited by user avatar alx‭ · 2023-10-10T02:46:32Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen, so I can choose; thus the --no-alt option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen, so I can choose; thus the `--no-alt` option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
#10: Post edited by user avatar alx‭ · 2023-10-10T02:46:15Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo\nbar' | ovr --no-alt
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • (I did actually implement it as an option to a script that can also use alternate screen, so I can choose; thus the --no-alt option.)
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
#9: Post edited by user avatar alx‭ · 2023-10-10T02:45:12Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr
  • $ echo foo | ovr
  • foo
  • $ echo -e 'foo
  • bar' | ovr
  • bar
  • $ echo -e 'foo
  • bar
  • baz' | ovr
  • baz
  • $ echo -e 'foo
  • bar
  • baz' | ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A/' \
  • -e '2,$ s/^/\x1B[K/';
  • ```
  • ```sh
  • $ echo | ovr --no-alt
  • $ echo foo | ovr --no-alt
  • foo
  • $ echo -e 'foo
  • bar' | ovr --no-alt
  • bar
  • $ echo -e 'foo
  • bar
  • baz' | ovr --no-alt
  • baz
  • $ echo -e 'foo
  • bar
  • baz' | ovr --no-alt | wc -l
  • 3
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bz) | ovr --no-alt
  • e
  • xxxxx
  • bz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr --no-alt
  • foo
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep. And it still has the issue that tabs or other characters may take up a number of columns different than 1.
#8: Post undeleted by user avatar alx‭ · 2023-10-10T02:37:48Z (about 1 year ago)
#7: Post deleted by user avatar alx‭ · 2023-10-10T02:33:09Z (about 1 year ago)
#6: Post edited by user avatar alx‭ · 2023-10-10T02:32:38Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • Note that it doesn't seem to need to trim lines to keep it working.
  • ```sh
  • $ 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
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ovr
  • e
  • xxxxx
  • bazz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ovr
  • foo
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • ```sh
  • $ 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
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#5: Post edited by user avatar alx‭ · 2023-10-10T02:31:15Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • Note that it doesn't seem to need to trim lines to keep it working.
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo
  • bar' | ./bin/ovr
  • bar
  • $ echo -e 'foo
  • bar
  • baz' | ./bin/ovr
  • baz
  • $ echo -e 'foo
  • bar
  • baz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o
  • foo' | ./bin/ovr
  • foo
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • Note that it doesn't seem to need to trim lines to keep it working.
  • ```sh
  • $ echo | ovr
  • $ echo foo | ovr
  • foo
  • $ echo -e 'foo
  • bar' | ovr
  • bar
  • $ echo -e 'foo
  • bar
  • baz' | ovr
  • baz
  • $ echo -e 'foo
  • bar
  • baz' | ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ovr
  • e
  • xxxxx
  • bazz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o
  • foo' | ovr
  • foo
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#4: Post edited by user avatar alx‭ · 2023-10-10T02:29:12Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • Note that it doesn't seem to need to trim lines to keep it working.
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo\nbar' | ./bin/ovr
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • Note that it doesn't seem to need to trim lines to keep it working.
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo\nbar' | ./bin/ovr
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • $ echo -e '123456789q123456789w123456789e123456789r123456789t123456789y123456789u123456789i123456789o\nfoo' | ./bin/ovr
  • foo
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#3: Post edited by user avatar alx‭ · 2023-10-10T02:28:51Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo\nbar' | ./bin/ovr
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen. And it doesn't seem to need to trim lines to kepp it working!
  • A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • Note that it doesn't seem to need to trim lines to keep it working.
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo\nbar' | ./bin/ovr
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen.
  • A disadvantage is that it's not possible to set the number of lines to keep.
#2: Post edited by user avatar alx‭ · 2023-10-10T02:28:03Z (about 1 year ago)
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
  • -e '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo\nbar' | ./bin/ovr
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen. A disadvantage is that it's not possible to set the number of lines to keep.
  • Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.
  • This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).
  • ```sh
  • #!/bin/sh
  • sed '$ ! s/$/\x1B[1A\x1B[K/';
  • ```
  • ```sh
  • $ echo | ./bin/ovr
  • $ echo foo | ./bin/ovr
  • foo
  • $ echo -e 'foo\nbar' | ./bin/ovr
  • bar
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr
  • baz
  • $ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
  • 1
  • $ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
  • e
  • xxxxx
  • bazz
  • ```
  • This implementation is much simpler than using the alternate screen. And it doesn't seem to need to trim lines to kepp it working!
  • A disadvantage is that it's not possible to set the number of lines to keep.
#1: Initial revision by user avatar alx‭ · 2023-10-10T02:26:35Z (about 1 year ago)
Instead of having new stdout lines overwrite the previous one, it seems better to specify that old stdout lines should be overwritten by anything that comes after them.

This has the benefit that stdout cannot overwrite stderr (or anything else that goes in the tty).

```sh
#!/bin/sh

sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
    -e '$ ! s/$/\x1B[1A\x1B[K/';
```

```sh
$ echo | ./bin/ovr 

$ echo foo | ./bin/ovr 
foo
$ echo -e 'foo\nbar' | ./bin/ovr 
bar
$ echo -e 'foo\nbar\nbaz' | ./bin/ovr 
baz
$ echo -e 'foo\nbar\nbaz' | ./bin/ovr | wc -l
1
$ (echo foo; echo e >&2; echo bar; echo xxxxx >&2; echo bazz) | ./bin/ovr
e
xxxxx
bazz
```

This implementation is much simpler than using the alternate screen.  A disadvantage is that it's not possible to set the number of lines to keep.