diff --git a/MinecraftClient/ClassicConsoleBackend.cs b/MinecraftClient/ClassicConsoleBackend.cs index ac4912c9..02712459 100644 --- a/MinecraftClient/ClassicConsoleBackend.cs +++ b/MinecraftClient/ClassicConsoleBackend.cs @@ -63,6 +63,12 @@ namespace MinecraftClient ConsoleInteractive.ConsoleReader.ClearBuffer(); } + public void ClearScreen() + { + Console.Clear(); + ConsoleInteractive.ConsoleSuggestion.ClearSuggestions(); + } + public void SetInputVisible(bool visible) { ConsoleInteractive.ConsoleReader.SetInputVisible(visible); diff --git a/MinecraftClient/Commands/ClearConsole.cs b/MinecraftClient/Commands/ClearConsole.cs new file mode 100644 index 00000000..68ff58cc --- /dev/null +++ b/MinecraftClient/Commands/ClearConsole.cs @@ -0,0 +1,45 @@ +using Brigadier.NET; +using Brigadier.NET.Builder; +using MinecraftClient.CommandHandler; + +namespace MinecraftClient.Commands +{ + public class ClearConsole : Command + { + public override string CmdName => "clear-console"; + public override string CmdUsage => "clear-console"; + public override string CmdDesc => Translations.cmd_clear_console_desc; + + public override void RegisterCommand(CommandDispatcher dispatcher) + { + dispatcher.Register(l => l.Literal("help") + .Then(l => l.Literal(CmdName) + .Executes(r => GetUsage(r.Source)) + ) + ); + + var clearConsole = dispatcher.Register(l => l.Literal(CmdName) + .Executes(r => Execute(r.Source)) + .Then(l => l.Literal("_help") + .Executes(r => GetUsage(r.Source)) + .Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName))) + ); + + dispatcher.Register(l => l.Literal("cc") + .Executes(r => Execute(r.Source)) + .Redirect(clearConsole) + ); + } + + private int GetUsage(CmdResult result) + { + return result.SetAndReturn(GetCmdDescTranslated()); + } + + private int Execute(CmdResult result) + { + ConsoleIO.ClearConsole(); + return result.SetAndReturn(CmdResult.Status.Done); + } + } +} diff --git a/MinecraftClient/Commands/ConsoleChat.cs b/MinecraftClient/Commands/ConsoleChat.cs new file mode 100644 index 00000000..fd52a6e4 --- /dev/null +++ b/MinecraftClient/Commands/ConsoleChat.cs @@ -0,0 +1,49 @@ +using Brigadier.NET; +using Brigadier.NET.Builder; +using MinecraftClient.CommandHandler; + +namespace MinecraftClient.Commands +{ + public class ConsoleChat : Command + { + public override string CmdName => "console-chat"; + public override string CmdUsage => "console-chat [on|off]"; + public override string CmdDesc => Translations.cmd_console_chat_desc; + + public override void RegisterCommand(CommandDispatcher dispatcher) + { + dispatcher.Register(l => l.Literal("help") + .Then(l => l.Literal(CmdName) + .Executes(r => GetUsage(r.Source)) + ) + ); + + dispatcher.Register(l => l.Literal(CmdName) + .Executes(r => SetChatVisibility(r.Source, null)) + .Then(l => l.Literal("on") + .Executes(r => SetChatVisibility(r.Source, true))) + .Then(l => l.Literal("off") + .Executes(r => SetChatVisibility(r.Source, false))) + .Then(l => l.Literal("_help") + .Executes(r => GetUsage(r.Source)) + .Redirect(dispatcher.GetRoot().GetChild("help").GetChild(CmdName))) + ); + } + + private int GetUsage(CmdResult result) + { + return result.SetAndReturn(GetCmdDescTranslated()); + } + + private int SetChatVisibility(CmdResult result, bool? visible) + { + ConsoleIO.ChatVisible = visible ?? !ConsoleIO.ChatVisible; + + return result.SetAndReturn( + CmdResult.Status.Done, + ConsoleIO.ChatVisible + ? Translations.cmd_console_chat_state_on + : Translations.cmd_console_chat_state_off); + } + } +} diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index 8a77aaee..246a27d8 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading; @@ -61,6 +62,11 @@ namespace MinecraftClient /// public static bool EnableTimestamps = false; + /// + /// Determine whether chat lines should be displayed in the console. + /// + public static bool ChatVisible = true; + /// /// Specify a generic log line prefix for WriteLogLine() /// @@ -155,6 +161,17 @@ namespace MinecraftClient } } + /// + /// Write a formatted chat line to the console when chat output is enabled. + /// + public static void WriteChatLineFormatted(string str, bool acceptnewlines = false, bool? displayTimestamp = null) + { + if (!ChatVisible) + return; + + WriteLineFormatted(str, acceptnewlines, displayTimestamp); + } + /// /// Write a prefixed log line. Prefix is set in LogPrefix. /// @@ -178,6 +195,30 @@ namespace MinecraftClient Backend.ClearInputBuffer(); } + /// + /// Clear the visible console output. + /// + public static void ClearConsole() + { + if (BasicIO || Backend is null) + { + try + { + Console.Clear(); + } + catch (IOException) + { + } + catch (PlatformNotSupportedException) + { + } + + return; + } + + Backend.ClearScreen(); + } + #endregion internal static bool AutoCompleteDone = false; diff --git a/MinecraftClient/IConsoleBackend.cs b/MinecraftClient/IConsoleBackend.cs index b4d52ebf..0fd23389 100644 --- a/MinecraftClient/IConsoleBackend.cs +++ b/MinecraftClient/IConsoleBackend.cs @@ -60,6 +60,8 @@ namespace MinecraftClient void ClearInputBuffer(); + void ClearScreen(); + bool DisplayUserInput { get; set; } void SetInputVisible(bool visible); diff --git a/MinecraftClient/Logger/FileLogLogger.cs b/MinecraftClient/Logger/FileLogLogger.cs index a7931202..7cc4972f 100644 --- a/MinecraftClient/Logger/FileLogLogger.cs +++ b/MinecraftClient/Logger/FileLogLogger.cs @@ -72,7 +72,8 @@ namespace MinecraftClient.Logger { if (ShouldDisplay(FilterChannel.Chat, msg)) { - LogAndSave(msg); + ConsoleIO.WriteChatLineFormatted(msg); + Save(msg); } else Debug("[Logger] One Chat message filtered: " + msg); } diff --git a/MinecraftClient/Logger/FilteredLogger.cs b/MinecraftClient/Logger/FilteredLogger.cs index 09a3596c..03ee06a1 100644 --- a/MinecraftClient/Logger/FilteredLogger.cs +++ b/MinecraftClient/Logger/FilteredLogger.cs @@ -81,7 +81,7 @@ namespace MinecraftClient.Logger { if (ShouldDisplay(FilterChannel.Chat, msg)) { - Log(msg); + ConsoleIO.WriteChatLineFormatted(msg); } else Debug("[Logger] One Chat message filtered: " + msg); } diff --git a/MinecraftClient/Resources/ConfigComments/ConfigComments.Designer.cs b/MinecraftClient/Resources/ConfigComments/ConfigComments.Designer.cs index ee4b68ed..fe4b188b 100644 --- a/MinecraftClient/Resources/ConfigComments/ConfigComments.Designer.cs +++ b/MinecraftClient/Resources/ConfigComments/ConfigComments.Designer.cs @@ -1429,6 +1429,15 @@ namespace MinecraftClient { } } + /// + /// Looks up a localized string similar to Whether to display received chat messages in the console. This does not affect chat bots or chat log files.. + /// + internal static string Console_General_Display_Chat { + get { + return ResourceManager.GetString("Console.General.Display_Chat", resourceCulture); + } + } + /// /// Looks up a localized string similar to Maximum number of input history records to keep.. /// diff --git a/MinecraftClient/Resources/ConfigComments/ConfigComments.resx b/MinecraftClient/Resources/ConfigComments/ConfigComments.resx index 3c5804ba..1495d83b 100644 --- a/MinecraftClient/Resources/ConfigComments/ConfigComments.resx +++ b/MinecraftClient/Resources/ConfigComments/ConfigComments.resx @@ -589,6 +589,9 @@ Custom colors are only available when using "vt100_24bit" color mode. You can use "Ctrl+P" to print out the current input and cursor position. + + Whether to display received chat messages in the console. This does not affect chat bots or chat log files. + Maximum number of input history records to keep. diff --git a/MinecraftClient/Resources/Translations/Translations.Designer.cs b/MinecraftClient/Resources/Translations/Translations.Designer.cs index 78fa66ab..621afa37 100644 --- a/MinecraftClient/Resources/Translations/Translations.Designer.cs +++ b/MinecraftClient/Resources/Translations/Translations.Designer.cs @@ -3024,6 +3024,42 @@ namespace MinecraftClient { } } + /// + /// Looks up a localized string similar to clear the console output.. + /// + internal static string cmd_clear_console_desc { + get { + return ResourceManager.GetString("cmd.clear_console.desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to toggle chat visibility in the console.. + /// + internal static string cmd_console_chat_desc { + get { + return ResourceManager.GetString("cmd.console_chat.desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Console chat output is now hidden. + /// + internal static string cmd_console_chat_state_off { + get { + return ResourceManager.GetString("cmd.console_chat.state_off", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Console chat output is now visible. + /// + internal static string cmd_console_chat_state_on { + get { + return ResourceManager.GetString("cmd.console_chat.state_on", resourceCulture); + } + } + /// /// Looks up a localized string similar to toggle debug messages.. /// diff --git a/MinecraftClient/Resources/Translations/Translations.resx b/MinecraftClient/Resources/Translations/Translations.resx index d4442833..e795cb63 100644 --- a/MinecraftClient/Resources/Translations/Translations.resx +++ b/MinecraftClient/Resources/Translations/Translations.resx @@ -1090,6 +1090,18 @@ Change EnableEmoji=false in the settings if the display is confusing. Unknown account '{0}'. + + toggle chat visibility in the console. + + + Console chat output is now hidden + + + Console chat output is now visible + + + clear the console output. + toggle debug messages. diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index caf92226..889abe5a 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -1122,6 +1122,8 @@ namespace MinecraftClient public void OnSettingUpdate() { + ConsoleIO.ChatVisible = General.Display_Chat; + var backend = ConsoleIO.Backend; if (backend == null) return; @@ -1225,6 +1227,9 @@ namespace MinecraftClient [TomlInlineComment("$Console.General.Display_Input$")] public bool Display_Input = true; + [TomlInlineComment("$Console.General.Display_Chat$")] + public bool Display_Chat = true; + [TomlInlineComment("$Console.General.History_Input_Records$")] public int History_Input_Records = 32; diff --git a/MinecraftClient/Tui/MainTuiView.cs b/MinecraftClient/Tui/MainTuiView.cs index 7c34d17c..d5400d3a 100644 --- a/MinecraftClient/Tui/MainTuiView.cs +++ b/MinecraftClient/Tui/MainTuiView.cs @@ -268,6 +268,12 @@ namespace MinecraftClient.Tui ScheduleScrollToEnd(); } + public void ClearLog() + { + _logLines.Clear(); + _logControls.Clear(); + } + private void TrimLog() { while (_logLines.Count > MaxLogLines) diff --git a/MinecraftClient/Tui/TuiConsoleBackend.cs b/MinecraftClient/Tui/TuiConsoleBackend.cs index 58bd444d..523b27f1 100644 --- a/MinecraftClient/Tui/TuiConsoleBackend.cs +++ b/MinecraftClient/Tui/TuiConsoleBackend.cs @@ -240,6 +240,15 @@ namespace MinecraftClient.Tui Dispatcher.UIThread.Post(() => _view?.ClearInput()); } + public void ClearScreen() + { + if (_view == null) return; + if (Dispatcher.UIThread.CheckAccess()) + _view.ClearLog(); + else + Dispatcher.UIThread.Post(() => _view?.ClearLog()); + } + public void SetInputVisible(bool visible) { }