This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit b578433e37
9 changed files with 784842 additions and 0 deletions

View file

@ -0,0 +1,78 @@
namespace Brutal.ImGuiApi.Abstractions;
public static class ConsoleColor
{
public static class Float4
{
public static readonly float4 Grey = float4.Rgb(0.4f, 0.4f, 0.4f);
public static readonly float4 DarkGrey = float4.Rgb(0.1f, 0.1f, 0.1f);
public static readonly float4 Orange = float4.Rgb(1f, 0.4f, 0f);
public static readonly float4 Red = float4.Rgb(1f, 0f, 0f);
public static readonly float4 RedTransparent = float4.Rgba(1f, 0f, 0f, 0.4f);
public static readonly float4 DarkRed = float4.Rgb(0.6f, 0f, 0f);
public static readonly float4 Black = float4.Rgb(0f, 0f, 0f);
public static readonly float4 Yellow = float4.Rgb(1f, 0.7f, 0f);
public static readonly float4 YellowTransparent = float4.Rgba(1f, 0.5f, 0f, 0.4f);
public static readonly float4 Blue = float4.Rgb(0f, 0.3f, 1f);
public static readonly float4 BlueTransparent = float4.Rgba(0f, 0.3f, 1f, 0.4f);
public static readonly float4 Green = float4.Rgb(0f, 1f, 0f);
public static readonly float4 GreenTransparent = float4.Rgba(0f, 1f, 0f, 0.4f);
public static readonly float4 GreenGrey = float4.Rgb(0.1f, 0.5f, 0.1f);
public static readonly float4 HalfTransparent = float4.Rgba(1f, 1f, 1f, 0.5f);
public static readonly float4 Transparent = float4.Rgba(0f, 0f, 0f, 0f);
public static readonly float4 Cyan = float4.Rgb(0f, 1f, 1f);
public static readonly float4 Magenta = float4.Rgb(1f, 0f, 1);
public static readonly float4 White = float4.Rgb(1f, 1f, 1);
public static readonly float4 WhiteTransparent = float4.Rgba(1f, 1f, 1, 0.4f);
public static readonly float4 DarkGreyTransparent = float4.Rgba(0.1f, 0.1f, 0.1f, 0.4f);
public static readonly float4 LightBlue = float4.Unpack(byte4.Rgb(66, 200, 245), Unpack.Float.Normalize);
public static readonly float4 GreyTransparent = float4.Rgba(0.4f, 0.4f, 0.4f, 0.4f);
//public static float4 Border => ImGui.GetStyle().Colors[(int)ImGuiCol.Border];
//public static float4 TabHovered => ImGui.GetStyle().Colors[(int)ImGuiCol.TabHovered];
//public static float4 PlotHistogramHovered => ImGui.GetStyle().Colors[(int)ImGuiCol.PlotHistogramHovered];
}
public static uint ColorConvertFloat4ToU32(float4 inVector)
{
uint outColor = 0;
var r = inVector.X;
var g = inVector.Y;
var b = inVector.Z;
var a = inVector.W;
outColor += (uint)(255 * r / 1.0f) << 0;
outColor += (uint)(255 * g / 1.0f) << 8;
outColor += (uint)(255 * b / 1.0f) << 16;
outColor += (uint)(255 * a / 1.0f) << 24;
return outColor;
}
public static class Integer
{
public static readonly uint White = ColorConvertFloat4ToU32(Float4.White);
public static readonly uint WhiteTransparent = ColorConvertFloat4ToU32(Float4.WhiteTransparent);
public static readonly uint Black = ColorConvertFloat4ToU32(Float4.Black);
public static readonly uint Grey = ColorConvertFloat4ToU32(Float4.Grey);
public static readonly uint DarkGrey = ColorConvertFloat4ToU32(Float4.DarkGrey);
public static readonly uint Yellow = ColorConvertFloat4ToU32(Float4.Yellow);
public static readonly uint YellowTransparent = ColorConvertFloat4ToU32(Float4.YellowTransparent);
public static readonly uint GreenTransparent = ColorConvertFloat4ToU32(Float4.GreenTransparent);
public static readonly uint Orange = ColorConvertFloat4ToU32(Float4.Orange);
public static readonly uint Red = ColorConvertFloat4ToU32(Float4.Red);
public static readonly uint RedTransparent = ColorConvertFloat4ToU32(Float4.RedTransparent);
public static readonly uint Green = ColorConvertFloat4ToU32(Float4.Green);
public static readonly uint GreenGrey = ColorConvertFloat4ToU32(Float4.GreenGrey);
public static readonly uint Blue = ColorConvertFloat4ToU32(Float4.Blue);
public static readonly uint LightBlue = ColorConvertFloat4ToU32(Float4.LightBlue);
public static readonly uint BlueTransparent = ColorConvertFloat4ToU32(Float4.BlueTransparent);
//public static readonly uint TabHovered = ColorConvertFloat4ToU32(Float4.TabHovered);
//public static readonly uint Border = ColorConvertFloat4ToU32(Float4.Border);
public static readonly uint HalfTransparent = ColorConvertFloat4ToU32(Float4.HalfTransparent);
public static readonly uint Transparent = ColorConvertFloat4ToU32(Float4.Transparent);
public static readonly uint DarkGreyTransparent = ColorConvertFloat4ToU32(Float4.DarkGreyTransparent);
public static readonly uint GreyTransparent = ColorConvertFloat4ToU32(Float4.GreyTransparent);
}
}

View file

@ -0,0 +1,17 @@
namespace Brutal.ImGuiApi.Abstractions;
public static class ConsoleLineColors
{
public static readonly ImColor8 Default = new(178, 178, 178, 255);
}
public struct ConsoleLogEntry()
{
public ImColor8 Color = ConsoleLineColors.Default;
public bool IsEmpty => TextSpanHandle.Length == 0;
public ConsoleWindow.RingBuffer.SpanHandle TextSpanHandle = new();
public ConsoleWindow.RingBuffer.SpanHandle TimeSpanHandle = new();
public float ActiveTime;
}

View file

