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,然後把相應的進度字符填充進去,最後再輸出了。
當然這次沒有太細緻的分析,邊猜帶蒙的。

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