在终端或控制台中就地输出进度
当您运行 git clone 时,它会就地更新进度。例如,接收到的对象的百分比发生了变化。
user@athena:~/cloj/src$ git clone git://git.boinkor.net/slime.git
Initialized empty Git repository in /home/user/cloj/src/slime/.git/
remote: Counting objects: 15936, done.
remote: Compressing objects: 100% (5500/5500), done.
Receiving objects: 28% (4547/15936), 3.16 MiB | 165 KiB/s
这是如何实现的呢?它是否使用 ncurses 或者更简单的东西,比如退格字符和常规字符输出的某种组合?
我对如何从 Ruby 完成这种控制台输出特别感兴趣。
编辑
我原来的问题已得到解答。但这里有一个附录。例如,当您使用 MPlayer 时,它不仅会更新一行以显示当前进度,还会更新上一行(例如,当您按下暂停键时)。
===== PAUSE =====
A: 79.9 (01:19.9) of 4718.0 ( 1:18:38.0) 0.3%
如何就地更新两行输出?
When you run git clone
, it updates progress in place. For example, the percentage of the objects received changes in place.
user@athena:~/cloj/src$ git clone git://git.boinkor.net/slime.git
Initialized empty Git repository in /home/user/cloj/src/slime/.git/
remote: Counting objects: 15936, done.
remote: Compressing objects: 100% (5500/5500), done.
Receiving objects: 28% (4547/15936), 3.16 MiB | 165 KiB/s
How is this acccomplished? Does it use ncurses or something even simpler, like some combination of backspace characters and regular character output?
I'm especially interested in how this kind of console output could be accomplished from Ruby.
EDIT
My original question is answered. But here's an addendum. When you use MPlayer, for example, it not only updates a line to show current progress, but also the previous line (e.g. when you press pause).
===== PAUSE =====
A: 79.9 (01:19.9) of 4718.0 ( 1:18:38.0) 0.3%
How would you update two lines of output in-place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用回车。 '\r' 通常应该可以工作。
Use carriage return. '\r' should usually work.
git/progress.c
Git只是发出一个回车符而不是换行符,终端将其解释为“移至第一列”。
git/progress.c
Git simply emits a carriage return and no line feed, which the terminal interprets as "move to first column".
您必须使用另一种方法(例如 Curses)来就地更新两行。
ablogaboutcode.com | web.archive.org
...和...
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/curses/rdoc/Curses.html
You'll have to use another method (like Curses) to update two lines in-place.
ablogaboutcode.com | web.archive.org
...and...
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/curses/rdoc/Curses.html
我为多行输出更新编写了一个小类:
受到 < 的启发代码>prydonius/spinning_cursor。有关示例用法,请参阅
test
方法。I wrote little class for multiline output update:
Inspired by
prydonius/spinning_cursor
. Seetest
method for example usage.Ruby 有许多诅咒库。我相信 rbbcurse 是维护得最多的。
There are a number of curses librbaries for Ruby. I believe rbbcurse is the most maintained.