private static async Task <bool> OnSearchHistoryTmuxModeAsync(ConsoleImproved prompt, ConsoleKeyEx key) { ProcessEx tmuxPopup = null; var result = HistoryAPI.ListenForSearchResultAsync((port, token) => { var cssCommand = string.Format(Settings.Default.HistoryPopupCommand, API.Shell.AssemblyLocation, port, token); var tmuxCommand = string.Format(Settings.Default.PopupCommand, cssCommand); // start tmux prompt tmuxPopup = OS.Exec(tmuxCommand); }); await tmuxPopup.WaitForExitAsync(); tmuxPopup.Dispose(); await Task.WhenAny(result, Task.Delay(1000)); if (result.IsCompletedSuccessfully) { prompt.DisplayPrompt(await result); } else { prompt.DisplayPrompt("Error"); } return(false); }
public async Task MultiLineBackspaceAsync() { using (var ms = new MemoryStream()) { var fakeShell = new Shell(); fakeShell.CommandHandlers.Add((cmd) => { Assert.IsTrue(string.IsNullOrWhiteSpace(cmd)); return(cmd); }); fakeShell.Prompt = () => { return(new ColorString("# > ", "# > ")); }; var mockConsole = new MockConsole() { WindowWidth = 12, }; var console = new ConsoleImproved(mockConsole, fakeShell); int lastPos = 0; console.AddKeyOverride(ConsoleKeyEx.Any, (console, key) => { if (key.Key == ConsoleKey.Backspace) { // This makes sure that after every character the position variable is correctly incremented by 1 lastPos--; Assert.AreEqual(console.UserEnteredText.Length, console.UserEnteredTextPosition); Assert.AreEqual(lastPos, console.UserEnteredTextPosition); } else if (key.Key != ConsoleKey.Enter) { lastPos++; } return(Task <bool> .FromResult(false)); }); console.DisplayPrompt(); foreach (var character in new[] { ConsoleKey.A, ConsoleKey.B, ConsoleKey.C, ConsoleKey.D, ConsoleKey.E, ConsoleKey.F, ConsoleKey.G }) { for (int x = 0; x < 3; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(character)); } } foreach (var character in new[] { ConsoleKey.A, ConsoleKey.B, ConsoleKey.C, ConsoleKey.D, ConsoleKey.E, ConsoleKey.F, ConsoleKey.G }) { for (int x = 0; x < 3; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Backspace)); } } mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Enter)); var command = await console.GetCommandAsync(CancellationToken.None); Assert.IsTrue(string.IsNullOrWhiteSpace(command)); } }
public async Task MultiLineArrowKeysAsync() { using (var ms = new MemoryStream()) { var input = "aaabbbcccdddeeefffggg"; var fakeShell = new Shell(); fakeShell.CommandHandlers.Add((cmd) => { Assert.AreEqual(input, cmd); return(cmd); }); fakeShell.Prompt = () => { return(new ColorString("# > ", "# > ")); }; var mockConsole = new MockConsole() { WindowWidth = 12, }; var console = new ConsoleImproved(mockConsole, fakeShell); int pos = 0; console.AddKeyOverride(ConsoleKeyEx.Any, (console, key) => { if (key.Key == ConsoleKey.LeftArrow) { pos--; } else if (key.Key == ConsoleKey.RightArrow || key.Key != ConsoleKey.Enter) { pos++; } Assert.AreEqual(pos, console.UserEnteredTextPosition); return(Task <bool> .FromResult(false)); }); console.DisplayPrompt(); foreach (var character in new[] { ConsoleKey.A, ConsoleKey.B, ConsoleKey.C, ConsoleKey.D, ConsoleKey.E, ConsoleKey.F, ConsoleKey.G }) { for (int x = 0; x < 3; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(character)); } } for (int x = 0; x < input.Length; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.LeftArrow)); } for (int x = 0; x < input.Length; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.RightArrow)); } mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Enter)); var command = await console.GetCommandAsync(CancellationToken.None); } }
private async Task <bool> OnSearchHistoryAltModeAsync(ConsoleImproved prompt, ConsoleKeyEx key) { await console.SaveAsync(); var command = await this.RunInterfaceAsync(prompt.Shell.History); await console.RestoreAsync(); prompt.DisplayPrompt(command); return(false); }
public async Task MultiLineInsertAsync() { using (var ms = new MemoryStream()) { var input = "aaabbbcccxxxdddyyyeeefffggg"; var fakeShell = new Shell(); fakeShell.CommandHandlers.Add((cmd) => { Assert.AreEqual(input, cmd); return(cmd); }); fakeShell.Prompt = () => { return(new ColorString("# > ", "# > ")); }; var mockConsole = new MockConsole() { WindowWidth = 12, }; var console = new ConsoleImproved(mockConsole, fakeShell); console.DisplayPrompt(); foreach (var character in new[] { ConsoleKey.A, ConsoleKey.B, ConsoleKey.C, ConsoleKey.D, ConsoleKey.E, ConsoleKey.F, ConsoleKey.G }) { for (int x = 0; x < 3; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(character)); } } //aaabbbcccdddeeefffggg for (int x = 0; x < 12; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.LeftArrow)); } mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.X)); mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.X)); mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.X)); for (int x = 0; x < 3; x++) { mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.RightArrow)); } mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Y)); mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Y)); mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Y)); mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Enter)); var command = await console.GetCommandAsync(CancellationToken.None); Assert.AreEqual(input, command); } }
private async Task <bool> OnSearchHistoryAsync(ConsoleImproved prompt, ConsoleKeyEx key) { int oldPos = implementation.CursorTop; implementation.CursorLeft = 0; implementation.CursorTop = implementation.WindowHeight - 2; // create fake shell object with a custom prompt var fakeShell = new Dotnet.Shell.API.Shell(); fakeShell.History.AddRange(shell.History); fakeShell.Prompt = () => { RenderSearchChanges(); // Search) user search text // [1/3]: matched entry return("Search) "); }; ci = new ConsoleImproved(implementation, fakeShell); ci.KeyOverrides.Where(x => x.Key.Key == ConsoleKey.UpArrow).ToList().ForEach(x => ci.KeyOverrides.Remove(x)); ci.KeyOverrides.Where(x => x.Key.Key == ConsoleKey.DownArrow).ToList().ForEach(x => ci.KeyOverrides.Remove(x)); ci.AddKeyOverride(ConsoleKeyEx.Any, OnSearchTextEnteredAsync); ci.AddKeyOverride(new ConsoleKeyEx(ConsoleKey.UpArrow), OnChangeSearchEntryAsync); ci.AddKeyOverride(new ConsoleKeyEx(ConsoleKey.DownArrow), OnChangeSearchEntryAsync); ci.AddKeyOverride(new ConsoleKeyEx(ConsoleKey.Enter), OnSelectSearchEntryAsync); ci.DisplayPrompt(); // When the prompt returns, instead of executing the command we just set that // as what to show on screen var command = await ci.GetCommandAsync(); implementation.CursorTop = implementation.WindowHeight - 2; implementation.Write(new string(' ', implementation.WindowWidth)); implementation.CursorTop = implementation.WindowHeight - 1; implementation.Write(new string(' ', implementation.WindowWidth)); implementation.CursorTop = oldPos; implementation.Write(new string(' ', implementation.WindowWidth)); prompt.DisplayPrompt(command); return(false); }
public void Prompt() { const string Prompt = "test>"; using (var ms = new MemoryStream()) { var fakeShell = new Shell { Prompt = () => { return(Prompt); } }; var mockConsole = new MockConsole(); var console = new ConsoleImproved(mockConsole, fakeShell); console.DisplayPrompt(); Assert.IsTrue(mockConsole.Output.ToString().StartsWith(Prompt)); } }