用诅咒清除包裹线

发布于 2025-01-19 14:44:44 字数 1085 浏览 4 评论 0原文

当我在 bash 中运行以下代码时,在不太宽的终端(当前我的终端是 97 列宽)中,我没有看到预期的行为,

# foo.sh
tput sc  # save cursor
for i in $(seq 10); do    
    printf "%s" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    tput rc  # restore cursor
    tput ed  # clear to end of screen
done
echo "foo"

这些行实际上并没有被清除: 在此处输入图像描述

但是,如果我将字符串更改为比终端的宽度短,这样它就不会换行,然后它就可以正常工作,并且curses会清除该行,以便只有最后的“foo”显示在屏幕上。

# bar.sh
tput sc  # save cursor
for i in $(seq 10); do    
    printf "%s" "aaaaaaaaaaaaa"
    tput rc  # restore cursor
    tput ed  # clear to end of screen
done
echo "foo"

所以我的问题是:在存在换行的情况下如何清除屏幕从光标到底部,以及......这里发生了什么?

输入图片此处描述

When I run the following code in bash, in a terminal that's not too wide (currently my terminal is 97 columns wide), I don't see the expected behavior

# foo.sh
tput sc  # save cursor
for i in $(seq 10); do    
    printf "%s" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    tput rc  # restore cursor
    tput ed  # clear to end of screen
done
echo "foo"

The lines don't actually get cleared: enter image description here

However, if I change the string to be shorter than the width of the terminal, so that it doesn't wrap, then it works fine and curses clears the line so that only the final "foo" shows up on the screen.

# bar.sh
tput sc  # save cursor
for i in $(seq 10); do    
    printf "%s" "aaaaaaaaaaaaa"
    tput rc  # restore cursor
    tput ed  # clear to end of screen
done
echo "foo"

So my question is: How do I clear the screen from the cursor down to the bottom in the presence of line wrapping, and... what's going on here?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文