有后台任务可以通知终端打印新提示的方法吗? [英] Is there a way for a backgrounded task to inform the terminal to print a new prompt?
问题描述
我想编写一个程序, 可以作为后台任务运行(即my-thing &
),并在必要时提供精美的输出.
I want to write a program that can be run as a backgrounded task (i.e. my-thing &
), and that will beautifully provide relevant output when necessary.
但是,随着后台任务打印到终端,总会有这种令人讨厌的小进展:
However, with background tasks that print to the terminal, there's always this annoying little progression:
bash-3.2$ my-thing &▏
bash-3.2$ my-thing &▏
bash-3.2$ my-thing &
bash-3.2$ -- Hello from my-thing!
-- now let me start doing what I do best ...
▏
bash-3.2$ my-thing &
bash-3.2$ -- Hello from my-thing!
-- now let me start doing what I do best ...
▏
…请注意,我们现在在光标之前没有任何提示,更不用说有一个丑陋的错误提示,我的程序在已打印提示的情况下吐出了输出.
… notice that we now have no prompt before the cursor, not to mention that there's an ugly erraneous prompt where my program spat out output while a prompt had already been printed.
因此,我想要三件事,其中只有一件事我不知道如何去做:
Thus, I want three things, only one of which I don't have any idea how to go about doing:
- 检测我的程序是否在后台运行,并相应地更改我的输出/操作(即打印出较少的无用信息;将更多信息排入队列,仅在再次显示该程序时才会输出;等等)
- 在突然将信息打印到我们有背景的终端上时清除提示(如果已经存在) 在完成向终端机打印的所有操作后,
- 通知终端机,以某种方式迫使其在我添加的内容下方重新绘制新提示
- Detect whether my program is being run in the background, and alter my output/operation accordingly (i.e. print out less useless information; queue further information to only be output when the program is foregrounded again; etceteras)
- Clear out the prompt (if it already exists) when suddenly printing information to a terminal we've been backgrounded in
- Inform the terminal after I've finished whatever I'm printing to the terminal for, somehow forcing it to re-draw a new prompt below the content I've added
第三个是我绝对不知道该怎么做的唯一一个.尽管我在其他两个方面有一些暗示,但也将对其中的任何链接或建议表示赞赏.
The third is the only one I have absolutely no idea how to go about; though I've got some inklings on the other two, any links or suggestions there would be appreciated as well.
同样赞赏的是,可以链接到任何已经具有运行该程序的模式的程序(任何语言/环境);因为我可以尝试对它们的实现进行反向工程/研究.
Equally appreciated, even, would be links to any programs (any language/environment) which already have modes that operate this; as I could attempt to reverse-engineer/spelunk their implementation.
推荐答案
这对我有用:
echo 'sleep 1; echo; echo hi' > /tmp/hi
chmod +x /tmp/hi
zsh
/tmp/hi &
asdfsadf # type whatever, here.
'hi'得到回显后,zsh知道重绘提示并保持原样的'asdfasdf'.
After the 'hi' gets echo'd, zsh knows to redraw your prompt with the 'asdfasdf' intact.
因此,毫不奇怪:zsh比bash更好.
So, no big surprise: zsh being better than bash.
这篇关于有后台任务可以通知终端打印新提示的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!