From ea2774068b2792471da14ab947cfbd088619a404 Mon Sep 17 00:00:00 2001 From: ORelio Date: Sat, 27 Jul 2013 18:44:54 +0200 Subject: [PATCH] Improved console input/output --- MinecraftClient/ConsoleIO.cs | 31 +++++++++++++++---------------- MinecraftClient/MinecraftCom.cs | 27 +++++++++++---------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/MinecraftClient/ConsoleIO.cs b/MinecraftClient/ConsoleIO.cs index d2a181f5..8ddc0261 100644 --- a/MinecraftClient/ConsoleIO.cs +++ b/MinecraftClient/ConsoleIO.cs @@ -16,9 +16,9 @@ namespace MinecraftClient private static LinkedList previous = new LinkedList(); private static string buffer = ""; private static string buffer2 = ""; - private static bool consolelock = false; private static bool reading = false; - private static bool writing = false; + private static bool reading_lock = false; + private static bool writing_lock = false; #region Read User Input public static string ReadLine() @@ -32,8 +32,8 @@ namespace MinecraftClient while (k.Key != ConsoleKey.Enter) { k = Console.ReadKey(true); - while (writing) { } - consolelock = true; + while (writing_lock) { } + reading_lock = true; switch (k.Key) { case ConsoleKey.Escape: @@ -86,13 +86,15 @@ namespace MinecraftClient Console.Write(buffer); } break; + case ConsoleKey.Tab: + break; default: AddChar(k.KeyChar); break; } - consolelock = false; + reading_lock = false; } - while (writing) { } + while (writing_lock) { } reading = false; previous.AddLast(buffer + buffer2); return buffer + buffer2; @@ -102,8 +104,8 @@ namespace MinecraftClient #region Console Output public static void Write(string text) { - while (consolelock) { } - writing = true; + while (reading_lock) { } + writing_lock = true; if (reading) { ConsoleColor fore = Console.ForegroundColor; @@ -135,7 +137,7 @@ namespace MinecraftClient Console.BackgroundColor = back; } else Console.Write(text); - writing = false; + writing_lock = false; } public static void WriteLine(string line) @@ -179,15 +181,12 @@ namespace MinecraftClient } private static void GoBack() { - if (buffer.Length > 0) + if (Console.CursorLeft == 0) { - if (Console.CursorLeft == 0) - { - Console.CursorLeft = Console.BufferWidth - 1; - Console.CursorTop--; - } - else Console.Write('\b'); + Console.CursorLeft = Console.BufferWidth - 1; + Console.CursorTop--; } + else Console.Write('\b'); } private static void GoLeft() { diff --git a/MinecraftClient/MinecraftCom.cs b/MinecraftClient/MinecraftCom.cs index 667ace67..32d4a07c 100644 --- a/MinecraftClient/MinecraftCom.cs +++ b/MinecraftClient/MinecraftCom.cs @@ -460,25 +460,20 @@ namespace MinecraftClient } private void printstring(string str, bool acceptnewlines) { - if (str != "") + if (!String.IsNullOrEmpty(str)) { - char prev = ' '; - foreach (char c in str) + if (!acceptnewlines) { str = str.Replace('\n', ' '); } + string[] subs = str.Split(new char[] { '§' }); + if (subs[0].Length > 0) { ConsoleIO.Write(subs[0]); } + for (int i = 1; i < subs.Length; i++) { - if (c == '§') + if (subs[i].Length > 0) { - prev = c; - continue; - } - else if (prev == '§') - { - setcolor(c); - prev = c; - } - else - { - if (c == '\n' && !acceptnewlines) { continue; } - else ConsoleIO.Write(c); + setcolor(subs[i][0]); + if (subs[i].Length > 1) + { + ConsoleIO.Write(subs[i].Substring(1, subs[i].Length - 1)); + } } } ConsoleIO.Write('\n');