1
0
Fork 0

Better Console I/O

This commit is contained in:
sheychen 2016-11-30 10:49:53 +01:00
parent 31263f8e0c
commit 73dbb09be9
11 changed files with 156 additions and 72 deletions

View File

@ -5,32 +5,6 @@ namespace Galactic_Colors_Control_Common
{
public static class Common
{
/// <summary>
/// Write line in console with correct colors
/// </summary>
/// <param name="v">Text to write</param>
/// <param name="Fore">Foreground color</param>
/// <param name="Back">Background color</param>
public static void ConsoleWrite(string v, ConsoleColor Fore = ConsoleColor.White, ConsoleColor Back = ConsoleColor.Black)
{
Console.Write("\b");
Console.ForegroundColor = Fore;
Console.BackgroundColor = Back;
Console.WriteLine(v);
ConsoleResetColor();
Console.Write(">");
}
/// <summary>
/// Reset Console Colors
/// For non black background console as Ubuntu
/// </summary>
public static void ConsoleResetColor()
{
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.Black;
}
/// <summary>
/// Simpler string array creation
/// </summary>

View File

@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using Cons = System.Console;
namespace Galactic_Colors_Control_Common
{
public class Console
{
private static string inputBuffer = "";
private static List<ColorStrings> outputBuffer = new List<ColorStrings>();
public static string Title { get { return Cons.Title; } set { Cons.Title = value; } }
public static void Write(ColorStrings Text)
{
outputBuffer.Add(Text);
while (outputBuffer.Count > Cons.WindowHeight - 2) { outputBuffer.RemoveAt(0); }
Update();
}
public static string Read()
{
ConsoleKeyInfo key = Cons.ReadKey();
while (key.Key != ConsoleKey.Enter)
{
switch (key.Key)
{
case ConsoleKey.Backspace:
if (inputBuffer.Length == 0) { SetInputPos(); }
if (inputBuffer.Length == 1) { inputBuffer = ""; }
if (inputBuffer.Length > 1) { inputBuffer = inputBuffer.Substring(0, inputBuffer.Length - 1); }
break;
default:
inputBuffer += key.KeyChar;
break;
}
key = Cons.ReadKey();
}
Cons.WriteLine();
string res = inputBuffer;
inputBuffer = "";
return res;
}
private static void Update()
{
Cons.Clear();
Cons.SetCursorPosition(0, 0);
foreach (ColorStrings output in outputBuffer) { output.Write(); }
SetInputPos();
Cons.ForegroundColor = ConsoleColor.White;
Cons.BackgroundColor = ConsoleColor.Black;
Cons.Write("> " + inputBuffer);
}
private static void SetInputPos()
{
Cons.SetCursorPosition(0, Math.Max(Cons.WindowHeight - 1, Cons.CursorTop + 1));
}
public static void ClearInput()
{
inputBuffer = ""; Update();
}
public static void ClearOutput()
{
outputBuffer.Clear(); Update();
}
}
public class ColorStrings
{
public ColorString[] Text;
public ColorStrings(params ColorString[] strings)
{
Text = strings;
}
public ColorStrings(string text)
{
Text = new ColorString[1] { new ColorString(text) };
}
public void Write()
{
foreach (ColorString cstring in Text)
{
Cons.BackgroundColor = cstring.Back;
Cons.ForegroundColor = cstring.Fore;
Cons.Write(cstring.Text);
}
Cons.WriteLine();
}
}
public class ColorString
{
public string Text;
public ConsoleColor Fore;
public ConsoleColor Back;
public ColorString(string text, ConsoleColor fore = ConsoleColor.White, ConsoleColor back = ConsoleColor.Black)
{
Text = text;
Fore = fore;
Back = back;
}
}
}

View File

@ -49,6 +49,7 @@
</Compile>
<Compile Include="Binary.cs" />
<Compile Include="Common.cs" />
<Compile Include="Console.cs" />
<Compile Include="Logger.cs" />
<Compile Include="MultiLang.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -38,13 +38,15 @@ namespace Galactic_Colors_Control_Common
public bool run { get { return _run; } }
private static bool _debug = false;
private static bool _dev = false;
private bool haveConsole = false;
/// <summary>
/// Create log file and start logger thread
/// </summary>
/// <param name="LogPath">Absolute path to logs directory</param>
public void Initialise(string LogPath, ConsoleColor[] backColor, ConsoleColor[] foreColor, logType LogLevel, bool debug, bool dev)
public void Initialise(string LogPath, ConsoleColor[] backColor, ConsoleColor[] foreColor, logType LogLevel, bool debug, bool dev, bool haveconsole = true)
{
haveConsole = haveconsole;
logPath = LogPath;
logBackColor = backColor;
logForeColor = foreColor;
@ -168,12 +170,8 @@ namespace Galactic_Colors_Control_Common
private void ConsoleWrite(Log log)
{
Console.BackgroundColor = logBackColor[(int)log.type];
Console.ForegroundColor = logForeColor[(int)log.type];
Console.Write("\b");
Console.WriteLine(DateTime.UtcNow.ToString("[yyyy-MM-dd]", CultureInfo.InvariantCulture) + ": " + log.text);
Common.ConsoleResetColor();
Console.Write(">");
if (haveConsole)
Console.Write(new ColorStrings(new ColorString(DateTime.UtcNow.ToString("[yyyy-MM-dd]", CultureInfo.InvariantCulture) + ": " + log.text, logForeColor[(int)log.type], logBackColor[(int)log.type])));
}
}
}

View File

@ -3,6 +3,7 @@ using Galactic_Colors_Control_Common;
using Galactic_Colors_Control_Common.Protocol;
using System.Reflection;
using System.Threading;
using Consol = Galactic_Colors_Control_Common.Console;
namespace Galactic_Colors_Control_Console
{
@ -45,7 +46,7 @@ namespace Galactic_Colors_Control_Console
break;
default:
Common.ConsoleWrite("Use --debug or --dev");
Consol.Write(new ColorStrings(new ColorString("Use"), new ColorString(" --debug", System.ConsoleColor.Red), new ColorString(" or"), new ColorString(" --dev", System.ConsoleColor.White, System.ConsoleColor.Red)));
break;
}
}
@ -53,19 +54,19 @@ namespace Galactic_Colors_Control_Console
while (!hostSet) //Request hostname
{
Thread.Sleep(100);
Common.ConsoleWrite(multilang.Get("EnterHostname", config.lang) + ":");
Consol.Write(new ColorStrings(multilang.Get("EnterHostname", config.lang) + ":"));
string host = client.ValidateHost(System.Console.ReadLine());
if (host[0] == '*')
{
host = host.Substring(1);
logger.Write("Validate error " + host, Logger.logType.error);
Common.ConsoleWrite(host, System.ConsoleColor.Red);
Consol.Write(new ColorStrings(new ColorString(host, System.ConsoleColor.Blue)));
client.ResetHost();
}
else
{
logger.Write("Validate " + host, Logger.logType.info);
Common.ConsoleWrite(multilang.Get("Use", config.lang) + " " + host + "? y/n");
Consol.Write(new ColorStrings(new ColorString(multilang.Get("Use", config.lang) + " "), new ColorString(host, System.ConsoleColor.Blue), new ColorString("? "), new ColorString("y", System.ConsoleColor.Green), new ColorString("/"), new ColorString("n", System.ConsoleColor.Red)));
System.ConsoleKeyInfo c = new System.ConsoleKeyInfo();
while (c.Key != System.ConsoleKey.Y && c.Key != System.ConsoleKey.N)
{
@ -81,7 +82,7 @@ namespace Galactic_Colors_Control_Console
}
}
}
Common.ConsoleWrite(multilang.Get("Loading", config.lang));
Consol.Write(new ColorStrings(multilang.Get("Loading", config.lang)));
if (client.ConnectHost()) //Try connection
{
logger.Write("Connected", Logger.logType.warm);
@ -90,7 +91,7 @@ namespace Galactic_Colors_Control_Console
//Identifaction
while (!connected)
{
Common.ConsoleWrite(multilang.Get("Username", config.lang) + ":");
Consol.Write(new ColorStrings(multilang.Get("Username", config.lang) + ":"));
string username = System.Console.ReadLine();
if (username.Length > 3)
{
@ -99,26 +100,26 @@ namespace Galactic_Colors_Control_Console
else
{
logger.Write("Identification error " + res.result, Logger.logType.info);
Common.ConsoleWrite(multilang.GetResultText(res, config.lang));
Consol.Write(new ColorStrings(new ColorString(multilang.GetResultText(res, config.lang), System.ConsoleColor.Red)));
}
}
else
{
Common.ConsoleWrite(multilang.Get("TooShort", config.lang));
Consol.Write(new ColorStrings(new ColorString(multilang.Get("TooShort", config.lang), System.ConsoleColor.Red)));
}
}
bool inparty = false;
while (!inparty)
{
System.Console.Clear();
Common.ConsoleWrite(multilang.GetResultText(client.Request(new string[2] { "party", "list" }), config.lang));
Common.ConsoleWrite(multilang.Get("Party", config.lang) + ":" + System.Environment.NewLine + " (<id> [password] or 'c' for create)");
Consol.Write(new ColorStrings(multilang.GetResultText(client.Request(new string[2] { "party", "list" }), config.lang)));
Consol.Write(new ColorStrings(multilang.Get("Party", config.lang) + ":" + System.Environment.NewLine + " (<id> [password] or 'c' for create)"));
string[] data = Common.SplitArgs(System.Console.ReadLine());
if (data.Length > 0)
{
if (data[0] == "c")
{
Common.ConsoleWrite("<party name> <player count>:");
Consol.Write(new ColorStrings("<party name> <player count>:"));
string[] split = Common.SplitArgs(System.Console.ReadLine());
if (split.Length == 2)
{
@ -126,13 +127,13 @@ namespace Galactic_Colors_Control_Console
if (createRes.type == ResultTypes.OK) { inparty = true; }
else
{
Common.ConsoleWrite(multilang.GetResultText(createRes, config.lang));
System.Console.ReadLine();
Consol.Write(new ColorStrings(new ColorString(multilang.GetResultText(createRes, config.lang), System.ConsoleColor.Red)));
System.Console.Read();
}
}
else
{
Common.ConsoleWrite("Format");
Consol.Write(new ColorStrings(new ColorString(multilang.Get("Format", config.lang), System.ConsoleColor.Red)));
}
}
else
@ -145,18 +146,18 @@ namespace Galactic_Colors_Control_Console
if (res.type == ResultTypes.OK) { inparty = true; }
else
{
Common.ConsoleWrite(multilang.GetResultText(res, config.lang));
Consol.Write(new ColorStrings(new ColorString(multilang.GetResultText(res, config.lang), System.ConsoleColor.Red)));
}
}
else
{
Common.ConsoleWrite("Format");
Consol.Write(new ColorStrings(new ColorString(multilang.Get("Format", config.lang), System.ConsoleColor.Red)));
}
}
}
}
logger.Write("Play", Logger.logType.info, Logger.logConsole.hide);
Common.ConsoleWrite(multilang.Get("Play", config.lang));
Consol.Write(new ColorStrings(new ColorString("P", System.ConsoleColor.Red), new ColorString("L", System.ConsoleColor.Green), new ColorString("A", System.ConsoleColor.Blue), new ColorString("Y", System.ConsoleColor.White)));
while (run)
{
Execute(System.Console.ReadLine()); //Process console input
@ -167,11 +168,11 @@ namespace Galactic_Colors_Control_Console
else
{
logger.Write("Connection error", Logger.logType.error);
Common.ConsoleWrite(multilang.Get("CantConnect", config.lang), System.ConsoleColor.Red);
Consol.Write(new ColorStrings(new ColorString(multilang.Get("CantConnect", config.lang), System.ConsoleColor.Red)));
}
run = false;
logger.Join();
System.Console.ReadLine();
System.Console.Read();
}
private static void Execute(string input)
@ -192,14 +193,14 @@ namespace Galactic_Colors_Control_Console
{
req = Common.Strings("say", input);
}
Common.ConsoleWrite(multilang.GetResultText(client.Request(req), config.lang));
Consol.Write(new ColorStrings(multilang.GetResultText(client.Request(req), config.lang)));
}
private static void OnEvent(object sender, System.EventArgs e)
{
//TODO add PartyKick
EventData eve = ((EventDataArgs)e).Data;
Common.ConsoleWrite(multilang.GetEventText(eve, config.lang));
Consol.Write(new ColorStrings(multilang.GetEventText(eve, config.lang)));
}
}
}

