2013-07-18 09:27:19 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2014-01-11 12:48:59 +01:00
|
|
|
|
using System.Threading;
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allows simultaneous console input and output without breaking user input
|
|
|
|
|
|
/// (Without having this annoying behaviour : User inp[Some Console output]ut)
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// Provide some fancy features such as formatted output, text pasting and tab-completion.
|
|
|
|
|
|
/// By ORelio - (c) 2012-2018 - Available under the CDDL-1.0 license
|
2013-07-18 09:27:19 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class ConsoleIO
|
|
|
|
|
|
{
|
2013-08-06 12:25:09 +02:00
|
|
|
|
private static IAutoComplete autocomplete_engine;
|
2015-05-10 18:57:33 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-05-13 10:59:46 +02:00
|
|
|
|
/// Reset the IO mechanism and clear all buffers
|
2015-05-10 18:57:33 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void Reset()
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
ClearLineAndBuffer();
|
2015-05-10 18:57:33 +02:00
|
|
|
|
}
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set an auto-completion engine for TAB autocompletion.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="engine">Engine implementing the IAutoComplete interface</param>
|
|
|
|
|
|
public static void SetAutoCompleteEngine(IAutoComplete engine)
|
|
|
|
|
|
{
|
|
|
|
|
|
autocomplete_engine = engine;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determines whether to use interactive IO or basic IO.
|
|
|
|
|
|
/// Set to true to disable interactive command prompt and use the default Console.Read|Write() methods.
|
|
|
|
|
|
/// Color codes are printed as is when BasicIO is enabled.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool BasicIO = false;
|
|
|
|
|
|
|
2020-05-14 13:36:56 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determines whether not to print color codes in BasicIO mode.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool BasicIO_NoColor = false;
|
|
|
|
|
|
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determine whether WriteLineFormatted() should prepend lines with timestamps by default.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool EnableTimestamps = false;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Specify a generic log line prefix for WriteLogLine()
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string LogPrefix = "§8[Log] ";
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read a password from the standard input
|
|
|
|
|
|
/// </summary>
|
2022-08-15 23:55:44 +08:00
|
|
|
|
public static string? ReadPassword()
|
2013-08-23 10:48:26 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (BasicIO)
|
|
|
|
|
|
return Console.ReadLine();
|
|
|
|
|
|
else
|
2013-08-23 10:48:26 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
ConsoleInteractive.ConsoleReader.SetInputVisible(false);
|
|
|
|
|
|
var input = ConsoleInteractive.ConsoleReader.RequestImmediateInput();
|
|
|
|
|
|
ConsoleInteractive.ConsoleReader.SetInputVisible(true);
|
|
|
|
|
|
return input;
|
2013-08-23 10:48:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read a line from the standard input
|
|
|
|
|
|
/// </summary>
|
2013-07-18 09:27:19 +02:00
|
|
|
|
public static string ReadLine()
|
|
|
|
|
|
{
|
2018-05-28 22:01:51 +02:00
|
|
|
|
if (BasicIO)
|
|
|
|
|
|
return Console.ReadLine();
|
2022-07-03 22:34:07 +08:00
|
|
|
|
else
|
|
|
|
|
|
return ConsoleInteractive.ConsoleReader.RequestImmediateInput();
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
2017-05-28 15:09:19 +02:00
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// <summary>
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// Debug routine: print all keys pressed in the console
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// </summary>
|
2017-05-28 15:09:19 +02:00
|
|
|
|
public static void DebugReadInput()
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleKeyInfo k = new ConsoleKeyInfo();
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
k = Console.ReadKey(true);
|
|
|
|
|
|
Console.WriteLine("Key: {0}\tChar: {1}\tModifiers: {2}", k.Key, k.KeyChar, k.Modifiers);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write a string to the standard output with a trailing newline
|
|
|
|
|
|
/// </summary>
|
2013-07-18 09:27:19 +02:00
|
|
|
|
public static void WriteLine(string line)
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (BasicIO)
|
|
|
|
|
|
Console.WriteLine(line);
|
|
|
|
|
|
else
|
|
|
|
|
|
ConsoleInteractive.ConsoleWriter.WriteLine(line);
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// Write a Minecraft-Like formatted string to the standard output, using §c color codes
|
|
|
|
|
|
/// See minecraft.gamepedia.com/Classic_server_protocol#Color_Codes for more info
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="str">String to write</param>
|
|
|
|
|
|
/// <param name="acceptnewlines">If false, space are printed instead of newlines</param>
|
2019-10-03 09:46:08 +02:00
|
|
|
|
/// <param name="displayTimestamp">
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// If false, no timestamp is prepended.
|
|
|
|
|
|
/// If true, "hh-mm-ss" timestamp will be prepended.
|
|
|
|
|
|
/// If unspecified, value is retrieved from EnableTimestamps.
|
|
|
|
|
|
/// </param>
|
2022-07-03 22:34:07 +08:00
|
|
|
|
public static void WriteLineFormatted(string str, bool acceptnewlines = false, bool? displayTimestamp = null)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
if (!String.IsNullOrEmpty(str))
|
|
|
|
|
|
{
|
2022-08-16 00:17:30 +08:00
|
|
|
|
if (displayTimestamp == null)
|
2018-05-28 22:01:51 +02:00
|
|
|
|
{
|
|
|
|
|
|
displayTimestamp = EnableTimestamps;
|
|
|
|
|
|
}
|
2022-08-16 00:17:30 +08:00
|
|
|
|
if (displayTimestamp.Value)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute, second = DateTime.Now.Second;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
output.Append(String.Format("{0}:{1}:{2} ", hour.ToString("00"), minute.ToString("00"), second.ToString("00")));
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
2022-08-16 00:17:30 +08:00
|
|
|
|
if (!acceptnewlines)
|
|
|
|
|
|
{
|
|
|
|
|
|
str = str.Replace('\n', ' ');
|
|
|
|
|
|
}
|
2018-05-28 22:01:51 +02:00
|
|
|
|
if (BasicIO)
|
|
|
|
|
|
{
|
2020-05-14 13:36:56 -04:00
|
|
|
|
if (BasicIO_NoColor)
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
output.Append(ChatBot.GetVerbatim(str));
|
2020-05-14 13:36:56 -04:00
|
|
|
|
}
|
2022-08-16 00:17:30 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
output.Append(str);
|
|
|
|
|
|
}
|
2022-07-03 22:34:07 +08:00
|
|
|
|
Console.WriteLine(output.ToString());
|
2018-05-28 22:01:51 +02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-08-16 00:17:30 +08:00
|
|
|
|
output.Append(str);
|
2022-07-03 22:34:07 +08:00
|
|
|
|
ConsoleInteractive.ConsoleWriter.WriteLineFormatted(output.ToString());
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-07-18 09:27:19 +02:00
|
|
|
|
|
2015-06-21 16:40:13 +02:00
|
|
|
|
/// <summary>
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// Write a prefixed log line. Prefix is set in LogPrefix.
|
2015-06-21 16:40:13 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="text">Text of the log line</param>
|
2020-04-01 21:15:35 +02:00
|
|
|
|
/// <param name="acceptnewlines">Allow line breaks</param>
|
|
|
|
|
|
public static void WriteLogLine(string text, bool acceptnewlines = true)
|
2015-06-21 16:40:13 +02:00
|
|
|
|
{
|
2020-04-01 21:15:35 +02:00
|
|
|
|
if (!acceptnewlines)
|
|
|
|
|
|
text = text.Replace('\n', ' ');
|
2022-09-01 23:10:09 +08:00
|
|
|
|
WriteLineFormatted(LogPrefix + text, acceptnewlines);
|
2015-06-21 16:40:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-11 12:48:59 +01:00
|
|
|
|
#region Subfunctions
|
2018-05-28 22:01:51 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Clear all text inside the input prompt
|
|
|
|
|
|
/// </summary>
|
2013-07-18 09:27:19 +02:00
|
|
|
|
private static void ClearLineAndBuffer()
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (BasicIO) return;
|
|
|
|
|
|
ConsoleInteractive.ConsoleReader.ClearBuffer();
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
2018-05-28 22:01:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
2013-07-18 09:27:19 +02:00
|
|
|
|
#endregion
|
2014-01-11 12:48:59 +01:00
|
|
|
|
|
2022-07-03 22:34:07 +08:00
|
|
|
|
public static void AutocompleteHandler(object? sender, ConsoleKey e)
|
2014-01-11 12:48:59 +01:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (e != ConsoleKey.Tab) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (autocomplete_engine == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
var buffer = ConsoleInteractive.ConsoleReader.GetBufferContent();
|
|
|
|
|
|
autocomplete_engine.AutoComplete(buffer.Text[..buffer.CursorPosition]);
|
2014-01-11 12:48:59 +01:00
|
|
|
|
}
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|
2013-08-06 12:25:09 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Interface for TAB autocompletion
|
|
|
|
|
|
/// Allows to use any object which has an AutoComplete() method using the IAutocomplete interface
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public interface IAutoComplete
|
|
|
|
|
|
{
|
2018-05-28 22:01:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provide a list of auto-complete strings based on the provided input behing the cursor
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="BehindCursor">Text behind the cursor, e.g. "my input comm"</param>
|
|
|
|
|
|
/// <returns>List of auto-complete words, e.g. ["command", "comment"]</returns>
|
2016-05-14 11:51:02 +02:00
|
|
|
|
IEnumerable<string> AutoComplete(string BehindCursor);
|
2013-08-06 12:25:09 +02:00
|
|
|
|
}
|
2013-07-18 09:27:19 +02:00
|
|
|
|
}
|