console buffer history

This commit is contained in:
2022-03-29 20:04:47 +03:00
parent 3ee7dddfd3
commit 91ee2f061f
2 changed files with 47 additions and 5 deletions

View File

@@ -80,6 +80,8 @@ namespace Cabrito
public static IReadOnlyCollection<ConsoleLine> Lines => instance.Lines;
public static string GetBufferHistory(int index) => instance.GetBufferHistory(index);
// Echoes text to Console
public static void Print(string text) => instance.Print(text);
@@ -104,7 +106,7 @@ namespace Cabrito
// Clears the content of the Console
public static void Clear() => instance.Clear();
public static void Execute(string str) => instance.Execute(str);
public static void Execute(string str, bool bufferInput = false) => instance.Execute(str, bufferInput);
public static string GetVariable(string variableName) => instance.GetVariable(variableName);
}
@@ -132,6 +134,7 @@ namespace Cabrito
private List<ConsoleLine> consoleLines = new List<ConsoleLine>();
private Dictionary<string, ConsoleCommand> consoleCommands = new Dictionary<string, ConsoleCommand>();
private Dictionary<string, ConsoleVariable> consoleVariables = new Dictionary<string, ConsoleVariable>();
private List<string> consoleBufferHistory = new List<string>();
internal ConsoleInstance()
{
@@ -288,6 +291,17 @@ namespace Cabrito
get => consoleLines.AsReadOnly();
}
public string GetBufferHistory(int index)
{
if (consoleBufferHistory.Count == 0)
return null;
if (index > consoleBufferHistory.Count - 1 || index < 0)
return null;
return consoleBufferHistory[index];
}
// Echoes text to Console
public void Print(string text)
{
@@ -368,8 +382,11 @@ namespace Cabrito
consoleLines.Clear();
}
public void Execute(string str)
public void Execute(string str, bool bufferInput = false)
{
if (bufferInput)
consoleBufferHistory.Insert(0, str);
str = str.Trim();
if (ShowExecutedLines)

View File

@@ -75,6 +75,7 @@ namespace Cabrito
return base.OnCharInput(c);
}
private int inputHistoryIndex = -1;
public override bool OnKeyDown(KeyboardKeys key)
{
bool shiftDown = Root.GetKey(KeyboardKeys.Shift);
@@ -95,7 +96,8 @@ namespace Cabrito
{
try
{
Console.Execute(Text);
Console.Execute(Text, true);
inputHistoryIndex = -1;
}
finally
{
@@ -105,9 +107,32 @@ namespace Cabrito
contentBox.ScrollOffset = 0;
return true;
}
else if (key == KeyboardKeys.ArrowUp || key == KeyboardKeys.ArrowDown)
else if (key == KeyboardKeys.ArrowUp)
{
// TODO: implement input history
inputHistoryIndex++;
string line = Console.GetBufferHistory(inputHistoryIndex);
if (line == null)
{
inputHistoryIndex--;
return true;
}
SetText(line);
SetSelection(TextLength);
return true;
}
else if (key == KeyboardKeys.ArrowDown)
{
if (inputHistoryIndex > 0)
inputHistoryIndex--;
string line = Console.GetBufferHistory(inputHistoryIndex);
if (line == null)
return true;
SetText(line);
SetSelection(TextLength);
return true;
}
else if (key == KeyboardKeys.PageUp || key == KeyboardKeys.PageDown)