我一直想知道人们如何更新命令行中的前一行。一个很好的例子是在 Linux 中使用 wget 命令。它创建了一个 ASCII 加载栏,如下所示:

[======>                    ] 37%

当然,加载栏会移动并且百分比会发生变化,但它不会产生新的一行。我不知道该怎么做。有人能指出我正确的方向吗?

有帮助吗?

解决方案

我知道有两种方法可以做到这一点:

  • 使用退格转义字符 ('\b') 删除该行
  • 使用 curses 包,如果您选择的编程语言有绑定。

谷歌透露 ANSI 转义码, ,这似乎是一个好方法。作为参考,下面是 C++ 中执行此操作的函数:

void DrawProgressBar(int len, double percent) {
  cout << "\x1B[2K"; // Erase the entire current line.
  cout << "\x1B[0E"; // Move to the beginning of the current line.
  string progress;
  for (int i = 0; i < len; ++i) {
    if (i < static_cast<int>(len * percent)) {
      progress += "=";
    } else {
      progress += " ";
    }
  }
  cout << "[" << progress << "] " << (static_cast<int>(100 * percent)) << "%";
  flush(cout); // Required.
}

其他提示

一种方法是使用当前进度重复更新文本行。例如:

def status(percent):
    sys.stdout.write("%3d%%\r" % percent)
    sys.stdout.flush()

请注意,我使用了 sys.stdout.write 代替 print (这是Python)因为 print 自动在每行末尾打印“ ”(回车换行符)。我只想要回车符,将光标返回到行的开头。另外, flush() 是必要的,因为默认情况下, sys.stdout 仅在换行符后(或缓冲区已满后)刷新其输出。

秘密是仅在行的和处打印 而不是 或 。

称为回车符,它将光标移动到行的开头

n称为行馈电,它在控制台的下一行上移动光标。如果仅使用 ,则会覆盖先前写入的行。所以首先写一行如下:

[          ]

然后为每个刻度添加一个符号

\r[=         ]

\r[==        ]

...

\r[==========]

等等。您可以使用 10 个字符,每个字符代表 10%。另外,如果您想在完成后显示一条消息,请不要忘记添加足够的白色字符,以便覆盖之前编写的等号,如下所示:

\r[done      ]

下面是我的答案,使用Windows API控制台(Windows), ,C 的编码。

/*
* file: ProgressBarConsole.cpp
* description: a console progress bar Demo
* author: lijian <hustlijian@gmail.com>
* version: 1.0
* date: 2012-12-06
*/
#include <stdio.h>
#include <windows.h>

HANDLE hOut;
CONSOLE_SCREEN_BUFFER_INFO bInfo;
char charProgress[80] = 
    {"================================================================"};
char spaceProgress = ' ';

/*
* show a progress in the [row] line
* row start from 0 to the end
*/
int ProgressBar(char *task, int row, int progress)
{
    char str[100];
    int len, barLen,progressLen;
    COORD crStart, crCurr;
    GetConsoleScreenBufferInfo(hOut, &bInfo);
    crCurr = bInfo.dwCursorPosition; //the old position
    len = bInfo.dwMaximumWindowSize.X;
    barLen = len - 17;//minus the extra char
    progressLen = (int)((progress/100.0)*barLen);
    crStart.X = 0;
    crStart.Y = row;

    sprintf(str,"%-10s[%-.*s>%*c]%3d%%", task,progressLen,charProgress, barLen-progressLen,spaceProgress,50);
#if 0 //use stdand libary
    SetConsoleCursorPosition(hOut, crStart);
    printf("%s\n", str);
#else
    WriteConsoleOutputCharacter(hOut, str, len,crStart,NULL);
#endif
    SetConsoleCursorPosition(hOut, crCurr);
    return 0;
}
int main(int argc, char* argv[])
{
    int i;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hOut, &bInfo);

    for (i=0;i<100;i++)
    {
        ProgressBar("test", 0, i);
        Sleep(50);
    }

    return 0;
}

PowerShell 有一个 Write-Progress cmdlet,可创建一个控制台内进度条,您可以在脚本运行时更新和修改该进度条。

这是您问题的答案...(Python)

def disp_status(timelapse, timeout):
  if timelapse and timeout:
     percent = 100 * (float(timelapse)/float(timeout))
     sys.stdout.write("progress : ["+"*"*int(percent)+" "*(100-int(percent-1))+"]"+str(percent)+" %")
     sys.stdout.flush()
     stdout.write("\r  \r")

作为后续 格雷格的回答, ,这里是他的功能的扩展版本,可以让你显示多行消息;只需传入要显示/刷新的字符串列表或元组。

def status(msgs):
    assert isinstance(msgs, (list, tuple))

    sys.stdout.write(''.join(msg + '\n' for msg in msgs[:-1]) + msgs[-1] + ('\x1b[A' * (len(msgs) - 1)) + '\r')
    sys.stdout.flush()

笔记:我仅使用 Linux 终端对此进行了测试,因此您的情况在基于 Windows 的系统上可能会有所不同。

如果您使用脚本语言,您可以使用“tput cup”命令来完成此操作......附:据我所知,这是 Linux/Unix 的事情......

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top