terminal - ANSI escape code ESC[0E doesn't work as expected -
i trying out example code this answer.
#include <iostream> #include <thread> #include <chrono> void drawprogressbar(int, double); int main() { drawprogressbar(30, .25); std::this_thread::sleep_for(std::chrono::seconds(1)); drawprogressbar(30, .50); std::this_thread::sleep_for(std::chrono::seconds(1)); drawprogressbar(30, .75); std::this_thread::sleep_for(std::chrono::seconds(1)); drawprogressbar(30, 1); return 0; } void drawprogressbar(int len, double percent) { std::cout << "\x1b[2k"; // erase entire current line. std::cout << "\x1b[0e"; // move beginning of current line. std::string progress; (int = 0; < len; ++i) { if (i < static_cast<int>(len * percent)) { progress += "="; } else { progress += " "; } } std::cout << "[" << progress << "] " << (static_cast<int>(100 * percent)) << "%" << std::flush; }
the expected behavior progress bar so:
[======= ] 25%
which update 3 times on same line, ending as:
[==============================] 100%
after 3 seconds.
while each progress bar gets erased expected, next progress bar drawn 1 line down, not on same line expecting to.
the documentation linked in answer (wikipedia) says csi n e
(esc[ne
) n
integer:
moves cursor beginning of line n (default 1) lines down.
so expect csi 0 e
(esc[0e
) move cursor beginning of current line (the line 0
lines down).
why doesn't it? also, how can achieve intended behavior?
i'm using terminal.app
on os x run program.
hmm, try:
std::cout << "\r";
instead of:
std::cout << "\x1b[2k"; // erase entire current line. std::cout << "\x1b[0e"; // move beginning of current line.
this carriage return, should reposition cursor @ beginning of line.
(by way, kudos commenting code. love when people on here :) )
Comments
Post a Comment