@ -0,0 +1,889 @@
using System.Buffers;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using Brutal.ImGuiApi.Internal;
using Brutal.Strings;
using Microsoft.Extensions.Logging;
namespace Brutal.ImGuiApi.Abstractions;
/// <summary>
/// This logger receives log messages, formats them and sends them to the ConsoleWindow
/// </summary>
public class ConsoleWindowLogger : ILogger, IDisposable
{
private readonly ConsoleWindow _consoleWindow;
private readonly string _categoryName;
/// <summary>
/// Log formatter function - users can override this, but we provide a default
/// LogLevel: The log level
/// string: The category name
/// string: The log message
/// Returns the formatted log string
/// </summary>
public static Func<LogLevel, string, string, string>? LogFormatter { get; set; }
public ConsoleWindowLogger(ConsoleWindow consoleWindow, string categoryName)
{
_categoryName = categoryName;
_consoleWindow = consoleWindow;
}
public void Dispose() { }
/// <summary>
/// Receive a log message, format it and send it to the ConsoleWindow
/// </summary>
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter
)
{
var formattedMessage = formatter(state, exception);
var message = LogFormatter != null ? LogFormatter(logLevel, _categoryName, formattedMessage) : formattedMessage;
_consoleWindow.OnLog(_categoryName, logLevel, message);
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public IDisposable? BeginScope<TState>(TState state)
where TState : notnull
{
return null;
}
}
public class ConsoleWindow : ILoggerProvider
{
public const int DEFAULT_LOG_TIME = 5;
public const int INITIAL_BUFFER_CAPACITY = 128;
private readonly struct PendingMessage
{
public readonly int TextOffset;
public readonly int TextLength; // -1 signals a Clear
public readonly DateTime Timestamp;
public readonly ImColor8 Color;
public readonly int TimeToShow;
public PendingMessage(int textOffset, int textLength, ImColor8 color, int timeToShow)
{
TextOffset = textOffset;
TextLength = textLength;
Timestamp = DateTime.Now;
Color = color;
TimeToShow = timeToShow;
}
}
private readonly Lock _pendingLock = new();
private bool _pendingClear;
private const int BUFFER_COUNT = 2;
private const int PENDING_TEXT_BUFFER_SIZE = 1024 * 1024; // 1MB pre-allocated for the pending text per-buffer for now, should be enough
private int _currentActiveBuffer = 0;
private readonly List<PendingMessage>[] _pendingMessages = new List<PendingMessage>[BUFFER_COUNT] { new(), new() };
private readonly byte[][] _pendingText = [new byte[PENDING_TEXT_BUFFER_SIZE], new byte[PENDING_TEXT_BUFFER_SIZE]];
private readonly int[] _pendingTextPos = new int[BUFFER_COUNT];
private class GrowingRingBuffer<T>(int initialCapacity)
{
private T[] _backingArray = new T[initialCapacity];
public int Begin;
public int End;
public int Capacity => _backingArray.Length;
public int Count => End >= Begin ? End - Begin : Capacity - Begin + End;
public ref T this[int index] => ref _backingArray[(Begin + index) % Capacity];
public static implicit operator Span<T>(GrowingRingBuffer<T> array) => array._backingArray;
public void Reset(int newCapacity)
{
if (newCapacity != Capacity)
_backingArray = new T[newCapacity];
Begin = 0;
End = 0;
}
public void Resize(int newCapacity)
{
var oldCount = Count;
var newArray = new T[newCapacity];
if (End > Begin)
{
// |--BbbeeE--| |BbbeeE--------------|
Array.Copy(_backingArray, Begin, newArray, 0, End - Begin);
}
else
{
// |eeE----Bbb| |BbbeeE--------------|
Array.Copy(_backingArray, Begin, newArray, 0, Capacity - Begin);
Array.Copy(_backingArray, 0, newArray, Capacity - Begin, End);
}
_backingArray = newArray;
Begin = 0;
End = oldCount;
Debug.Assert(End != Begin);
Debug.Assert(End < Capacity);
}
public void Push(T newItem)
{
if ((End + 1) % Capacity == Begin)
Resize(Capacity * 2);
_backingArray[End] = newItem;
End = (End + 1) % Capacity;
Debug.Assert(End != Begin);
}
}
private readonly GrowingRingBuffer<ConsoleLogEntry> _consoleBuffer = new(INITIAL_BUFFER_CAPACITY);
public void DrawStats()
{
ImGui.Text($"Capacity: {_consoleBuffer.Capacity}");
ImGui.Text($"Count: {_consoleBuffer.Count}");
ImGui.Text($"Begin: {_consoleBuffer.Begin}");
ImGui.Text($"End: {_consoleBuffer.End}");
var units = Math.Min(20, _consoleBuffer.Capacity);
var region1Units = 0;
var region2Units = 0;
var region3Units = 0;
var region1Char = '?';
var region2Char = '?';
var region3Char = '?';
var region1Color = ConsoleColor.Float4.Magenta;
var region2Color = ConsoleColor.Float4.Magenta;
var region3Color = ConsoleColor.Float4.Magenta;
if (_consoleBuffer.Begin > _consoleBuffer.End)
{
region1Units = (int)((float)_consoleBuffer.End / _consoleBuffer.Capacity * units);
region2Units = (int)((float)(_consoleBuffer.Begin - _consoleBuffer.End) / _consoleBuffer.Capacity * units);
region1Char = 'x';
region2Char = '-';
region3Char = 'x';
region1Color = ConsoleColor.Float4.Green;
region2Color = ConsoleColor.Float4.Grey;
region3Color = ConsoleColor.Float4.Green;
}
else
{
region1Units = (int)(((float)_consoleBuffer.Begin / _consoleBuffer.Capacity) * units);
var num = _consoleBuffer.End - _consoleBuffer.Begin;
region2Units = (int)((float)num / _consoleBuffer.Capacity * units);
if (region2Units == 0 && num != 0)
region2Units = 1;
region1Char = '-';
region2Char = 'x';
region3Char = '-';
region1Color = ConsoleColor.Float4.Grey;
region2Color = ConsoleColor.Float4.Green;
region3Color = ConsoleColor.Float4.Grey;
}
region3Units = units - region1Units - region2Units;
ImGui.Text("Buffer: |"u8);
ImGui.SameLine();
ImGui.TextColored(region1Color, new string(region1Char, region1Units));
ImGui.SameLine();
ImGui.TextColored(region2Color, new string(region2Char, region2Units));
ImGui.SameLine();
ImGui.TextColored(region3Color, new string(region3Char, region3Units));
ImGui.SameLine();
ImGui.Text("|"u8);
}
public TerminalInterface Terminal;
private bool _show;
private bool _setFocus;
private bool _overwrite;
private bool _scrollToBottom;
private bool _autoScroll = false;
private bool _fadeMessages = true;
private readonly ImInputString _consoleInput = new(512, []);
public readonly RingBuffer Storage;
private bool _clearConsoleInput;
public bool IsOpen => _show;
// #TODO-batchmode: Implement BatchMode()
private float2 _inputSize;
private bool _inputLastActive;
private readonly string[] _commandBuffer = new string[8];
private int _commandBufferIndex;
private int _headIndex;
private int _tailIndex;
private static readonly ImColor8 DefaultColor = ConsoleColor.ColorConvertFloat4ToU32(
new float4(0.7f, 0.7f, 0.7f, 1f)
);
public static ImColor8 InfoColor = DefaultColor;
public static ImColor8 DebugColor = ConsoleColor.Integer.Yellow;
public static ImColor8 WarningColor = ConsoleColor.Integer.Orange;
public static ImColor8 ErrorColor = ConsoleColor.Integer.Red;
public static ImColor8 CriticalColor = ConsoleColor.Integer.Red;
public static ImColor8 TraceColor = ConsoleColor.Integer.GreenGrey;
/// <summary>
/// Scale of the font in the console window.
/// Use as a field so it can be referenced in an ImGui slider etc.
/// </summary>
public float FontScale = 1f;
/// <summary>
/// Font Pointer for rendering texting to console window
/// <para></para>
/// This shouldn't really be changed. Only used for more easily testing fonts for ConsoleWindow.
/// For the sake of consistency between games. Leave this value as is.
/// </summary>
public ImFontPtr FontPtr;
public ConsoleWindow()
: this(ByteSize.FromMegabytes(2)) { }
public ConsoleWindow(ByteSize bufferSize)
{
Storage = new RingBuffer(bufferSize);
Terminal = new(this);
}
/// <summary>
/// Retrieves the default font for the console window.
/// If no fonts are available in the font atlas, a default font is added and returned.
/// Otherwise, the first font in the font atlas is returned.
/// </summary>
/// <returns>The pointer to the default font in the font atlas.</returns>
public ImFontPtr GetDefaultFont()
{
var fontAtlas = ImGui.GetIO().Fonts;
var fonts = fontAtlas.Fonts;
return fonts.Count == 0 ? fontAtlas.AddFontDefault() : fonts[0];
}
public ILogger CreateLogger(string categoryName)
{
return new ConsoleWindowLogger(this, categoryName);
}
public void OnLog(string categoryName, LogLevel logLevel, string message)
{
// Otherwise just sort by log level
switch (logLevel)
{
case LogLevel.None:
case LogLevel.Information:
Print(message, InfoColor);
break;
case LogLevel.Trace:
Print(message, TraceColor);
break;
case LogLevel.Debug:
Print(message, DebugColor);
break;
case LogLevel.Warning:
Print(message, WarningColor);
break;
case LogLevel.Error:
Print(message, ErrorColor);
break;
case LogLevel.Critical:
Print(message, CriticalColor);
break;
default:
throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null);
}
}
public void Show()
{
_show = true;
_setFocus = true;
_inputLastActive = true;
}
public void Hide()
{
_show = false;
_inputLastActive = false;
}
public void EnableMessageFading()
{
_fadeMessages = true;
}
public void DisableMessageFading()
{
_fadeMessages = false;
}
private void DrawLog(float unscaledDeltaTime, bool noFade = false)
{
_inputSize = ImGui.GetStyle().WindowPadding;
for (var i = 0; i < _consoleBuffer.Count; ++i)
Draw(ref _consoleBuffer[i], ref _inputSize, _show, unscaledDeltaTime, noFade);
}
/// <summary>
/// Draws the ConsoleWindow to the screen using the position and size of the current viewport
/// </summary>
/// <param name="viewportPosition"></param>
/// <param name="viewportSize"></param>
/// <param name="unscaledDeltaTime"></param>
/// <param name="noFade"></param>
public void Draw(float2 viewportPosition, float2 viewportSize, float unscaledDeltaTime, bool noFade = false)
{
// Swap the buffers before to resolve all the "pending messages"
SwapBuffers();
if (FontPtr.IsNull())
FontPtr = GetDefaultFont();
ImGui.PushFont(FontPtr, ImGui.GetStyle().FontSizeBase * FontScale);
// PREPARE WINDOW FLAGS
var flags =
ImGuiWindowFlags.NoCollapse
| ImGuiWindowFlags.NoSavedSettings
| ImGuiWindowFlags.NoTitleBar
| ImGuiWindowFlags.NoMove
| ImGuiWindowFlags.NoResize
| ImGuiWindowFlags.NoScrollbar
| ImGuiWindowFlags.NoMouseInputs;
if (!_show)
flags |= ImGuiWindowFlags.NoBackground;
// CREATE WINDOW SIZE
var scalingSize = _inputSize.Y + ImGui.GetItemRectSize().Y;
var ySize = _show ? Math.Min(scalingSize, viewportSize.Y / 3) : _inputSize.Y;
ImGui.SetNextWindowSize(viewportSize with { Y = ySize }, ImGuiCond.Always);
ImGui.SetNextWindowPos(viewportPosition + new float2(0, viewportSize.Y - ySize), ImGuiCond.Always);
// SET STYLES FOR WINDOW
ImGui.PushStyleVar(ImGuiStyleVar.WindowRounding, 0f);
ImGui.PushStyleColor(ImGuiCol.WindowBg, ConsoleColor.Integer.Black);
ImGui.PushStyleColor(ImGuiCol.Border, ConsoleColor.Integer.DarkGrey);
// BEGIN THE DRAW PHASE
if (ImGui.Begin("ConsoleWindow"u8, ref _show, flags))
{
//POP THE BORDER AND BG STYLES
ImGui.PopStyleColor(2);
ImGui.PopStyleVar();
// IF CONSOLE IS OPEN
if (_show)
{
// DRAW A SCROLL AREA FOR HISTORY
var footerSize = ImGui.GetFrameHeightWithSpacing();
if (ImGui.BeginChild("ScrollingRegion"u8, new float2(0, -footerSize)))
{
DrawLog(unscaledDeltaTime, noFade);
if (_scrollToBottom || (_autoScroll && ImGui.GetScrollY() >= ImGui.GetScrollMaxY()))
ImGui.SetScrollHereY(1.0f);
_scrollToBottom = false;
ImGui.EndChild();
}
//ImGui.Separator();
// PREPARE INPUT FIELD
ImGui.Text(">"u8);
ImGui.SameLine();
ImGui.PushItemWidth(ImGui.GetContentRegionAvail().X);
ImGui.PushStyleColor(ImGuiCol.Border, ConsoleColor.Integer.Transparent);
ImGui.PushStyleColor(ImGuiCol.FrameBg, ConsoleColor.Integer.Transparent);
// DRAW INPUT FIELD
var id = "###ConsoleInput"u8;
if (_overwrite)
{
_overwrite = false;
ImGui.Internal.GetInputTextState(ImGui.GetID(id)).ReloadUserBufAndMoveToEnd();
}
ImGui.InputText(id, _consoleInput);
_inputLastActive = ImGui.IsItemActive();
// SET FOCUS ON INPUT FIELD
ImGuiUtils.SetLastFocusOnAppearing(true);
_inputSize += ImGui.GetItemRectSize();
ImGui.PopStyleColor(2);
ImGui.PopItemWidth();
if (ImGui.IsItemVisible())
{
if (_setFocus)
{
ImGui.SetWindowFocus();
ImGui.SetKeyboardFocusHere();
_setFocus = false;
_scrollToBottom = true;
}
else if (!ImGui.IsWindowFocused())
{
_show = false;
_scrollToBottom = true;
}
}
}
// IF CONSOLE IS CLOSED
else
{
// DRAW FADING CRITICAL LOG HISTORY SUCH AS ACTIONS OR ERRORS
DrawLog(unscaledDeltaTime, noFade);
}
// CLEAR THE INPUT FIELD
if (_clearConsoleInput)
{
_clearConsoleInput = false;
_consoleInput.Clear();
_scrollToBottom = true;
}
}
//END THE DRAW PHASE
ImGui.End();
ImGui.PopFont();
}
public void Submit(string input)
{
_consoleInput.Value16 = input;
Submit();
}
private void Submit()
{
if (_consoleInput.IsEmpty)
{
Hide();
return;
}
Terminal.Execute(_consoleInput.ToString());
_clearConsoleInput = true;
_setFocus = true;
_scrollToBottom = true;
}
public void ClearConsole()
{
// #TODO-batchmode Implement clear console for batch mode
lock (_pendingLock)
{
var buf = _currentActiveBuffer;
_pendingMessages[buf].Clear();
_pendingTextPos[buf] = 0;
_pendingClear = true;
}
}
public void PrintMessage(string message) => Print(message, ConsoleColor.Integer.LightBlue);
// Default colour
public void Print(string text, int timeToShow = DEFAULT_LOG_TIME) => Print(text, DefaultColor, timeToShow);
public void Print(ImString text, int timeToShow = DEFAULT_LOG_TIME) =>
Print((ReadOnlySpan<byte>)text, DefaultColor, timeToShow);
public void Print(ReadOnlySpan<char> text, int timeToShow = DEFAULT_LOG_TIME) =>
Print(text, DefaultColor, timeToShow);
public void Print(ReadOnlySpan<byte> text, int timeToShow = DEFAULT_LOG_TIME) =>
Print(text, DefaultColor, timeToShow);
// With colour
public void Print(ImString text, ImColor8 color, int timeToShow = DEFAULT_LOG_TIME) =>
AddPendingMessage((ReadOnlySpan<byte>)text, color, timeToShow);
public void Print(ReadOnlySpan<byte> text, ImColor8 color, int timeToShow = DEFAULT_LOG_TIME) =>
AddPendingMessage(text, color, timeToShow);
public void Print(ReadOnlySpan<char> text, ImColor8 color, int timeToShow = DEFAULT_LOG_TIME) =>
AddPendingMessage(text, color, timeToShow);
private void AddPendingMessage(ReadOnlySpan<byte> text, ImColor8 color, int timeToShow)
{
// Don't show the message in the fading list if it's disabled
if (!_fadeMessages)
timeToShow = 0;
lock (_pendingLock)
{
var bufIdx = _currentActiveBuffer;
var offset = _pendingTextPos[bufIdx];
Debug.Assert(
offset + text.Length <= PENDING_TEXT_BUFFER_SIZE,
"Pending text buffer overflow: increase PENDING_TEXT_BUFFER_SIZE"
);
if (offset + text.Length > PENDING_TEXT_BUFFER_SIZE)
return;
text.CopyTo(_pendingText[bufIdx].AsSpan(offset));
_pendingTextPos[bufIdx] += text.Length;
_pendingMessages[bufIdx].Add(new PendingMessage(offset, text.Length, color, timeToShow));
}
}
private void AddPendingMessage(ReadOnlySpan<char> text, ImColor8 color, int timeToShow)
{
// Don't show the message in the fading list if it's disabled
if (!_fadeMessages)
timeToShow = 0;
var byteCount = Utf8.GetByteCount(text);
lock (_pendingLock)
{
var bufIdx = _currentActiveBuffer;
var offset = _pendingTextPos[bufIdx];
Debug.Assert(
offset + byteCount <= PENDING_TEXT_BUFFER_SIZE,
"Pending text buffer overflow: increase PENDING_TEXT_BUFFER_SIZE"
);
if (offset + byteCount > PENDING_TEXT_BUFFER_SIZE)
return;
var status = Utf8.FromUtf16(text, _pendingText[bufIdx].AsSpan(offset), out _, out var bytesWritten);
if (status != OperationStatus.Done)
throw new InvalidOperationException($"Failed to convert UTF-16 to UTF-8: {status}");
_pendingTextPos[bufIdx] += bytesWritten;
_pendingMessages[bufIdx].Add(new PendingMessage(offset, bytesWritten, color, timeToShow));
}
}
public void SwapBuffers()
{
int active;
bool clear;
lock (_pendingLock)
{
active = _currentActiveBuffer;
_currentActiveBuffer = (active + 1) % BUFFER_COUNT;
clear = _pendingClear;
_pendingClear = false;
}
if (clear)
{
Storage.Reset();
_consoleBuffer.Reset(INITIAL_BUFFER_CAPACITY);
}
var messages = CollectionsMarshal.AsSpan(_pendingMessages[active]);
var pendingText = _pendingText[active].AsSpan(0, _pendingTextPos[active]);
for (var i = 0; i < messages.Length; ++i)
{
ref readonly var msg = ref messages[i];
Apply(in msg, pendingText.Slice(msg.TextOffset, msg.TextLength));
}
_pendingMessages[active].Clear();
_pendingTextPos[active] = 0;
}
private void Apply(in PendingMessage pending, ReadOnlySpan<byte> text)
{
var textHandle = Storage.Append(text);
Span<byte> timeBytes = stackalloc byte[8]; // "HH:mm:ss" is always 8 bytes
pending.Timestamp.TryFormat(timeBytes, out var timeBytesWritten, "HH:mm:ss", CultureInfo.InvariantCulture);
_consoleBuffer.Push(
new ConsoleLogEntry
{
TextSpanHandle = textHandle,
Color = pending.Color,
TimeSpanHandle = Storage.Append(timeBytes[..timeBytesWritten]),
ActiveTime = pending.TimeToShow,
}
);
// push the begin pointer until we find the next valid message
while (Storage.TokenIsStale(_consoleBuffer[0].TextSpanHandle))
_consoleBuffer.Begin = (_consoleBuffer.Begin + 1) % _consoleBuffer.Capacity;
}
public enum Actions
{
Up,
Down,
Submit,
Toggle,
}
/// <summary>
/// Callback for handling input actions.
/// </summary>
/// <param name="action">One of the supported actions.</param>
/// <returns>True if the action was handled, false otherwise.</returns>
public bool HandleInputAction(Actions action)
{
if (_inputLastActive)
{
if (action is Actions.Up or Actions.Down)
{
// save the current input if it is not already saved
if (_commandBufferIndex == _headIndex)
_commandBuffer[_headIndex] = _consoleInput.ToString();
if (action is Actions.Up)
{
var next = _commandBufferIndex == 0 ? _commandBuffer.Length - 1 : _commandBufferIndex - 1;
if (_commandBufferIndex != _tailIndex)
{
_commandBufferIndex = next;
_consoleInput.Value16 = _commandBuffer[_commandBufferIndex];
_overwrite = true;
}
}
else
{
// key is down
if (_commandBufferIndex != _headIndex)
{
_commandBufferIndex = (_commandBufferIndex + 1) % _commandBuffer.Length;
_consoleInput.Value16 = _commandBuffer[_commandBufferIndex];
_overwrite = true;
}
}
return true;
}
}
if (!_show && action == Actions.Toggle)
{
Show();
return true;
}
if (!_show)
return false;
if (action == Actions.Toggle)
{
Hide();
return true;
}
if (action is Actions.Submit)
{
Submit();
if (!_consoleInput.IsEmpty)
{
var inputString = _consoleInput.ToString();
if (_commandBufferIndex == _headIndex)
{
// we wrote to the head, so we need to move the head
_commandBuffer[_commandBufferIndex] = inputString;
_headIndex = (_commandBufferIndex + 1) % _commandBuffer.Length;
}
else
{
if (inputString != _commandBuffer[_commandBufferIndex])
{
// we have edited an old command, so we need to move the head and write to the head
_commandBuffer[_headIndex] = inputString;
_headIndex = (_headIndex + 1) % _commandBuffer.Length;
}
}
_commandBufferIndex = _headIndex;
// if the head is at the tail, we need to move the tail
if (_headIndex == _tailIndex)
_tailIndex = (_tailIndex + 1) % _commandBuffer.Length;
_consoleInput.Clear();
_overwrite = true;
}
return true;
}
return false;
}
public void Draw(
ref ConsoleLogEntry line,
ref float2 inputSize,
bool isShown,
float unscaledDeltaTime,
bool noFade = false
)
{
Debug.Assert(!Storage.TokenIsStale(line.TimeSpanHandle));
if (line.IsEmpty || !isShown && line.ActiveTime <= 0f)
return;
var isShowing = !isShown & line.ActiveTime <= 1f;
if (isShowing)
{
float alpha;
if (noFade)
alpha = line.ActiveTime > 0f ? 1f : 0f;
else
alpha = float.Lerp(0f, 1f, line.ActiveTime);
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, alpha);
}
DrawTime(line, isShown);
ImGui.SameLine();
var textString = Storage.GetSpan(line.TextSpanHandle);
if (!isShown)
{
ImGuiUtils.TextShadow(textString, line.Color);
}
else
{
ImGui.PushStyleColor(ImGuiCol.Text, line.Color);
ImGui.TextWrapped(textString);
ImGui.PopStyleColor();
}
inputSize += ImGui.GetItemRectSize() + ImGui.GetStyle().ItemSpacing;
if (isShowing)
ImGui.PopStyleVar();
if (line.ActiveTime > 0)
line.ActiveTime -= unscaledDeltaTime;
}
private void DrawTime(ConsoleLogEntry line, bool isShown)
{
var timeString = Storage.GetSpan(line.TimeSpanHandle);
if (!isShown)
{
ImGuiUtils.TextShadow(timeString, ConsoleColor.Integer.Grey);
}
else
{
ImGui.PushStyleColor(ImGuiCol.Text, ConsoleColor.Integer.Grey);
ImGui.Text(timeString);
ImGui.PopStyleColor();
}
}
public void Dispose() { }
public class RingBuffer(ByteSize capacity)
{
public readonly record struct SpanHandle(int Offset, int Length, uint Generation);
private readonly byte[] _array = new byte[capacity.Bytes];
internal int Position;
private int Remaining => _array.Length - Position;
public int CurrentPosition => Position;
public uint Generation { get; private set; }
public Span<byte> Destination => _array.AsSpan(Position);
private SpanHandle Advance(int bytesWritten) => Advance(bytesWritten, true, Position);
public SpanHandle Advance(int bytesWritten, bool nullTerminate, int start)
{
Position += bytesWritten;
var length = Position - start; // don't include the null terminator
if (nullTerminate)
_array[Position++] = 0;
return new SpanHandle(start, length, Generation);
}
public void EnsureCapacity(int requiredBytes)
{
if (requiredBytes > _array.Length)
throw new OutOfMemoryException(
$"Required bytes {requiredBytes} exceed buffer capacity {_array.Length}"
);
if (Remaining < requiredBytes)
{
Position = 0; // reset position to the beginning of the buffer if we exceed capacity
++Generation; // Increment the generation when we reset the position to track stale Tokens
}
}
public SpanHandle Append(ReadOnlySpan<byte> value)
{
EnsureCapacity(value.Length + 1); // +1 for null terminator added by Advance
value.CopyTo(_array.AsSpan(Position));
return Advance(value.Length);
}
public ReadOnlySpan<byte> AppendToSpan(ReadOnlySpan<byte> value)
{
return GetSpan(Append(value));
}
private SpanHandle Append(ReadOnlySpan<char> value, int requiredBytes)
{
EnsureCapacity(requiredBytes + 1); // +1 for null terminator added by Advance
var status = Utf8.FromUtf16(value, _array.AsSpan(Position), out _, out var bytesWritten);
if (status != OperationStatus.Done)
throw new InvalidOperationException($"Failed to convert UTF-16 to UTF-8: {status}");
return Advance(bytesWritten);
}
public SpanHandle Append(ReadOnlySpan<char> value)
{
return Append(value, Utf8.GetByteCount(value));
}
public ReadOnlySpan<byte> AppendToSpan(ReadOnlySpan<char> value)
{
return GetSpan(Append(value));
}
public void Reset()
{
Position = 0;
++Generation;
}
public bool TokenIsStale(SpanHandle spanHandle)
{
if (spanHandle.Generation == Generation)
return false; // we are the current generation
if (spanHandle.Generation == Generation - 1)
return Position > spanHandle.Offset;
return true; // any older generations are always stale
}
public ReadOnlySpan<byte> GetSpan(SpanHandle spanHandle)
{
return _array.AsSpan(spanHandle.Offset, spanHandle.Length);
}
}
}

