Improved console input/output

This commit is contained in:
ORelio 2013-07-27 18:44:54 +02:00
parent b40ad6ee16
commit ea2774068b
2 changed files with 26 additions and 32 deletions

View file

@ -16,9 +16,9 @@ namespace MinecraftClient
private static LinkedList<string> previous = new LinkedList<string>(); private static LinkedList<string> previous = new LinkedList<string>();
private static string buffer = ""; private static string buffer = "";
private static string buffer2 = ""; private static string buffer2 = "";
private static bool consolelock = false;
private static bool reading = 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 #region Read User Input
public static string ReadLine() public static string ReadLine()
@ -32,8 +32,8 @@ namespace MinecraftClient
while (k.Key != ConsoleKey.Enter) while (k.Key != ConsoleKey.Enter)
{ {
k = Console.ReadKey(true); k = Console.ReadKey(true);
while (writing) { } while (writing_lock) { }
consolelock = true; reading_lock = true;
switch (k.Key) switch (k.Key)
{ {
case ConsoleKey.Escape: case ConsoleKey.Escape:
@ -86,13 +86,15 @@ namespace MinecraftClient
Console.Write(buffer); Console.Write(buffer);
} }
break; break;
case ConsoleKey.Tab:
break;
default: default:
AddChar(k.KeyChar); AddChar(k.KeyChar);
break; break;
} }
consolelock = false; reading_lock = false;
} }
while (writing) { } while (writing_lock) { }
reading = false; reading = false;
previous.AddLast(buffer + buffer2); previous.AddLast(buffer + buffer2);
return buffer + buffer2; return buffer + buffer2;
@ -102,8 +104,8 @@ namespace MinecraftClient
#region Console Output #region Console Output
public static void Write(string text) public static void Write(string text)
{ {
while (consolelock) { } while (reading_lock) { }
writing = true; writing_lock = true;
if (reading) if (reading)
{ {
ConsoleColor fore = Console.ForegroundColor; ConsoleColor fore = Console.ForegroundColor;
@ -135,7 +137,7 @@ namespace MinecraftClient
Console.BackgroundColor = back; Console.BackgroundColor = back;
} }
else Console.Write(text); else Console.Write(text);
writing = false; writing_lock = false;
} }
public static void WriteLine(string line) public static void WriteLine(string line)
@ -178,8 +180,6 @@ namespace MinecraftClient
} }
} }
private static void GoBack() private static void GoBack()
{
if (buffer.Length > 0)
{ {
if (Console.CursorLeft == 0) if (Console.CursorLeft == 0)
{ {
@ -188,7 +188,6 @@ namespace MinecraftClient
} }
else Console.Write('\b'); else Console.Write('\b');
} }
}
private static void GoLeft() private static void GoLeft()
{ {
if (buffer.Length > 0) if (buffer.Length > 0)

View file

@ -460,25 +460,20 @@ namespace MinecraftClient
} }
private void printstring(string str, bool acceptnewlines) private void printstring(string str, bool acceptnewlines)
{ {
if (str != "") if (!String.IsNullOrEmpty(str))
{ {
char prev = ' '; if (!acceptnewlines) { str = str.Replace('\n', ' '); }
foreach (char c in str) 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; setcolor(subs[i][0]);
continue; if (subs[i].Length > 1)
{
ConsoleIO.Write(subs[i].Substring(1, subs[i].Length - 1));
} }
else if (prev == '§')
{
setcolor(c);
prev = c;
}
else
{
if (c == '\n' && !acceptnewlines) { continue; }
else ConsoleIO.Write(c);
} }
} }
ConsoleIO.Write('\n'); ConsoleIO.Write('\n');