首页 > 解决方案 > 在 c 中加载 bar 而不会弄乱其他输出

问题描述

我需要制作一个像 apt 数据包管理器中的加载栏(图片)

在此处输入图像描述

我找到了一个很酷的库来实现这一点,但问题是由于打印了进度条,加载条上方的所有文本(例如在图像中,“处理触发器...”)都没有打印出来。

如何在屏幕上同时打印加载栏和信息?

我的实际代码是这样的:

int main(void){
  while ((read = getline(&line, &len, file)) != -1) {
  char *newline = strchr(line, '\n');
  if (newline)
    *newline = 0;
  puts("");
  currline++;
  progressBar(currline, lines);
 }
}

void progressBar(size_t count, size_t lines){
  progressbar *progress = progressbar_new("Loading",lines); // function provided b the linked library (It initialize the progress bar so takes the name and the number of lines)
  for (size_t i = 0; i < lines; i++)
    progressbar_inc(progress); // provided by the library (it increments progressbar of a single step and call progressbar_update() that print the value by calling progressbar_draw()
  progressbar_finish(progress); // just free all allocated memory and print a newline
}

如果您想更详细地查看引用的函数,请参见此处。

标签: coutputloading

解决方案


推荐阅读