Use carriage return. '\r' should usually work.
git/progress.c
...
eol = done ? done : " \r";
...
fprintf(stderr, "...%s", ..., eol);
fflush(stderr);
Git simply emits a carriage return and no line feed, which the terminal interprets as "move to first column".
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
I wrote little class for multiline output update:
class ConsoleReset
# Unix
# Contains a string to clear the line in the shell
CLR = "\e[0K"
# ANSI escape sequence for hiding terminal cursor
ESC_CURS_INVIS = "\e[?25l"
# ANSI escape sequence for showing terminal cursor
ESC_CURS_VIS = "\e[?25h"
# ANSI escape sequence for clearing line in terminal
ESC_R_AND_CLR = "\r#{CLR}"
# ANSI escape sequence for going up a line in terminal
ESC_UP_A_LINE = "\e[1A"
def initialize
@first_call = true
end
def reset_line(text = '')
# Initialise ANSI escape string
escape = ""
# The number of lines the previous message spanned
lines = text.strip.lines.count - 1
# Clear and go up a line
lines.times { escape += "#{ESC_R_AND_CLR}#{ESC_UP_A_LINE}" }
# Clear the line that is to be printed on
# escape += "#{ESC_R_AND_CLR}"
# Console is clear, we can print!
STDOUT.print escape if !@first_call
@first_call = false
print text
end
def hide_cursor
STDOUT.print(ESC_CURS_INVIS)
end
def show_cursor
STDOUT.print(ESC_CURS_VIS)
end
def test
hide_cursor
5.times do |i|
line = ['===========================================']
(1..10).each do |num|
line << ["#{num}:\t#{rand_num}"]
end
line << ['===========================================']
line = line.join("\n")
reset_line(line)
sleep 1
end
show_cursor
puts ''
end
private
def rand_num
rand(10 ** rand(10))
end
end
Inspired by prydonius/spinning_cursor
. See test
method for example usage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With