View file

@ -0,0 +1,76 @@
using System.Text;
namespace Brutal.ImGuiApi.Abstractions;
public class TableString
{
private readonly List<Row> _rows = new();
private readonly List<int> _columnWidths = new();
public readonly struct Row
{
public readonly String[] Values;
public Row(string[] values)
{
Values = values;
}
public static Row Create(params string[] values) => new(values);
public void ToConsole(ConsoleWindow console)
{
foreach (var value in Values)
console.Print(value);
}
}
public void AddRow(params string[] values) => _rows.Add(new Row(values));
public void ToConsole(ConsoleWindow console)
{
// populate column widths
foreach (var row in _rows)
{
for (var i = 0; i < row.Values.Length; i++)
{
if (i >= _columnWidths.Count)
_columnWidths.Add(0);
var rowColumnValue = row.Values[i].Length + 1;
if (rowColumnValue > _columnWidths[i])
_columnWidths[i] = rowColumnValue;
}
}
foreach (var row in _rows)
{
var sb = new StringBuilder();
for (var i = 0; i < row.Values.Length; i++)
{
var value = row.Values[i];
switch (i)
{
case > 0:
sb.Append(' ');
break;
case 0:
sb.Append("| ");
break;
}
sb.Append(value.PadRight(_columnWidths[i]));
sb.Append(" |");
}
console.Print(sb.ToString());
}
}
public void AddRow(Row toRow) => _rows.Add(toRow);
public void Sort()
{
// sort rows by first column
_rows.Sort((a, b) => string.Compare(a.Values[0], b.Values[0], StringComparison.Ordinal));
}
}

