Skip to content

Commit

Permalink
Tab complete compatibilty fix with non-interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander authored and Alexander committed Mar 20, 2018
1 parent 389f62f commit f7bc6a8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
38 changes: 29 additions & 9 deletions Tokenvator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ static void Main(string[] args)
{
using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(memeoryStream))
{
streamWriter.Write(String.Join(" ", args));
streamWriter.Flush();
using (System.IO.StreamReader streamReader = new System.IO.StreamReader(memeoryStream))
{
memeoryStream.Seek(0, System.IO.SeekOrigin.Begin);
Console.SetIn(streamReader);
String[] commands = String.Join(" ", args).Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
Int32 offset = 0;
foreach (String command in commands)
{
streamWriter.Write(command.Trim());
streamWriter.Flush();

memeoryStream.Seek(offset, System.IO.SeekOrigin.Begin);
Console.SetIn(streamReader);

new MainLoop().Run();
new MainLoop(false).Run();
offset += command.Trim().Length;
}
}
}
}
return;
}

MainLoop mainLoop = new MainLoop();
MainLoop mainLoop = new MainLoop(true);
while (true)
{
mainLoop.Run();
Expand Down Expand Up @@ -74,18 +81,31 @@ class MainLoop
private String command;

private TabComplete console;
private Boolean activateTabs;

public MainLoop()
public MainLoop(Boolean activateTabs)
{
console = new TabComplete(context, options);
this.activateTabs = activateTabs;
if (activateTabs)
{
console = new TabComplete(context, options);
}
}

internal void Run()
{
try
{
Console.Write(context);
String input = console.ReadLine();//Console.ReadLine();
String input;
if (activateTabs)
{
input = console.ReadLine();
}
else
{
input = Console.ReadLine();
}

switch (NextItem(ref input))
{
Expand Down
49 changes: 11 additions & 38 deletions Tokenvator/Resources/TabComplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TabComplete
private List<String> namespaces;

private List<String> scrollback = new List<string>();
private Int32 scollbackPosition = 0;
private Int32 scrollbackPosition = 0;

private List<String> options = new List<String>();

Expand Down Expand Up @@ -62,43 +62,17 @@ public String ReadLine()
}
break;
case ConsoleKey.UpArrow:
try
if (scrollbackPosition > 0 && scrollback.Count > 0)
{
if (scollbackPosition > 0)
{
scollbackPosition--;
stringBuilder.Remove(0, stringBuilder.Length);
stringBuilder.Append(scrollback[scollbackPosition]);
ResetLine();
Console.Write(stringBuilder.ToString());
}
}
catch (Exception)
{
if (scollbackPosition < scrollback.Count)
{
scollbackPosition++;
}
stringBuilder.Remove(0, stringBuilder.Length);
stringBuilder.Append(scrollback[scrollbackPosition--]);
}
break;
case ConsoleKey.DownArrow:
try
if (scrollbackPosition + 1 < scrollback.Count)
{
if (scollbackPosition < scrollback.Count)
{
scollbackPosition++;
stringBuilder.Remove(0, stringBuilder.Length);
stringBuilder.Append(scrollback[scollbackPosition]);
ResetLine();
Console.Write(stringBuilder.ToString());
}
}
catch (Exception)
{
if (scollbackPosition > 0)
{
scollbackPosition--;
}
stringBuilder.Remove(0, stringBuilder.Length);
stringBuilder.Append(scrollback[scrollbackPosition++]);
}
break;
case ConsoleKey.LeftArrow:
Expand Down Expand Up @@ -150,12 +124,11 @@ private void TabInput(StringBuilder stringBuilder, Boolean doubleTab)
////////////////////////////////////////////////////////////////////////////////
private void ResetLine()
{
Int32 line = Console.CursorTop;
Console.SetCursorPosition(context.Length, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth - context.Length));
Console.SetCursorPosition(0, line);
//Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(context);
Console.SetCursorPosition(context.Length, line);
Console.SetCursorPosition(context.Length, Console.CursorTop);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -184,7 +157,7 @@ private void KeyInput(StringBuilder stringBuilder, ConsoleKeyInfo keyDown)
}
else
{
var key = keyDown.KeyChar;
Char key = keyDown.KeyChar;
stringBuilder.Append(key);
}
}
Expand Down

0 comments on commit f7bc6a8

Please sign in to comment.