refactor: simplify color handling in ClassicConsoleBackend

- Removed hex color handling logic from ClassicConsoleBackend, streamlining the WriteLineFormatted method.
- Updated ColorHelper to ensure consistent formatting for color codes.
- Ensured console-specific settings are reapplied after backend initialization in Program.cs.
This commit is contained in:
BruceChen 2026-04-08 01:33:38 +08:00
parent 64c42bb4b5
commit ff00d01fd7
3 changed files with 9 additions and 23 deletions

View file

@ -1,6 +1,4 @@
using System; using System;
using System.Text.RegularExpressions;
using static MinecraftClient.Settings.ConsoleConfigHealper.ConsoleConfig;
namespace MinecraftClient namespace MinecraftClient
{ {
@ -28,29 +26,11 @@ namespace MinecraftClient
ConsoleInteractive.ConsoleWriter.WriteLine(text); ConsoleInteractive.ConsoleWriter.WriteLine(text);
} }
private static readonly Regex HexColorRegex = new(@"§#([0-9a-fA-F]{6})", RegexOptions.Compiled);
public void WriteLineFormatted(string text) public void WriteLineFormatted(string text)
{ {
bool hasHex = text.Contains("§#");
if (hasHex)
text = ResolveHexColors(text);
ConsoleInteractive.ConsoleWriter.WriteLineFormatted(text); ConsoleInteractive.ConsoleWriter.WriteLineFormatted(text);
} }
private static string ResolveHexColors(string text)
{
var mode = Settings.Config.Console.General.ConsoleColorMode;
return HexColorRegex.Replace(text, match =>
{
ReadOnlySpan<char> hex = match.Groups[1].ValueSpan;
byte r = Convert.ToByte(hex[..2].ToString(), 16);
byte g = Convert.ToByte(hex[2..4].ToString(), 16);
byte b = Convert.ToByte(hex[4..6].ToString(), 16);
return ColorHelper.GetColorEscapeCode(r, g, b, foreground: true, mode);
});
}
public void BeginReadThread() public void BeginReadThread()
{ {
ConsoleInteractive.ConsoleReader.MessageReceived += ForwardMessage; ConsoleInteractive.ConsoleReader.MessageReceived += ForwardMessage;

View file

@ -1,4 +1,4 @@
using System; using System;
using static MinecraftClient.Settings.ConsoleConfigHealper.ConsoleConfig; using static MinecraftClient.Settings.ConsoleConfigHealper.ConsoleConfig;
namespace MinecraftClient namespace MinecraftClient
@ -100,9 +100,9 @@ namespace MinecraftClient
} }
} }
if (foreground) if (foreground)
return $"§{best_idx:X}"; return $"§{best_idx:x}";
else else
return $"§§{best_idx:X}"; return $"§§{best_idx:x}";
} }
case ConsoleColorModeType.vt100_4bit: case ConsoleColorModeType.vt100_4bit:

View file

@ -202,6 +202,11 @@ namespace MinecraftClient
{ {
ConsoleIO.Backend = new ClassicConsoleBackend(); ConsoleIO.Backend = new ClassicConsoleBackend();
ConsoleIO.Backend.Init(); ConsoleIO.Backend.Init();
// Config deserialization triggers OnSettingUpdate before the backend
// exists, so console-specific settings (UseVT100ColorCode, colors, etc.)
// are never applied. Re-apply them now that the backend is ready.
Config.Console.OnSettingUpdate();
} }
if (!ProcessStartupState(startupState)) if (!ProcessStartupState(startupState))
@ -240,6 +245,7 @@ namespace MinecraftClient
ConsoleIO.Backend = new ClassicConsoleBackend(); ConsoleIO.Backend = new ClassicConsoleBackend();
ConsoleIO.Backend.Init(); ConsoleIO.Backend.Init();
Config.Console.OnSettingUpdate();
ConsoleIO.WriteLineFormatted("§c" + Translations.mcc_tui_startup_failed); ConsoleIO.WriteLineFormatted("§c" + Translations.mcc_tui_startup_failed);
ConsoleIO.WriteLine(exception.ToString()); ConsoleIO.WriteLine(exception.ToString());