Wget 下载进度条是如何实现的?

Wget 下载进度条是如何实现的?

自从有一次用过wget后对它如何实现以下这样的进度条有些兴趣,当然做为一个不入流的程序员心里也是有自己的想法。
wget进度条

今天Google到wget老家https://www.gnu.org/software/wget/,下载了1.9的源码,找到progress.c。

      /* The progress bar: "[====>      ]" or "[++==>      ]". */
      /* Size of the initial portion. */
      int insz = (double)bp->initial_length / bp->total_length * progress_size;

      /* Size of the downloaded portion. */
      int dlsz = (double)size / bp->total_length * progress_size;

      char *begin;
      int i;

      assert (dlsz <= progress_size);
      assert (insz <= dlsz);

      *p++ = '[';
      begin = p;

      /* Print the initial portion of the download with '+' chars, the
     rest with '=' and one '>'.  */
      for (i = 0; i < insz; i++)
    *p++ = '+';
      dlsz -= insz;
      if (dlsz > 0)
    {
      for (i = 0; i < dlsz - 1; i++)
        *p++ = '=';
      *p++ = '>';
    }

      while (p - begin < progress_size)
    *p++ = ' ';
      *p++ = ']';

从上面的代码可以看到,提前准备好的buffer,然后把相应的进度字符填充进去,最后再输出了。
当然这次没有太细致的分析,边猜带蒙的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章