Skip to content

Instantly share code, notes, and snippets.

@NigoroJr
Last active April 9, 2016 14:20
Show Gist options
  • Select an option

  • Save NigoroJr/678adfdf2f6500fee93e to your computer and use it in GitHub Desktop.

Select an option

Save NigoroJr/678adfdf2f6500fee93e to your computer and use it in GitHub Desktop.
#include <iostream> // For std::cout, std::flush, and std::endl
#include <chrono> // For std::chrono::milliseconds
#include <thread> // For std::this_thread::sleep_for
/** Update current line without putting the text on the next line
*
* This is done by using \r (carriage return) instead of \n
*/
int main(const int argc, char const* argv[]) {
std::cout << "Using \\r to go back to the beginning of the line." << std::endl;
std::cout << "Don't forget to std::flush" << std::endl;
std::cout << "Here we go!!\r" << std::flush;
// Wait for a bit
std::this_thread::sleep_for(std::chrono::seconds(3));
for (int i = 0; i < 10; i++) {
// Erase current line
std::cout << "\33[2K";
std::cout << "Hello! " << i << "\r" << std::flush;
// Wait for 500 ms
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << std::endl;
std::cout << "Done" << std::endl;
std::cout << "By not using carriage return," << std::endl;
std::cout << "I can also do something like this!\r" << std::flush;
// Wait for a bit
std::this_thread::sleep_for(std::chrono::seconds(3));
for (int i = 0; i < 20; i++) {
std::cout << "\33[2K";
std::cout << ">" <<std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
std::cout << "\33[2K\r";
std::cout << "Done!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment