Skip to content

Commit

Permalink
Set terminal output/input encodings to utf-8 (#4690)
Browse files Browse the repository at this point in the history
* Set terminal output/input encodings to utf-8
* Add possibility to config encoding from env variable
  • Loading branch information
ivanduplenskikh authored Mar 26, 2024
1 parent 03113b6 commit 8d741d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Agent.Sdk/Knob/AgentKnobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public class AgentKnobs
new EnvironmentKnobSource("USE_LATEST_GIT_VERSION"),
new BuiltInDefaultKnobSource("false"));

public static readonly Knob AgentTerminalEncoding = new Knob(
nameof(AgentTerminalEncoding),
"If the correct encoding name is specified, the encoding from the environment will be used instead of default UTF-8",
new EnvironmentKnobSource("AGENT_TERMINAL_ENCODING"),
new BuiltInDefaultKnobSource(string.Empty));

public static readonly Knob TfVCUseSecureParameterPassing = new Knob(
nameof(TfVCUseSecureParameterPassing),
"If true, don't pass auth token in TFVC parameters",
Expand Down
21 changes: 21 additions & 0 deletions src/Microsoft.VisualStudio.Services.Agent/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Text;
using Agent.Sdk.Knob;
using Agent.Sdk.Util;

namespace Microsoft.VisualStudio.Services.Agent
Expand Down Expand Up @@ -37,6 +39,25 @@ public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
Console.CancelKeyPress += Console_CancelKeyPress;

var terminalEncoding = Encoding.UTF8;
var endEncodingName = AgentKnobs.AgentTerminalEncoding.GetValue(hostContext).AsString();

try
{
if (!string.IsNullOrEmpty(endEncodingName))
{
terminalEncoding = Encoding.GetEncoding(endEncodingName);
}
}
catch (Exception ex)
{
Trace.Error($@"Encoding ""{endEncodingName}"" not found:");
Trace.Error(ex);
}

Console.OutputEncoding = terminalEncoding;
Console.InputEncoding = terminalEncoding;
}

private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
Expand Down

0 comments on commit 8d741d2

Please sign in to comment.