From 8d741d220962e458f3da92a0a7f2d5fe35c63825 Mon Sep 17 00:00:00 2001 From: Ivan Duplenskikh <115665590+ivanduplenskikh@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:36:23 +0100 Subject: [PATCH] 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 --- src/Agent.Sdk/Knob/AgentKnobs.cs | 6 ++++++ .../Terminal.cs | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Agent.Sdk/Knob/AgentKnobs.cs b/src/Agent.Sdk/Knob/AgentKnobs.cs index bccf5ca689..f67295f98d 100644 --- a/src/Agent.Sdk/Knob/AgentKnobs.cs +++ b/src/Agent.Sdk/Knob/AgentKnobs.cs @@ -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", diff --git a/src/Microsoft.VisualStudio.Services.Agent/Terminal.cs b/src/Microsoft.VisualStudio.Services.Agent/Terminal.cs index 9c8e5cc79c..a31d33454e 100644 --- a/src/Microsoft.VisualStudio.Services.Agent/Terminal.cs +++ b/src/Microsoft.VisualStudio.Services.Agent/Terminal.cs @@ -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 @@ -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)