Files
HellionChat/HellionChat/_Helpers/CompactInputHistoryNavigator.cs
T
JonKazama-Hellion c4c85cf4b8 docs: unify documentation and streamline code comments
- Translated project documentation (LEARNING-JOURNEY, CONTRIBUTORS, AI_DISCLOSURE) to English for better accessibility.
- Standardized internal code documentation by converting XML-doc blocks to standard comment format.
- Cleaned up inline comments and removed redundant versioning metadata across the codebase.
- Refactored non-functional text elements to improve readability and maintain a consistent style.
2026-05-11 00:52:15 +02:00

75 lines
2.2 KiB
C#

using System;
namespace HellionChat._Helpers;
// Extracted history-navigation cursor math from CompactCallback to allow unit
// testing without ImGuiInputTextCallbackData (DeleteChars/InsertChars).
// Buffer mutation stays at the call site; only the cursor/replacement decision lives here.
//
// Index semantics match InputHistoryService:
// index 0 = oldest entry
// index Count-1 = newest entry
// cursor == -1 = not browsing history
//
// replacement == null: caller must NOT touch the buffer (cursor unchanged).
// replacement != null: write it to the buffer (including "" to clear it).
//
// TEST-MIRROR: ../../../Hellion Build test/Ui/CompactInputHistoryNavigatorTests.cs
public static class CompactInputHistoryNavigator
{
public enum Direction
{
Up,
Down,
}
public static (int cursor, string? replacement) Navigate(
Direction direction,
int currentCursor,
string currentBuffer,
Func<int> getCount,
Action<string> push,
Func<int, string?> getByCursor
)
{
ArgumentNullException.ThrowIfNull(getCount);
ArgumentNullException.ThrowIfNull(push);
ArgumentNullException.ThrowIfNull(getByCursor);
var next = currentCursor;
switch (direction)
{
case Direction.Up:
if (currentCursor == -1)
{
// Stash current input so the user can recover it after browsing.
var offset = 0;
if (!string.IsNullOrWhiteSpace(currentBuffer))
{
push(currentBuffer);
offset = 1;
}
next = getCount() - 1 - offset;
}
else if (currentCursor > 0)
next--;
break;
case Direction.Down:
if (currentCursor != -1)
{
next++;
if (next >= getCount())
next = -1;
}
break;
}
if (next == currentCursor)
return (next, null);
return (next, getByCursor(next) ?? string.Empty);
}
}