// This is called everytime a key is pressed.
public static AttackState CommandProcessor(AttackState attackState)
{
attackState.output = null;
int relativePos = attackState.relativeCursorPos();
int cmdLength = attackState.displayCmd.Length;
/////////////////////////
// BACKSPACE OR DELETE //
/////////////////////////
if (attackState.keyInfo.Key == ConsoleKey.Backspace || attackState.keyInfo.Key == ConsoleKey.Delete)
{
attackState.ClearLoop();
if (attackState.displayCmd != "" && attackState.relativeCursorPos() > 0)
{
if (attackState.keyInfo.Key == ConsoleKey.Backspace)
{
attackState.cursorPos -= 1;
}
List <char> displayCmd = attackState.displayCmd.ToList();
int relativeCursorPos = attackState.relativeCmdCursorPos();
displayCmd.RemoveAt(relativeCursorPos);
attackState.displayCmd = new string(displayCmd.ToArray());
}
}
/////////////////////////
// BACKSPACE OR DELETE //
/////////////////////////
else if (attackState.keyInfo.Key == ConsoleKey.Home || attackState.keyInfo.Key == ConsoleKey.End)
{
if (attackState.keyInfo.Key == ConsoleKey.Home)
{
attackState.cursorPos = attackState.promptLength;
}
else
{
attackState.cursorPos = attackState.promptLength + attackState.displayCmd.Length;
}
}
////////////////
// UP OR DOWN //
////////////////
else if (attackState.keyInfo.Key == ConsoleKey.UpArrow || attackState.keyInfo.Key == ConsoleKey.DownArrow)
{
return(history(attackState));
}
///////////////////
// LEFT OR RIGHT //
///////////////////
// TODO: Fix arrows navigating between wrapped command lines
else if (attackState.keyInfo.Key == ConsoleKey.LeftArrow)
{
if (attackState.relativeCmdCursorPos() > 0)
{
attackState.ClearLoop();
attackState.cursorPos -= 1;
}
return(attackState);
}
else if (attackState.keyInfo.Key == ConsoleKey.RightArrow)
{
if (attackState.relativeCmdCursorPos() < attackState.displayCmd.Length)
{
attackState.ClearLoop();
attackState.cursorPos += 1;
}
return(attackState);
}
///////////
// ENTER //
///////////
else if (attackState.keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine();
attackState.ClearLoop();
attackState.cmd = attackState.displayCmd;
// don't add blank lines to history
if (attackState.cmd != "")
{
attackState.history.Add(attackState.cmd);
}
if (attackState.cmd == "exit")
{
System.Environment.Exit(0);
}
else if (attackState.cmd == "clear")
{
Console.Clear();
attackState.displayCmd = "";
Display.printPrompt(attackState);
}
// TODO: Make this better.
//else if (attackState.cmd.Contains(".exe"))
//{
// attackState.cmd = "Start-Process -NoNewWindow -Wait " + attackState.cmd;
// attackState = Processing.PSExec(attackState);
// Display.Output(attackState);
//}
// assume that we just want to execute whatever makes it here.
else
{
attackState = Processing.PSExec(attackState);
attackState.displayCmd = "";
Display.Output(attackState);
}
// clear out cmd related stuff from state
attackState.ClearIO(display: true);
}
/////////
// TAB //
/////////
else if (attackState.keyInfo.Key == ConsoleKey.Tab)
{
return(TabExpansion.Process(attackState));
}
//////////
// if nothing matched, lets assume its a character and add it to displayCmd
//////////
else
{
attackState.ClearLoop();
// figure out where to insert the typed character
List <char> displayCmd = attackState.displayCmd.ToList();
int relativeCmdCursorPos = attackState.relativeCmdCursorPos();
int cmdInsertPos = attackState.cursorPos - attackState.promptLength;
displayCmd.Insert(attackState.cursorPos - attackState.promptLength, attackState.keyInfo.KeyChar);
attackState.displayCmd = new string(displayCmd.ToArray());
attackState.cursorPos += 1;
}
return(attackState);
}