Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        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));
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        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);
            }
        }