View file

@ -0,0 +1,53 @@
namespace Brutal.ImGuiApi.Abstractions;
/// <summary>
/// Defines how a terminal action should be parsed
/// </summary>
public enum ArgParseMode
{
/// <summary>
/// Parse the command string args into individual tokens, considering whitespace and quotes
/// </summary>
Default,
/// <summary>
/// Output the raw unparsed command arg string
/// </summary>
Unparsed,
}
/// <summary>
/// An attribute which is used to define methods that can be registered as callback through a TerminalInterface
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class TerminalActionAttribute : Attribute
{
/// <param name="commandName">The command name</param>
/// <param name="description">A description of the command</param>
/// <param name="parsingMode">The type of parsing which should be used on the command</param>
public TerminalActionAttribute(
string commandName,
string description = "",
ArgParseMode parsingMode = ArgParseMode.Default
)
{
CommandName = commandName.ToLower();
Description = description;
ParsingMode = parsingMode;
}
/// <summary>
/// The name of the command
/// </summary>
public string CommandName { get; }
/// <summary>
/// A description of the command
/// </summary>
public string Description { get; }
/// <summary>
/// The parsing mode of the command
/// </summary>
public ArgParseMode ParsingMode { get; }
}

View file

@ -0,0 +1,197 @@
namespace Brutal.ImGuiApi.Abstractions;
/// <summary>
/// Defines the arguments which a command takes
/// </summary>
public struct ArgDefinition
{
/// <summary>
/// The name of the arg
/// </summary>
public string Name;
/// <summary>
/// The type of the arg
/// </summary>
public Type Type;
/// <summary>
/// Whether or not this arg has a default value
/// </summary>
public bool HasDefault;
/// <summary>
/// ToString() helper
/// </summary>
public override string ToString()
{
return Name;
}
}
/// <summary>
/// A struct which represents a command which can be called through the terminal interface
/// </summary>
public readonly struct Command
{
private readonly List<ArgDefinition> _argDefinitions;
private readonly Delegate _callback;
/// <summary>
/// A description of the command
/// </summary>
public string Description { get; }
/// <summary>
/// The parsing mode which should be used for this command
/// </summary>
public ArgParseMode ParsingMode { get; }
/// <summary>
/// Constructor, which takes a callback function and uses its parameters to define argument definitions
/// </summary>
/// <param name="callback"></param>
/// <param name="description"></param>
/// <param name="parsingMode"></param>
public Command(Delegate callback, string description, ArgParseMode parsingMode)
{
_argDefinitions = new List<ArgDefinition>();
_callback = callback;
Description = description;
ParsingMode = parsingMode;
CreateArgDefinitions();
}
/// <summary>
/// Call to issue this command
/// </summary>
/// <param name="args">The args to call the command with</param>
public void Issue(string[]? args)
{
ParseArgs(args, out var parsedArgs);
_callback.Method.Invoke(_callback.Target, parsedArgs);
}
/// <summary>
/// Returns a readonly list of the arg definitions for this command
/// </summary>
public IReadOnlyList<ArgDefinition> GetDefinitions => _argDefinitions;
private void ParseArgs(string[]? args, out object[]? outParsedArgs)
{
if (args?.Length > _argDefinitions.Count)
throw new TerminalInterfaceException(
"Command parsing failed,"
+ $" too many arguments for command: {_callback.Method.Name}."
+ $" Expected: {_argDefinitions.Count}, got: {args.Length}."
);
if (_argDefinitions.Count == 0)
{
outParsedArgs = null;
return;
}
outParsedArgs = new object[_argDefinitions.Count];
// Iterate through our defined definitions and try to parse the args into the correct types
for (var i = 0; i < _argDefinitions.Count; i++)
{
var definition = _argDefinitions[i];
ref var outArg = ref outParsedArgs[i];
if (args is null || args.Length <= i)
{
// If an arg is optional pass Type.Missing to use the function's defined default
if (definition.HasDefault)
{
outArg = Type.Missing;
continue;
}
throw new TerminalInterfaceException(
"Command parsing failed,"
+ $" missing required argument: {definition.Name}"
+ $" of type: {definition.Type.Name}"
);
}
var arg = args[i];
TryParse(arg, definition.Type, out outArg);
}
}
private static void TryParse(string arg, Type type, out object value)
{
// TODO: Add specific cases that we want to handle with our own parsing logic - e.g. vectors
try
{
value = Convert.ChangeType(arg, type);
}
catch
{
throw new TerminalInterfaceException($"Failed to parse command argument: {arg} to type: {type.Name}.");
}
}
private void CreateArgDefinitions()
{
var parameters = _callback.Method.GetParameters();
// Validation of single string parsing mode
if (ParsingMode == ArgParseMode.Unparsed)
{
if (parameters.Length != 1)
throw new TerminalInterfaceException(
"Failed to parse delegate parameters into argument types,"
+ " single string parsing mode requires exactly one parameter"
+ $" in callback: {_callback.Method.Name}"
);
if (parameters[0].ParameterType != typeof(string))
throw new TerminalInterfaceException(
"Failed to parse delegate parameters into argument types,"
+ " single string parsing mode requires a string parameter"
+ $" in callback: {_callback.Method.Name}"
);
}
foreach (var parameter in parameters)
{
if (!TypeIsSupported(parameter.ParameterType))
throw new TerminalInterfaceException(
"Failed to parse delegate parameters into argument types,"
+ $" unsupported argument type: {parameter.ParameterType.Name}"
+ $" in callback: {_callback.Method.Name}"
);
var definition = new ArgDefinition
{
Name = parameter.Name ?? $"param_{parameter.Position}",
Type = parameter.ParameterType,
HasDefault = parameter.HasDefaultValue,
};
_argDefinitions.Add(definition);
}
}
private static bool TypeIsSupported(Type type)
{
// Convert.ChangeType() supports primitives, strings, and DateTime
if (type.IsPrimitive)
return true;
if (type == typeof(string))
return true;
if (type == typeof(DateTime))
return true;
// TODO: add any types for which we offer custom parsing logic
return false;
}
}

