Skip to content

Commit

Permalink
(GH-525) Improve Write-Progress
Browse files Browse the repository at this point in the history
As a continuation of GH-8, download progress should be written inline,
but needs some improvements:
- Write an empty line when it has completed so that the next log item
   doesn't write to the same line.
- Improve the content of the progress message with both percentage and
  the status message.
  • Loading branch information
ferventcoder committed Jan 2, 2016
1 parent 78e87db commit ab82a78
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,27 @@ public override void WriteDebugLine(string message)
this.Log().Debug(message);
}

private bool hasLoggedStartProgress = false;
private bool hasLoggedFinalProgress = false;
public override void WriteProgress(long sourceId, ProgressRecord record)
{
{
if (record.PercentComplete == -1) return;

if (record.PercentComplete == 100) this.Log().Debug(() => "Progress: 100%{0}".format_with(" ".PadRight(20)));
if (hasLoggedFinalProgress) return;
if (!hasLoggedStartProgress)
{
hasLoggedStartProgress = true;
this.Log().Debug(record.Activity);
}

// http://stackoverflow.com/a/888569/18475
Console.Write("\rProgress: {0}%{1}".format_with(record.PercentComplete, " ".PadRight(20)));
Console.Write("\rProgress: {0}% - {1}".format_with(record.PercentComplete.to_string(), record.StatusDescription.PadRight(50, ' ')));

if (record.PercentComplete == 100 && !hasLoggedFinalProgress)
{
hasLoggedFinalProgress = true;
this.Log().Info("");
this.Log().Info(record.StatusDescription.Replace("Saving","Finished downloading. Saved"));
}
}

public override void WriteVerboseLine(string message)
Expand Down

0 comments on commit ab82a78

Please sign in to comment.