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 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)
@ -178,8 +180,6 @@ namespace MinecraftClient
}
}
private static void GoBack()
{
if (buffer.Length > 0)
{
if (Console.CursorLeft == 0)
{
@ -188,7 +188,6 @@ namespace MinecraftClient
}
else Console.Write('\b');
}
}
private static void GoLeft()
{
if (buffer.Length > 0)

View file

@ -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;
setcolor(subs[i][0]);
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');