如何解决Bash脚本:使用手表分别更新屏幕的上半部分
我正在处理一个bash脚本,该脚本在屏幕的前16行显示一个随机的ANSI艺术横幅,然后在其余行中显示我的todo.txt列表的项目视图。它打算放在tmux窗口的一个窗格中,以便屏幕的右三分之一(大约80x48个字符)始终显示我的待办事项列表。
现在,我的横幅广告在加载时随机分配,然后在我添加或删除内容时通过fswatch更新待办事项列表。我还想每15分钟左右更新一次横幅,因此我想使用诸如watch之类的命令运行命令,以每15分钟间隔显示一个随机ANSI横幅,但是当使用watch时,该脚本只会使整个屏幕空白。 (下面的代码有10秒的间隔用于测试)
是否有更好的方法来做到这一点,或者有一种让手表开始正确输出横幅的方法?
这是脚本:
#!/bin/bash
clear
banner.sh
/usr/local/bin/todo.sh projectview | fold -w 80 -s
watch -t -c -n 10 banner.sh # this just gives me a blank screen and hangs
fswatch -l 1 -o --event=Updated -e "~/.todo/.*" -i "todo.txt" ~/.todo | while read;
do
tput cup 18 0 && tput ed && /usr/local/bin/todo.sh projectview | fold -w 80 -s # draw the todo list starting on the 18th line.
done
这是banner.sh:
#!/bin/bash
filename=`ls -d ~/banners/* | shuf -n 1`
tput cup 0 0 && cat $filename && echo ""
解决方法
由于观看ls或其他一些彩色输出工作正常,看来cat不会以手表可以接受的格式产生彩色输出,但是涉及cat和彩色输出的任何事情都不能使屏幕黑屏。 / p>
我的答案涉及放下手表,只是组合脚本,在后台运行bash循环,每60秒更新一次横幅。不幸的是,对于此循环或待办事项列表,对STDOUT的写入均不是原子操作,因此我只需要简单地更新包含标题和列表的变量,并在列表文件每次更改或时更新整个屏幕>现在是60秒,现在是换新横幅的时候了。
这不是理想的,因为我要重画一堆我不必重画的东西,但这是解决我找不到找到写入方法的唯一方法。 STDOUT原子。
#!/bin/bash
clear
# choose a random banner and initialize variables
filename=`ls -d ~/banners/*.ans | shuf -n 1`
banner=$(head -c -1 $filename)
function todo {
# move to the top left of the screen but don't clear
tput cup 0 0
# display the banner
echo "${banner}"
# make sure we're on line 16
tput cup 16 0
# update the todo list,so that it will clear each line to the end
todolist=$(/usr/local/bin/todo.sh today | fold -w 80 -s | sed -r 's/$/\\033\[K/g')
# display the todo list
echo -e "${todolist}"
# clear the rest of the screen
tput ed
}
# CTRL-C exits cleanly,killing the banner process that was running in the background
trap 'trap - SIGTERM && tput cnorm && clear && kill 0' SIGINT SIGTERM EXIT
while true; do
# choose a new banner
filename=`ls -d ~/banners/*.ans | shuf -n 1`
banner=$(head -c -1 $filename)
# redraw the screen
todo
# do this every 60 seconds
sleep 60
done &
# make the cursor invisible for now
tput civis
fswatch -l 1 -o --event=Updated -e "~/.todo/.*" -i "todo.txt" ~/.todo | while read;
do
# redraw the screen
todo
done
# make the cursor visible again
tput cnorm
exit
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。