View file

@ -0,0 +1,272 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Brutal.Logging;
namespace Brutal.ImGuiApi.Abstractions;
/// <summary>
/// The type of message the terminal interface is outputting
/// </summary>
public enum TerminalInterfaceOutputType
{
/// <summary>
/// An informative message
/// </summary>
Message,
/// <summary>
/// An error with the processing of a command
/// </summary>
Error,
}
/// <summary>
/// Provides an interface which can be used to register terminal commands and handle terminal input.
/// </summary>
public class TerminalInterface
{
public static LogCategory Log = LogCategory.Make("TerminalInterface");
private readonly Dictionary<object, Dictionary<string, Command>> CommandLibrary = new();
private readonly Dictionary<string, Command> StaticCommandLibrary = new();
private readonly ConsoleWindow _consoleWindow;
/// <summary>
/// Invoked with a message when the terminal interface wants to output something
/// </summary>
public event Action<string, TerminalInterfaceOutputType>? OnOutput;
/// <summary>
/// Constructor
/// </summary>
public TerminalInterface(ConsoleWindow consoleWindow)
{
_consoleWindow = consoleWindow;
RegisterCommand(ListCommands);
RegisterCommand(ClearConsole);
}
/// <summary>
/// Executes a batch of terminal input commands.
/// </summary>
/// <param name="inInput">A collection of terminal input strings.</param>
public void Execute(params ReadOnlySpan<string> inInput)
{
foreach (var commandLine in inInput)
Execute(commandLine);
}
/// <summary>
/// Parses terminal input and processes it as commands.
/// </summary>
/// <param name="inInput">The input string from the terminal</param>
public bool Execute(string inInput)
{
// Split full input into individual command statements
foreach (var commandString in Parser.SplitToCommands(inInput))
try
{
ProcessCommand(commandString);
}
catch (TerminalInterfaceException e)
{
OutputProcessCommandError(commandString, e.Message);
return false;
}
return true;
}
private static Type? GetDelegateTypeForMethod(MethodBase inMethodInfo)
{
var parameterTypes = Array.ConvertAll(inMethodInfo.GetParameters(), p => p.ParameterType);
// Direct lookup of the right Action arity. Returns false when no Action can represent the
// signature: more than 16 parameters, or ByRef/pointer parameter types.
return Expression.TryGetActionType(parameterTypes, out var delegateType) ? delegateType : null;
}
/// <summary>
/// Register an object with TerminalAction methods
/// </summary>
/// <param name="inObject">The object we want to register commands from</param>
/// <exception cref="TerminalInterfaceException"></exception>
public void RegisterObject(object inObject)
{
var methods = GetMethodInfos(inObject);
foreach (var method in methods)
{
var delegateType = GetDelegateTypeForMethod(method);
if (delegateType != null)
{
if (method.IsStatic)
RegisterCommand(Delegate.CreateDelegate(delegateType, method), inObject);
else
RegisterCommand(Delegate.CreateDelegate(delegateType, inObject, method), inObject);
}
else
{
throw new TerminalInterfaceException(
$"Error registering command: callback {method.Name} has an unsupported signature"
);
}
}
}
private IEnumerable<MethodInfo> GetMethodInfos(object inObject)
{
const BindingFlags flags =
BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.FlattenHierarchy
| BindingFlags.Static;
var objectType = inObject.GetType();
var allMethods = objectType.GetMethods(flags);
return allMethods.Where(m => m.GetCustomAttribute<TerminalActionAttribute>() != null);
}
public void UnregisterObject(object inObject)
{
if (CommandLibrary.ContainsKey(inObject))
CommandLibrary.Remove(inObject);
}
/// <summary>
/// Registers commands which can be called from the terminal.
/// </summary>
/// <param name="inCallback">The function which the command will call</param>
/// <param name="inObject">Object to which the command delegate is bound to. Null for methods</param>
public void RegisterCommand(Delegate inCallback, object? inObject = null)
{
var attribute = inCallback.Method.GetCustomAttribute<TerminalActionAttribute>();
if (attribute is null)
throw new TerminalInterfaceException(
$"Error registering command:" + $" callback {inCallback.Method.Name} has no TerminalActionAttribute"
);
if (inObject is not null)
{
if (!CommandLibrary.ContainsKey(inObject))
CommandLibrary[inObject] = new Dictionary<string, Command>();
CommandLibrary[inObject][attribute.CommandName] = new Command(
inCallback,
attribute.Description,
attribute.ParsingMode
);
}
else
{
StaticCommandLibrary[attribute.CommandName] = new Command(
inCallback,
attribute.Description,
attribute.ParsingMode
);
}
}
private void ProcessCommand(string inCommandString)
{
// Split command into command name and arguments
Parser.SplitCommand(inCommandString, out var commandName, out var argsString);
if (StaticCommandLibrary.ContainsKey(commandName))
{
var args = Parser.SplitToArgs(argsString, StaticCommandLibrary[commandName].ParsingMode);
// Issue the command
StaticCommandLibrary[commandName].Issue(args);
return;
}
foreach (var (_, commands) in CommandLibrary)
{
if (commands.ContainsKey(commandName))
{
var args = Parser.SplitToArgs(argsString, commands[commandName].ParsingMode);
// Issue the command
commands[commandName].Issue(args);
return;
}
}
Log.Error($"unknown command {commandName}, use 'help' for a list of commands");
}
[TerminalAction("clear", "Clears the console output")]
private void ClearConsole() => _consoleWindow.ClearConsole();
[TerminalAction("help", "Lists all registered commands and their descriptions")]
private void ListCommands()
{
var commandCount = 0;
var tableString = new TableString();
var cmds = new Dictionary<string, Command>();
// commands
foreach (var (command, args) in StaticCommandLibrary)
cmds.Add(command, args);
// instanced commands
foreach (var (_, commands) in CommandLibrary)
foreach (var (command, args) in commands)
cmds.Add(command, args);
foreach (var cmd in cmds)
PrintHelpLine(cmd.Key, cmd.Value);
_consoleWindow.Print($"listing {commandCount} commands");
_consoleWindow.Print(
"Multiple commands can be called in a single statement by separating them with a ';' semicolon"
);
tableString.Sort();
tableString.ToConsole(_consoleWindow);
OnOutput?.Invoke(string.Empty, TerminalInterfaceOutputType.Message);
return;
void PrintHelpLine(string command, Command args)
{
commandCount++;
var definitions = args.GetDefinitions;
if (definitions.Count == 0)
{
tableString.AddRow(command, args.Description);
return;
}
var argSb = new StringBuilder();
for (var i = 0; i < definitions.Count; i++)
{
if (i > 0)
argSb.Append(", ");
var definition = args.GetDefinitions[i];
argSb.Append('<');
var type = definition.Type.ToString();
// remove "System." from the type name
if (type.StartsWith("System."))
type = type[7..];
type = type.ToLowerInvariant();
argSb.Append(type);
argSb.Append('>');
if (definition.HasDefault)
argSb.Append('?');
argSb.Append(' ');
argSb.Append(definition.Name);
}
tableString.AddRow(command, args.Description, argSb.ToString());
}
}
private void OutputProcessCommandError(string inCommand, string inErrorMessage)
{
var message = $"Terminal interface failed to process command: {inCommand}, error: {inErrorMessage}";
OnOutput?.Invoke(message, TerminalInterfaceOutputType.Error);
}
}