View File

@ -76,7 +76,7 @@ namespace Galactic_Colors_Control_GUI
{
config = config.Load();
logger.Write("Galactic Colors Control GUI " + Assembly.GetEntryAssembly().GetName().Version.ToString(), Logger.logType.fatal);
logger.Initialise(config.logPath, config.logBackColor, config.logForeColor, config.logLevel, Program._debug, Program._dev);
logger.Initialise(config.logPath, config.logBackColor, config.logForeColor, config.logLevel, Program._debug, Program._dev, false);
multilang.Load();
if (Program._debug) { logger.Write("CLIENT IS IN DEBUG MODE !", Logger.logType.error, Logger.logConsole.show); }
if (Program._dev) { logger.Write("CLIENT IS IN DEV MODE !", Logger.logType.error, Logger.logConsole.show); }

View File

@ -1,5 +1,6 @@
using Galactic_Colors_Control_Common;
using System;
using Console = Galactic_Colors_Control_Common.Console;
namespace Galactic_Colors_Control_GUI
{
@ -30,7 +31,7 @@ namespace Galactic_Colors_Control_GUI
break;
default:
Common.ConsoleWrite("Use --debug or --dev");
Console.Write(new ColorStrings(new ColorString("Use"), new ColorString(" --debug", System.ConsoleColor.Red), new ColorString(" or"), new ColorString(" --dev", System.ConsoleColor.White, System.ConsoleColor.Red)));
break;
}
}

