Skip to content

Commit 2846bef

Browse files
committed
Fix bad output when console repl is disabled
1 parent b0edca9 commit 2846bef

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Management.Automation.Host;
6+
7+
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
8+
{
9+
internal class NullPSHostRawUI : PSHostRawUserInterface
10+
{
11+
private readonly BufferCell[,] _buffer;
12+
13+
public NullPSHostRawUI()
14+
{
15+
_buffer = new BufferCell[0, 0];
16+
}
17+
18+
public override ConsoleColor BackgroundColor { get; set; }
19+
public override Size BufferSize { get; set; }
20+
public override Coordinates CursorPosition { get; set; }
21+
public override int CursorSize { get; set; }
22+
public override ConsoleColor ForegroundColor { get; set; }
23+
24+
public override bool KeyAvailable => false;
25+
26+
public override Size MaxPhysicalWindowSize => MaxWindowSize;
27+
28+
public override Size MaxWindowSize => new Size { Width = _buffer.GetLength(0), Height = _buffer.GetLength(1) };
29+
30+
public override Coordinates WindowPosition { get; set; }
31+
public override Size WindowSize { get; set; }
32+
public override string WindowTitle { get; set; }
33+
34+
public override void FlushInputBuffer()
35+
{
36+
// Do nothing
37+
}
38+
39+
public override BufferCell[,] GetBufferContents(Rectangle rectangle) => _buffer;
40+
41+
public override KeyInfo ReadKey(ReadKeyOptions options) => default;
42+
43+
public override void ScrollBufferContents(Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill)
44+
{
45+
// Do nothing
46+
}
47+
48+
public override void SetBufferContents(Coordinates origin, BufferCell[,] contents)
49+
{
50+
// Do nothing
51+
}
52+
53+
public override void SetBufferContents(Rectangle rectangle, BufferCell fill)
54+
{
55+
// Do nothing
56+
}
57+
}
58+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Management.Automation;
8+
using System.Management.Automation.Host;
9+
using System.Security;
10+
11+
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Host
12+
{
13+
internal class NullPSHostUI : PSHostUserInterface
14+
{
15+
public NullPSHostUI()
16+
{
17+
RawUI = new NullPSHostRawUI();
18+
}
19+
20+
public override PSHostRawUserInterface RawUI { get; }
21+
22+
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
23+
{
24+
return new Dictionary<string, PSObject>();
25+
}
26+
27+
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
28+
{
29+
return 0;
30+
}
31+
32+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
33+
{
34+
return new PSCredential(userName: string.Empty, password: new SecureString());
35+
}
36+
37+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
38+
=> PromptForCredential(caption, message, userName, targetName, PSCredentialTypes.Default, PSCredentialUIOptions.Default);
39+
40+
public override string ReadLine()
41+
{
42+
return string.Empty;
43+
}
44+
45+
public override SecureString ReadLineAsSecureString()
46+
{
47+
return new SecureString();
48+
}
49+
50+
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
51+
{
52+
// Do nothing
53+
}
54+
55+
public override void Write(string value)
56+
{
57+
// Do nothing
58+
}
59+
60+
public override void WriteDebugLine(string message)
61+
{
62+
// Do nothing
63+
}
64+
65+
public override void WriteErrorLine(string value)
66+
{
67+
// Do nothing
68+
}
69+
70+
public override void WriteLine(string value)
71+
{
72+
// Do nothing
73+
}
74+
75+
public override void WriteProgress(long sourceId, ProgressRecord record)
76+
{
77+
// Do nothing
78+
}
79+
80+
public override void WriteVerboseLine(string message)
81+
{
82+
// Do nothing
83+
}
84+
85+
public override void WriteWarningLine(string message)
86+
{
87+
// Do nothing
88+
}
89+
}
90+
}

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public PsesInternalHost(
103103
Version = hostInfo.Version;
104104

105105
DebugContext = new PowerShellDebugContext(loggerFactory, languageServer, this);
106-
UI = new EditorServicesConsolePSHostUserInterface(loggerFactory, _readLineProvider, hostInfo.PSHost.UI);
106+
UI = hostInfo.ConsoleReplEnabled
107+
? new EditorServicesConsolePSHostUserInterface(loggerFactory, _readLineProvider, hostInfo.PSHost.UI)
108+
: new NullPSHostUI();
107109
}
108110

109111
public override CultureInfo CurrentCulture => _hostInfo.PSHost.CurrentCulture;
@@ -590,6 +592,11 @@ private void RunExecutionLoop()
590592

591593
private void DoOneRepl(CancellationToken cancellationToken)
592594
{
595+
if (!_hostInfo.ConsoleReplEnabled)
596+
{
597+
return;
598+
}
599+
593600
// When a task must run in the foreground, we cancel out of the idle loop and return to the top level.
594601
// At that point, we would normally run a REPL, but we need to immediately execute the task.
595602
// So we set _skipNextPrompt to do that.

0 commit comments

Comments
 (0)