View file

@ -0,0 +1,14 @@
namespace Brutal.ImGuiApi.Abstractions;
/// <summary>
/// An exception which is thrown when a terminal command fails to issue
/// </summary>
public class TerminalInterfaceException : Exception
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="message"></param>
public TerminalInterfaceException(string message)
: base(message) { }
}

View file

@ -0,0 +1,90 @@
using System.CommandLine.Parsing;
namespace Brutal.ImGuiApi.Abstractions;
/// <summary>
/// A parsing utility class for the terminal interface
/// </summary>
internal static class Parser
{
private const char CommandDelimiter = ';';
/// <summary>
/// Splits an input string into individual command strings
/// </summary>
/// <param name="inInput">The raw string from terminal input</param>
/// <returns>A list of individual command strings</returns>
public static List<string> SplitToCommands(string inInput)
{
var commands = new List<string>();
var withinQuotes = false;
var startIndex = 0;
void TryAddCommand(string command)
{
var trimmedCommand = command.Trim();
if (trimmedCommand.Length > 0)
commands.Add(trimmedCommand);
}
// Ignore our delimiter if it's part of a quoted string
for (var i = 0; i < inInput.Length; i++)
if (inInput[i] == '\"')
{
withinQuotes = !withinQuotes;
}
else if (inInput[i] == CommandDelimiter && !withinQuotes)
{
// Split input into individual commands by delimiter
TryAddCommand(inInput.Substring(startIndex, i - startIndex));
startIndex = i + 1;
}
// Add the last command if there is one
if (startIndex < inInput.Length)
TryAddCommand(inInput.Substring(startIndex).Trim());
return commands;
}
/// <summary>
/// Splits a command string into a command name and args string
/// </summary>
/// <param name="inCommandString">The command string to split</param>
/// <param name="inCommandName">The command name part of the input string</param>
/// <param name="inArgsString">The args part of the input string</param>
public static void SplitCommand(string inCommandString, out string inCommandName, out string inArgsString)
{
const StringSplitOptions flags = StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries;
// Split at first whitespace
var split = inCommandString.Split(Array.Empty<char>(), 2, flags);
if (split.Length == 0)
throw new TerminalInterfaceException("Command parsing failed, command string was empty.");
inCommandName = split[0].ToLower();
inArgsString = split.Length > 1 ? split[1] : "";
}
/// <summary>
/// Splits an arg string into individual args according to the parsing mode
/// </summary>
/// <param name="inArgsString">The string to split</param>
/// <param name="inArgParseMode">The parsing mode to use</param>
/// <returns>An array of arg strings, or null if the args string was empty</returns>
public static string[]? SplitToArgs(string inArgsString, ArgParseMode inArgParseMode)
{
// If we're using single string parsing mode, return the args as a single string
if (inArgParseMode == ArgParseMode.Unparsed)
// Remove outer whitespace and quotes if they exist
return new[] { inArgsString };
// If there are no args, return null
if (string.IsNullOrEmpty(inArgsString))
return null;
// If we're using default parsing mode, split the args into individual strings
return CommandLineParser.SplitCommandLine(inArgsString).ToArray();
}
}