Skip to content

Commit ea27740

Browse files
committed
Improved console input/output
1 parent b40ad6e commit ea27740

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

MinecraftClient/ConsoleIO.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public static class ConsoleIO
1616
private static LinkedList<string> previous = new LinkedList<string>();
1717
private static string buffer = "";
1818
private static string buffer2 = "";
19-
private static bool consolelock = false;
2019
private static bool reading = false;
21-
private static bool writing = false;
20+
private static bool reading_lock = false;
21+
private static bool writing_lock = false;
2222

2323
#region Read User Input
2424
public static string ReadLine()
@@ -32,8 +32,8 @@ public static string ReadLine()
3232
while (k.Key != ConsoleKey.Enter)
3333
{
3434
k = Console.ReadKey(true);
35-
while (writing) { }
36-
consolelock = true;
35+
while (writing_lock) { }
36+
reading_lock = true;
3737
switch (k.Key)
3838
{
3939
case ConsoleKey.Escape:
@@ -86,13 +86,15 @@ public static string ReadLine()
8686
Console.Write(buffer);
8787
}
8888
break;
89+
case ConsoleKey.Tab:
90+
break;
8991
default:
9092
AddChar(k.KeyChar);
9193
break;
9294
}
93-
consolelock = false;
95+
reading_lock = false;
9496
}
95-
while (writing) { }
97+
while (writing_lock) { }
9698
reading = false;
9799
previous.AddLast(buffer + buffer2);
98100
return buffer + buffer2;
@@ -102,8 +104,8 @@ public static string ReadLine()
102104
#region Console Output
103105
public static void Write(string text)
104106
{
105-
while (consolelock) { }
106-
writing = true;
107+
while (reading_lock) { }
108+
writing_lock = true;
107109
if (reading)
108110
{
109111
ConsoleColor fore = Console.ForegroundColor;
@@ -135,7 +137,7 @@ public static void Write(string text)
135137
Console.BackgroundColor = back;
136138
}
137139
else Console.Write(text);
138-
writing = false;
140+
writing_lock = false;
139141
}
140142

141143
public static void WriteLine(string line)
@@ -179,15 +181,12 @@ private static void RemoveOneChar()
179181
}
180182
private static void GoBack()
181183
{
182-
if (buffer.Length > 0)
184+
if (Console.CursorLeft == 0)
183185
{
184-
if (Console.CursorLeft == 0)
185-
{
186-
Console.CursorLeft = Console.BufferWidth - 1;
187-
Console.CursorTop--;
188-
}
189-
else Console.Write('\b');
186+
Console.CursorLeft = Console.BufferWidth - 1;
187+
Console.CursorTop--;
190188
}
189+
else Console.Write('\b');
191190
}
192191
private static void GoLeft()
193192
{

MinecraftClient/MinecraftCom.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -460,25 +460,20 @@ private void setcolor(char c)
460460
}
461461
private void printstring(string str, bool acceptnewlines)
462462
{
463-
if (str != "")
463+
if (!String.IsNullOrEmpty(str))
464464
{
465-
char prev = ' ';
466-
foreach (char c in str)
465+
if (!acceptnewlines) { str = str.Replace('\n', ' '); }
466+
string[] subs = str.Split(new char[] { '§' });
467+
if (subs[0].Length > 0) { ConsoleIO.Write(subs[0]); }
468+
for (int i = 1; i < subs.Length; i++)
467469
{
468-
if (c == '§')
470+
if (subs[i].Length > 0)
469471
{
470-
prev = c;
471-
continue;
472-
}
473-
else if (prev == '§')
474-
{
475-
setcolor(c);
476-
prev = c;
477-
}
478-
else
479-
{
480-
if (c == '\n' && !acceptnewlines) { continue; }
481-
else ConsoleIO.Write(c);
472+
setcolor(subs[i][0]);
473+
if (subs[i].Length > 1)
474+
{
475+
ConsoleIO.Write(subs[i].Substring(1, subs[i].Length - 1));
476+
}
482477
}
483478
}
484479
ConsoleIO.Write('\n');

0 commit comments

Comments
 (0)