View File

@ -1,7 +1,7 @@
using Galactic_Colors_Control_Common;
using Galactic_Colors_Control_Common.Protocol;
using System;
using System.Net.Sockets;
using Console = Galactic_Colors_Control_Common.Console;
namespace Galactic_Colors_Control_Server.Commands
{
@ -22,8 +22,7 @@ namespace Galactic_Colors_Control_Server.Commands
{
if (server)
{
Console.Clear();
Console.Write(">");
Console.ClearOutput();
return new RequestResult(ResultTypes.OK);
}
else

View File

@ -1,5 +1,4 @@
using Galactic_Colors_Control_Common;
using Galactic_Colors_Control_Common.Protocol;
using Galactic_Colors_Control_Common.Protocol;
using System.Net.Sockets;
namespace Galactic_Colors_Control_Server.Commands

View File

@ -7,6 +7,7 @@ using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using Console = Galactic_Colors_Control_Common.Console;
//TODO gui parties pages
@ -65,12 +66,11 @@ namespace Galactic_Colors_Control_Server
break;
default:
Common.ConsoleWrite("Use --debug or --dev");
Console.Write(new ColorStrings(new ColorString("Use"), new ColorString(" --debug", System.ConsoleColor.Red), new ColorString(" or"), new ColorString(" --dev", System.ConsoleColor.White, System.ConsoleColor.Red)));
break;
}
}
if (Type.GetType("Mono.Runtime") != null) { logger.Write("Using Mono", Logger.logType.warm, Logger.logConsole.show); }
Console.Write(">");
SetupServer();
ConsoleLoop();
CloseAllSockets();
@ -99,10 +99,9 @@ namespace Galactic_Colors_Control_Server
{
while (_run)
{
string ConsoleInput = Console.ReadLine();
Console.Write(">");
string ConsoleInput = Console.Read();
string[] args = Common.SplitArgs(ConsoleInput);
Common.ConsoleWrite(multilang.GetResultText(new ResultData(-1, Commands.Manager.Execute(args, null, true)), config.lang));
Console.Write(new ColorStrings(multilang.GetResultText(new ResultData(-1, Commands.Manager.Execute(args, null, true)), config.lang)));
ConsoleInput = null;
}
}
@ -275,7 +274,7 @@ namespace Galactic_Colors_Control_Server
if (connected) { Utilities.Broadcast(new EventData(EventTypes.ServerLeave, Common.Strings(username))); }
}
else
{
{
logger.Write("Client forcefully disconnected : ObjectDisposedException", Logger.logType.warm);
}
}

