Exemple #1
0
        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);
        }
Exemple #2
0
        private Task <bool> OnChangeSearchEntryAsync(ConsoleImproved prompt, ConsoleKeyEx key)
        {
            return(Task.Run(() =>
            {
                if (ci.Tag is SearchHistory results)
                {
                    if (key.Key == ConsoleKey.DownArrow)
                    {
                        if (results.SelectedItem < results.SearchResults.Count - 1)
                        {
                            results.SelectedItem++;
                        }
                    }
                    else
                    {
                        if (results.SelectedItem > 0)
                        {
                            results.SelectedItem--;
                        }
                    }

                    RenderSearchChanges(results);
                    ci.Tag = results;
                }

                return false;
            }));
        }
Exemple #3
0
        private Task <bool> OnSearchTextEnteredAsync(ConsoleImproved prompt, ConsoleKeyEx key)
        {
            return(Task.Run(() =>
            {
                if (key.Key.Value == ConsoleKey.UpArrow || key.Key.Value == ConsoleKey.DownArrow)
                {
                    return false;
                }

                var text = ci.UserEnteredText.ToString();

                var primaryResults = shell.History.Where(x => x.CmdLine.Contains(text));
                var secondaryResults = shell.History.Where(x => x.CmdLine.Contains(text.Trim()));

                var search = new SearchHistory()
                {
                    SearchResults = primaryResults.Union(secondaryResults).Select(x => x.CmdLine).Distinct().ToList(),
                    SelectedItem = 0,
                    Term = text
                };
                RenderSearchChanges(search);
                ci.Tag = search;

                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);
            }
        }
Exemple #6
0
        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 void Write()
        {
            using (var ms = new MemoryStream())
            {
                var fakeShell   = new Shell();
                var mockConsole = new MockConsole();
                var console     = new ConsoleImproved(mockConsole, fakeShell);

                console.Write("hello");
                console.WriteLine("world");

                Assert.AreEqual("helloworld\n", mockConsole.Output.ToString());
            }
        }
        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);
            }
        }
Exemple #9
0
        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);
        }
Exemple #10
0
        private Task <bool> OnSelectSearchEntryAsync(ConsoleImproved prompt, ConsoleKeyEx key)
        {
            return(Task.Run(() =>
            {
                if (ci.Tag is SearchHistory search)
                {
                    if (search.SearchResults.Any())
                    {
                        ci.UserEnteredText.Clear();
                        ci.UserEnteredText.Append(search.SearchResults[search.SelectedItem]);
                    }
                }

                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));
            }
        }
        public async Task AddKeyOverrideAsync()
        {
            using (var ms = new MemoryStream())
            {
                var fakeShell   = new Shell();
                var mockConsole = new MockConsole();
                var console     = new ConsoleImproved(mockConsole, fakeShell);

                int handlerCalled = 0;
                console.AddKeyOverride(new ConsoleKeyEx(ConsoleKey.Tab), (a, b) => { handlerCalled++; return(Task <bool> .FromResult(false)); });

                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.A));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Tab));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Enter));

                var key = await console.GetCommandAsync(CancellationToken.None);

                Assert.AreEqual("a", key);
                Assert.AreEqual(1, handlerCalled);
            }
        }
        public async Task CommandOverrideAsync()
        {
            using (var ms = new MemoryStream())
            {
                var fakeShell = new Shell();
                fakeShell.CommandHandlers.Add((cmd) => { Assert.AreEqual("aaaaa", cmd); return("TEST"); });

                var mockConsole = new MockConsole();
                var console     = new ConsoleImproved(mockConsole, fakeShell);

                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.A));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.A));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.A));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.A));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.A));
                mockConsole.keys.Enqueue(new ConsoleKeyEx(ConsoleKey.Enter));

                var command = await console.GetCommandAsync(CancellationToken.None);

                Assert.AreEqual("TEST", command);
            }
        }