qamp/Qrakhen.Qamp.CLI/Program.cs

165 lines
5.1 KiB
C#

using Qrakhen.Qamp.Core;
using Qrakhen.Qamp.Core.Execution;
using Qrakhen.Qamp.Core.Logging;
using System.Text;
// do args here too, keep console, etc. output with exit, all the spicy things
// if no file is given for example in args just keep cli live until quit
void Init(string[] args)
{
Console.WriteLine(string.Join(',', args));
string command = args.Length > 1 ? args[0] : args.Length == 1 ? "run" : "cli";
string target = args.Length > 1 ? args[1] : args.Length == 1 ? args[0] : "";
switch (command) {
case "run":
// check wheter .sqi or .sqr file
break;
case "cli":
break;
case "read":
break;
case "digest":
break;
case "help":
break;
}
}
Init(args);
List<char> Ignored = [ '\0', '\b' ];
LoggerService.Default = LogLevel.Error;
List<byte[]> History = [];
int historyCursor = -1;
ConsoleKeyInfo input;
bool useSyntaxHighlighting = false;
(int x, int y) cursor = (0, 0);
ConsoleCode code = ConsoleCode.Error;
Runner runner = new(new Options());
do {
Console.ForegroundColor = ConsoleColor.White;
MemoryStream stream = new MemoryStream();
Console.Write(" <: ");
cursor = Console.GetCursorPosition();
do {
if (useSyntaxHighlighting)
new ConsoleRenderer(stream).Render(cursor);
input = Console.ReadKey(true);
if (input.Modifiers == ConsoleModifiers.Control) {
if (input.Key == ConsoleKey.H) {
useSyntaxHighlighting = !useSyntaxHighlighting;
Console.Write($"\n #: {nameof(useSyntaxHighlighting)} = {useSyntaxHighlighting}\n <: ");
cursor = Console.GetCursorPosition();
}
if (input.Key == ConsoleKey.L) {
LoggerService.Default = LoggerService.Default < LogLevel.All ? LogLevel.All : LogLevel.Info;
Console.Write($"\n #: LogLevel = {LoggerService.Default}\n <: ");
cursor = Console.GetCursorPosition();
}
continue;
}
if (input.Key == ConsoleKey.LeftArrow) {
if (stream.Position > 0) {
stream.Position--;
Console.CursorLeft--;
}
continue;
}
if (input.Key == ConsoleKey.RightArrow) {
if (stream.Position < stream.Length) {
stream.Position++;
Console.CursorLeft++;
}
continue;
}
if (input.Key == ConsoleKey.UpArrow) {
historyCursor = Math.Min(History.Count - 1, historyCursor + 1);
byte[] bytes = History[historyCursor];
continue;
}
if (input.Key == ConsoleKey.DownArrow) {
historyCursor = Math.Max(0, historyCursor - 1);
byte[] bytes = History[historyCursor];
continue;
}
if (input.Key == ConsoleKey.Backspace && stream.Position > 0) {
Console.CursorLeft -= 1;
Console.Write(' ');
if (!useSyntaxHighlighting)
Console.CursorLeft -= 1;
//stream.Position--;
//stream.SetLength(stream.Position);
stream.Remove(stream.Position - 1, 1);
} else if (input.Key == ConsoleKey.Enter && input.Modifiers == ConsoleModifiers.Shift) {
if (!useSyntaxHighlighting)
Console.Write("\n : ");
stream.Insert(Encoding.Default.GetBytes(Environment.NewLine));
} else if (Ignored.IndexOf(input.KeyChar) < 0) {
if (!useSyntaxHighlighting)
Console.Write(input.KeyChar);
stream.Insert(Encoding.Default.GetBytes([input.KeyChar], 0, 1));
}
} while (input.Key != ConsoleKey.Enter || input.Modifiers == ConsoleModifiers.Shift);
try {
History.Insert(0, stream.GetBuffer());
Console.WriteLine();
runner.Run(stream);
stream.Dispose();
}
catch (QampException e) {
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($" !> {e.Message}");
if (e.StackTrace != null) {
Console.Write($" ! ");
Console.WriteLine(string.Join("\n ! ", e.StackTrace.Split('\n')));
}
}
catch (Exception e) {
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($" !? {e.Message}");
if (e.StackTrace != null) {
Console.Write($" ! ");
Console.WriteLine(string.Join("\n ! ", e.StackTrace.Split('\n')));
}
}
} while(code != ConsoleCode.Exit);
public class Command
{
public class Parameters
{
}
public class Definition
{
public string Name { get; init; }
public string Alias { get; init; }
public class Parameter
{
public string Name { get; init; }
public string Alias { get; init; }
}
}
}
public class ArgumentAttribute(string name, string alias, string description)
{
public string Name { get; } = name;
public string Alias { get; } = alias;
public string Description { get; } = description;
}