<p>I have always wondered how people update a previous line in a command line. a great example of this is when using the wget command in linux. It creates an ASCII loading bar of sorts that looks like this:</p>

<blockquote>
  <p>[======>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] 37%</p>
</blockquote>

<p>and of course the loading bar moves and the percent changes, But it doesn't make a new line. I cannot figure out how to do this. Can someone point me in the right direction?</p>

[{'answer_id': 60226, 'author': 'hazzen', 'author_id': 5066, 'author_profile': 'https://Stackoverflow.com/users/5066', 'pm_score': 7, 'selected': True, 'text': '<p>There are two ways I know of to do this:</p>\n\n<ul>\n<li>Use the backspace escape character (\'\\b\') to erase your line</li>\n<li>Use the <code>curses</code> package, if your programming language of choice has bindings for it.</li>\n</ul>\n\n<p>And a Google revealed <a href="http://en.wikipedia.org/wiki/ANSI_escape_code" rel="noreferrer">ANSI Escape Codes</a>, which appear to be a good way. For reference, here is a function in C++ to do this:</p>\n\n<pre><code>void DrawProgressBar(int len, double percent) {\n  cout &lt;&lt; "\\x1B[2K"; // Erase the entire current line.\n  cout &lt;&lt; "\\x1B[0E"; // Move to the beginning of the current line.\n  string progress;\n  for (int i = 0; i &lt; len; ++i) {\n    if (i &lt; static_cast&lt;int&gt;(len * percent)) {\n      progress += "=";\n    } else {\n      progress += " ";\n    }\n  }\n  cout &lt;&lt; "[" &lt;&lt; progress &lt;&lt; "] " &lt;&lt; (static_cast&lt;int&gt;(100 * percent)) &lt;&lt; "%";\n  flush(cout); // Required.\n}\n</code></pre>\n'}, {'answer_id': 60227, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 6, 'selected': False, 'text': '<p>One way to do this is to repeatedly update the line of text with the current progress. For example:</p>\n\n<pre><code>def status(percent):\n    sys.stdout.write("%3d%%\\r" % percent)\n    sys.stdout.flush()\n</code></pre>\n\n<p>Note that I used <code>sys.stdout.write</code> instead of <code>print</code> (this is Python) because <code>print</code> automatically prints "\\r\\n" (carriage-return new-line) at the end of each line. I just want the carriage-return which returns the cursor to the start of the line. Also, the <code>flush()</code> is necessary because by default, <code>sys.stdout</code> only flushes its output after a newline (or after its buffer gets full).</p>\n'}, {'answer_id': 60235, 'author': 'Steven Murawski', 'author_id': 1233, 'author_profile': 'https://Stackoverflow.com/users/1233', 'pm_score': 2, 'selected': False, 'text': '<p>PowerShell has a Write-Progress cmdlet that creates an in-console progress bar that you can update and modify as your script runs.</p>\n'}, {'answer_id': 1618758, 'author': 'Justin', 'author_id': 195960, 'author_profile': 'https://Stackoverflow.com/users/195960', 'pm_score': 0, 'selected': False, 'text': '<p>If your using a scripting language you could use the "tput cup" command to get this done...\nP.S. This is a Linux/Unix thing only as far as I know...</p>\n'}, {'answer_id': 11173569, 'author': 'Blaker', 'author_id': 1048120, 'author_profile': 'https://Stackoverflow.com/users/1048120', 'pm_score': 2, 'selected': False, 'text': '<p>As a follow up to <a href="https://stackoverflow.com/a/60227/1048120">Greg\'s answer</a>, here is an extended version of his function that allows you to display multi-line messages; just pass in a list or tuple of the strings you want to display/refresh.</p>\n\n<pre><code>def status(msgs):\n    assert isinstance(msgs, (list, tuple))\n\n    sys.stdout.write(\'\'.join(msg + \'\\n\' for msg in msgs[:-1]) + msgs[-1] + (\'\\x1b[A\' * (len(msgs) - 1)) + \'\\r\')\n    sys.stdout.flush()\n</code></pre>\n\n<p>Note: I have only tested this using a linux terminal, so your mileage may vary on Windows-based systems.</p>\n'}, {'answer_id': 11325990, 'author': 'naren', 'author_id': 1193863, 'author_profile': 'https://Stackoverflow.com/users/1193863', 'pm_score': 2, 'selected': False, 'text': '<p>Here is the answer for your question...  (python)</p>\n\n<pre><code>def disp_status(timelapse, timeout):\n  if timelapse and timeout:\n     percent = 100 * (float(timelapse)/float(timeout))\n     sys.stdout.write("progress : ["+"*"*int(percent)+" "*(100-int(percent-1))+"]"+str(percent)+" %")\n     sys.stdout.flush()\n     stdout.write("\\r  \\r")\n</code></pre>\n'}, {'answer_id': 13738348, 'author': 'hustljian', 'author_id': 1048072, 'author_profile': 'https://Stackoverflow.com/users/1048072', 'pm_score': 2, 'selected': False, 'text': '<p>below is my answer,use the windows API<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682055%28v=vs.85%29.aspx" rel="nofollow">Consoles(Windows)</a>, coding of C.</p>\n\n<pre><code>/*\n* file: ProgressBarConsole.cpp\n* description: a console progress bar Demo\n* author: lijian &lt;hustlijian@gmail.com&gt;\n* version: 1.0\n* date: 2012-12-06\n*/\n#include &lt;stdio.h&gt;\n#include &lt;windows.h&gt;\n\nHANDLE hOut;\nCONSOLE_SCREEN_BUFFER_INFO bInfo;\nchar charProgress[80] = \n    {"================================================================"};\nchar spaceProgress = \' \';\n\n/*\n* show a progress in the [row] line\n* row start from 0 to the end\n*/\nint ProgressBar(char *task, int row, int progress)\n{\n    char str[100];\n    int len, barLen,progressLen;\n    COORD crStart, crCurr;\n    GetConsoleScreenBufferInfo(hOut, &amp;bInfo);\n    crCurr = bInfo.dwCursorPosition; //the old position\n    len = bInfo.dwMaximumWindowSize.X;\n    barLen = len - 17;//minus the extra char\n    progressLen = (int)((progress/100.0)*barLen);\n    crStart.X = 0;\n    crStart.Y = row;\n\n    sprintf(str,"%-10s[%-.*s&gt;%*c]%3d%%", task,progressLen,charProgress, barLen-progressLen,spaceProgress,50);\n#if 0 //use stdand libary\n    SetConsoleCursorPosition(hOut, crStart);\n    printf("%s\\n", str);\n#else\n    WriteConsoleOutputCharacter(hOut, str, len,crStart,NULL);\n#endif\n    SetConsoleCursorPosition(hOut, crCurr);\n    return 0;\n}\nint main(int argc, char* argv[])\n{\n    int i;\n    hOut = GetStdHandle(STD_OUTPUT_HANDLE);\n    GetConsoleScreenBufferInfo(hOut, &amp;bInfo);\n\n    for (i=0;i&lt;100;i++)\n    {\n        ProgressBar("test", 0, i);\n        Sleep(50);\n    }\n\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 26219138, 'author': 'icenac', 'author_id': 3910576, 'author_profile': 'https://Stackoverflow.com/users/3910576', 'pm_score': 5, 'selected': False, 'text': "<p>The secret is to print only \\r instead of \\n or \\r\\n at the and of the line.</p>\n\n<p>\\r is called carriage return and it moves the cursor at the start of the line</p>\n\n<p>\\n is called line feed and it moves the cursor on the next line\nIn the console. If you only use \\r you overwrite the previously written line. \nSo first write a line like the following:</p>\n\n<pre><code>[          ]\n</code></pre>\n\n<p>then add a sign for each tick</p>\n\n<pre><code>\\r[=         ]\n\n\\r[==        ]\n\n...\n\n\\r[==========]\n</code></pre>\n\n<p>and so on.\nYou can use 10 chars, each representing a 10%. \nAlso, if you want to display a message when finished, don't forget to also add enough white chars so that you overwrite the previously written equal signs like so:</p>\n\n<pre><code>\\r[done      ]\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/60221', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2128/']