Writing over previously output lines in the command prompt with ruby

后端 未结 1 1112
盖世英雄少女心
盖世英雄少女心 2020-12-13 14:56

I\'ve run command line programs that output a line, and then update that line a moment later. But with ruby I can only seem to output a line and then another line.

相关标签:
1条回答
  • 2020-12-13 15:17

    Use \r to move the cursor to the beginning of the line. And you should not be using puts as it adds \n, use print instead. Like this:

    print "11MB 294K/s"
    print "\r"
    print "12MB 307K/s"
    

    One thing to keep in mind though: \r doesn't delete anything, it just moves the cursor back, so you would need to pad the output with spaces to overwrite the previous output (in case it was longer).

    By default when \n is printed to the standard output the buffer is flushed. Now you might need to use STDOUT.flush after print to make sure the text get printed right away.

    0 讨论(0)
提交回复
热议问题