Skip to content

Reset progress messages at end of REPL #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
Expand All @@ -22,6 +23,12 @@ internal class EditorServicesConsolePSHostUserInterface : PSHostUserInterface

private readonly PSHostUserInterface _consoleHostUI;

/// <summary>
/// We use a ConcurrentDictionary because ConcurrentHashSet does not exist, hence the value
/// is never actually used, and `WriteProgress` must be thread-safe.
/// </summary>
private readonly ConcurrentDictionary<(long, int), object> _currentProgressRecords = new();

public EditorServicesConsolePSHostUserInterface(
ILoggerFactory loggerFactory,
IReadLineProvider readLineProvider,
Expand Down Expand Up @@ -103,7 +110,35 @@ public override PSCredential PromptForCredential(string caption, string message,

public override void WriteLine(string value) => _underlyingHostUI.WriteLine(value);

public override void WriteProgress(long sourceId, ProgressRecord record) => _underlyingHostUI.WriteProgress(sourceId, record);
public override void WriteProgress(long sourceId, ProgressRecord record)
{
if (record.RecordType == ProgressRecordType.Completed)
{
_ = _currentProgressRecords.TryRemove((sourceId, record.ActivityId), out _);
}
else
{
_ = _currentProgressRecords.TryAdd((sourceId, record.ActivityId), null);
}
_underlyingHostUI.WriteProgress(sourceId, record);
}

public void ResetProgress()
{
// Mark all processed progress records as completed.
foreach ((long sourceId, int activityId) in _currentProgressRecords.Keys)
{
// NOTE: This initializer checks that string is not null nor empty, so it must have
// some text in it.
ProgressRecord record = new(activityId, "0", "0")
{
RecordType = ProgressRecordType.Completed
};
_underlyingHostUI.WriteProgress(sourceId, record);
_currentProgressRecords.Clear();
}
// TODO: Maybe send the OSC sequence to turn off progress indicator.
}

public override void WriteVerboseLine(string message) => _underlyingHostUI.WriteVerboseLine(message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,15 @@ private void DoOneRepl(CancellationToken cancellationToken)
UI.WriteErrorLine($"An error occurred while running the REPL loop:{Environment.NewLine}{e}");
_logger.LogError(e, "An error occurred while running the REPL loop");
}
finally
{
// At the end of each REPL we need to complete all progress records so that the
// progress indicator is cleared.
if (UI is EditorServicesConsolePSHostUserInterface ui)
{
ui.ResetProgress();
}
}
}

private string GetPrompt(CancellationToken cancellationToken)
Expand Down