render method

void render({
  1. bool force = false,
})

Render the progress bar.

If force is true, the progress bar will be redrawn even if the output hasn't changed since the last render.

Implementation

void render({bool force = false}) {
  // Abort if stdout isn't attached to a terminal.
  assert(stdout.hasTerminal, 'Cannot render - no terminal is available!');

  // Calculate the progress percentage.
  final progress = min(max(_value / _total, 0.0), 1.0);

  // Calculate the elapsed time.
  final elapsed = _startTime == null
      ? Duration.zero
      : DateTime.now().difference(_startTime!);

  // Format the output.
  var output = formatter(
    _value,
    _total,
    progress,
    elapsed,
  );

  // Compute the available space for the bar.
  final textLength = output.replaceAll(':bar', '').length;
  final availableSpace = max(0, stdout.terminalColumns - textLength);

  // Use the set width for the rendered bar width, unless there isn't enough
  // space.
  final computedWidth =
      width == null ? availableSpace : min(width!, availableSpace);

  // If the output string length is equal to the text length, there's no :bar
  // token.
  if (output.length != textLength) {
    // Render the progress bar and add it to the output string.
    final barProgressLength = (computedWidth * progress).round();
    final completeBarProgress = _completeChar * barProgressLength;
    final incompleteBarProgress =
        _incompleteChar * (computedWidth - barProgressLength);
    output = output.replaceAll(
        formatterBarToken, completeBarProgress + incompleteBarProgress);
  }

  // Output the progress bar.
  _output(output, force: force);
}