Skip to content

Commit

Permalink
Fixed issue with trying to write a verbose message outside of the mai…
Browse files Browse the repository at this point in the history
…n cmdlet thread.
  • Loading branch information
erwinvanhunen committed Nov 20, 2024
1 parent f04231d commit 3780f38
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Commands/Base/ConnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ private PnPConnection ConnectDeviceLogin()
}
else
{
WriteVerbose("Using Managed AppId from secure store");
messageWriter.WriteVerbose("Using Managed AppId from secure store");
}
}

Expand Down
44 changes: 41 additions & 3 deletions src/Commands/Utilities/CmdletMessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Security.Cryptography.Pkcs;
using System.Threading;

namespace PnP.PowerShell.Commands.Utilities
Expand Down Expand Up @@ -38,7 +39,25 @@ public void Start()
}
else
{
Cmdlet.Host.UI.WriteLine(message.Text);
switch (message.Type)
{
case MessageType.Message:
{
Cmdlet.Host.UI.WriteLine(message.Text);
break;
}
case MessageType.Warning:
{
Cmdlet.Host.UI.WriteWarningLine(message.Text);
break;
}
case MessageType.Verbose:
{
Cmdlet.Host.UI.WriteVerboseLine(message.Text);
break;
}
}

}
break;
}
Expand All @@ -63,6 +82,14 @@ public void WriteMessage(string message, bool formatted = true)
}
}

public void WriteVerbose(string message)
{
lock (LockToken)
{
Queue.Enqueue(new Message() { Formatted = false, Text = message, Type = MessageType.Verbose });
}
}

internal class Message
{
public string Text { get; set; }
Expand All @@ -73,7 +100,8 @@ internal class Message
internal enum MessageType
{
Message,
Warning
Warning,
Verbose
}

private static List<string> WordWrap(string text, int maxLineLength)
Expand All @@ -99,7 +127,7 @@ internal static void WriteFormattedWarning(PSCmdlet cmdlet, string message)
{
WriteFormattedMessage(cmdlet, new Message { Text = message, Type = MessageType.Warning, Formatted = true });
}

internal static void WriteFormattedMessage(PSCmdlet cmdlet, Message message)
{
if (cmdlet.Host.Name == "ConsoleHost" && cmdlet.Host.UI.RawUI.MaxWindowSize.Width > 8)
Expand Down Expand Up @@ -142,6 +170,11 @@ internal static void WriteFormattedMessage(PSCmdlet cmdlet, Message message)
cmdlet.WriteWarning($"{notificationColor}\n{outMessage}{resetColor}\n");
break;
}
case MessageType.Verbose:
{
cmdlet.WriteVerbose(outMessage);
break;
}
}
}
else
Expand All @@ -158,6 +191,11 @@ internal static void WriteFormattedMessage(PSCmdlet cmdlet, Message message)
cmdlet.WriteWarning(message.Text);
break;
}
case MessageType.Verbose:
{
cmdlet.WriteVerbose(message.Text);
break;
}
}
}
}
Expand Down

0 comments on commit 3780f38

Please sign in to comment.