59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
|
|
using Qrakhen.Qamp.Core;
|
|
using Qrakhen.Qamp.Core.Tokenization;
|
|
using System.Text;
|
|
|
|
public class ConsoleRenderer
|
|
{
|
|
private readonly IReader<Token> _reader;
|
|
private readonly Stream _stream;
|
|
|
|
public ConsoleRenderer(Stream stream)
|
|
{
|
|
_stream = new MemoryStream();
|
|
stream.Position = 0;
|
|
stream.CopyTo(_stream);
|
|
_reader = new Reader(_stream);
|
|
}
|
|
|
|
public void Render((int x, int y) position)
|
|
{
|
|
Console.CursorVisible = false;
|
|
Console.SetCursorPosition(position.x, position.y);
|
|
List<Token> tokens = [];
|
|
while (!_reader.Done) {
|
|
tokens.Add(_reader.NextToken(true));
|
|
}
|
|
_stream.Position = 0;
|
|
foreach (var token in tokens) {
|
|
ConsoleColor color = ConsoleColor.Gray;
|
|
if (token.Type.IsBoolean())
|
|
color = ConsoleColor.Blue;
|
|
if (token.Type.IsString())
|
|
color = ConsoleColor.Magenta;
|
|
if (token.Type.IsNumber())
|
|
color = ConsoleColor.DarkCyan;
|
|
if (token.Type.IsOperator())
|
|
color = ConsoleColor.DarkGreen;
|
|
if (token.Type.IsBracket())
|
|
color = ConsoleColor.Red;
|
|
if (token.Type.IsIdentifier())
|
|
color = ConsoleColor.White;
|
|
if (token.Type.IsControl())
|
|
color = ConsoleColor.DarkYellow;
|
|
if (token.Type == TokenType.Error)
|
|
color = ConsoleColor.DarkRed;
|
|
Console.ForegroundColor = color;
|
|
byte[] buffer = new byte[token.Span.Length];
|
|
_stream.ReadExactly(buffer, 0, (int)token.Span.Length);
|
|
string str = Encoding.Default.GetString(buffer);
|
|
Console.Write(str);
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
if (token.Type == TokenType.NewLine)
|
|
Console.Write(" : ");
|
|
}
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.CursorVisible = true;
|
|
}
|
|
}
|