elkuku/console-progressbar

此类为您提供了一个易于使用的进度条界面。

2.0 2021-02-11 13:00 UTC

This package is auto-updated.

Last update: 2024-09-12 00:44:36 UTC


README

通常,要生成进度条,您需要调用构造函数,然后在循环中调用update()方法。

您可以使用reset()方法重用对象来显示另一个进度条。它的参数与构造函数相同。

构造函数期望5到6个参数

  • 第一个参数是用于显示进度条的格式字符串。它可能(并且应该)包含占位符,该类将用进度条本身、进度百分比等信息替换这些占位符。当前占位符包括:%bar% 进度条 %current% 当前值 %max% 最大值(目标值) %fraction% 与%current%/%max%相同 %percent% 百分比状态 %elapsed% 已用时间 %estimate% 剩余时间的估计。更多占位符将随后添加。例如,格式字符串:“* stuff.tar %fraction% KB [%bar%] %percent%” 将生成如下进度条:“* stuff.tar 391/900 KB [=====>---------] 43.44%”
  • 第二个参数是要填充进度条的字符串。在上面的示例中,使用了字符串“=>”。如果您传递的字符串太短(如本示例中的“=>”),则将使用最左边的字符将其填充到所需的大小。如果您传递的字符串太长,则将从左侧截断多余的字符。
  • 第三个参数是要填充进度条“空”空间的字符串。在上面的示例中,这将是“-”。如果您传递的字符串太短(如本示例中的“-”),则将使用最右边的字符将其填充到所需的大小。如果字符串太短,则将从右侧截断多余的字符。
  • 第四个参数指定显示的宽度。如果选项保持不变,它将告诉显示应使用多少个字符。如果将“absolute_width”选项设置为false,它将告诉实际进度条(替换%bar%占位符)应使用多少个字符。
  • 第五个参数是进度条的目标数字。例如,如果您想显示一个115 KB大文件的下载进度条,您将在这里传递115。
  • 第六个参数是可选的。如果传递,它应包含一个选项数组。例如,传递array('absolute_width' => false)将设置absolute_width选项为false。当前选项包括
    option             | def.  |  meaning
    --------------------------------------------------------------------
    percent_precision  | 2     |  Number of decimal places to show when
                       |       |  displaying the percentage.
    fraction_precision | 0     |  Number of decimal places to show when
                       |       |  displaying the current or target
                       |       |  number.
    percent_pad        | ' '   |  Character to use when padding the
                       |       |  percentage to a fixed size. Senseful
                       |       |  values are ' ' and '0', but any are
                       |       |  possible.
    fraction_pad       | ' '   |  Character to use when padding max and
                       |       |  current number to a fixed size.
                       |       |  Senseful values are ' ' and '0', but
                       |       |  any are possible.
    width_absolute     | true  |  If the width passed as an argument
                       |       |  should mean the total size (true) or
                       |       |  the width of the bar alone.
    ansi_terminal      | false |  If this option is true, a better
                       |       |  (faster) method for erasing the bar is
                       |       |  used.
    ansi_clear         | false |  If the bar should be cleared everytime
    num_datapoints     | 5     |  How many datapoints to use to create
                       |       |  the estimated remaining time

update()方法只期望一个参数,即当前状态(在构造函数传递给的目标数字之间),并刷新显示(或开始显示,如果是第一次调用)。