View File

@ -3,6 +3,7 @@ using Galactic_Colors_Control_Common.Protocol;
using System;
using System.Net;
using System.Net.Sockets;
using Console = Galactic_Colors_Control_Common.Console;
namespace Galactic_Colors_Control_Server
{
@ -89,11 +90,11 @@ namespace Galactic_Colors_Control_Server
switch (packet.GetType().Name)
{
case "EventData":
Common.ConsoleWrite(Server.multilang.GetEventText((EventData)packet, Server.config.lang));
Console.Write(new ColorStrings(Server.multilang.GetEventText((EventData)packet, Server.config.lang)));
break;
default:
Common.ConsoleWrite(packet.ToSmallString());
Console.Write(new ColorStrings(packet.ToSmallString()));
break;
}
}
@ -119,11 +120,11 @@ namespace Galactic_Colors_Control_Server
switch (data.GetType().Name)
{
case "EventData":
Common.ConsoleWrite(Server.multilang.GetEventText((EventData)data, Server.config.lang));
Console.Write(new ColorStrings(Server.multilang.GetEventText((EventData)data, Server.config.lang)));
break;
default:
Common.ConsoleWrite(data.ToSmallString());
Console.Write(new ColorStrings(data.ToSmallString()));
break;
}
}
@ -140,7 +141,7 @@ namespace Galactic_Colors_Control_Server
{
if (server)
{
Common.ConsoleWrite(data.ToSmallString());
Console.Write(new ColorStrings(data.ToSmallString()));
}
else
{