Skip to content

Commit 8d741d2

Browse files
Set terminal output/input encodings to utf-8 (#4690)
* Set terminal output/input encodings to utf-8 * Add possibility to config encoding from env variable
1 parent 03113b6 commit 8d741d2

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Agent.Sdk/Knob/AgentKnobs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ public class AgentKnobs
149149
new EnvironmentKnobSource("USE_LATEST_GIT_VERSION"),
150150
new BuiltInDefaultKnobSource("false"));
151151

152+
public static readonly Knob AgentTerminalEncoding = new Knob(
153+
nameof(AgentTerminalEncoding),
154+
"If the correct encoding name is specified, the encoding from the environment will be used instead of default UTF-8",
155+
new EnvironmentKnobSource("AGENT_TERMINAL_ENCODING"),
156+
new BuiltInDefaultKnobSource(string.Empty));
157+
152158
public static readonly Knob TfVCUseSecureParameterPassing = new Knob(
153159
nameof(TfVCUseSecureParameterPassing),
154160
"If true, don't pass auth token in TFVC parameters",

src/Microsoft.VisualStudio.Services.Agent/Terminal.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Text;
7+
using Agent.Sdk.Knob;
68
using Agent.Sdk.Util;
79

810
namespace Microsoft.VisualStudio.Services.Agent
@@ -37,6 +39,25 @@ public override void Initialize(IHostContext hostContext)
3739
{
3840
base.Initialize(hostContext);
3941
Console.CancelKeyPress += Console_CancelKeyPress;
42+
43+
var terminalEncoding = Encoding.UTF8;
44+
var endEncodingName = AgentKnobs.AgentTerminalEncoding.GetValue(hostContext).AsString();
45+
46+
try
47+
{
48+
if (!string.IsNullOrEmpty(endEncodingName))
49+
{
50+
terminalEncoding = Encoding.GetEncoding(endEncodingName);
51+
}
52+
}
53+
catch (Exception ex)
54+
{
55+
Trace.Error($@"Encoding ""{endEncodingName}"" not found:");
56+
Trace.Error(ex);
57+
}
58+
59+
Console.OutputEncoding = terminalEncoding;
60+
Console.InputEncoding = terminalEncoding;
4061
}
4162

4263
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)

0 commit comments

Comments
 (0)