Exemplo n.º 1
0
Arquivo: Shell.cs Projeto: wangzq/bips
        private void ExecuteRunLoop()
        {
            var autoCompleteProviders = new List <IAutoCompleteProvider>
            {
                new AutoCompleteProviderChain(
                    new PowerShellTabExpansion2AutoCompleteProvider(_commandExecutor),
                    new PowerShellTabExpansionAutoCompleteProvider(_commandExecutor),
                    new CompositeAutoCompleteProvider(
                        new ProviderPathAutoCompleteProvider(_commandExecutor),
                        new CommandListAutoCompleteProvider(_commandExecutor)
                        )
                    )
            };

            _autoCompleteWalker = new AutoCompleteWalker(autoCompleteProviders);
            _historyStackWalker = new HistoryStackWalker(_commandExecutor);

            _consoleWindow.AutoCompleteWalker = _autoCompleteWalker;
            _consoleWindow.HistoryStackWalker = _historyStackWalker;

            WritePrompt();

            while (true)
            {
                WaitHandle[] handles = new[]
                {
                    _host.ExitWaitHandle,
                    _consoleWindow.CommandEnteredEvent,
                    Queue.WaitHandle,
                    _threadStopEvent,
                    _writePromptEvent
                }.ToList().Where(f => f != null).ToArray();

                int index = WaitHandle.WaitAny(handles);
                switch (index)
                {
                case (0):
                    InvokeShouldExit(_host.ExitCode);
                    _host.ResetExitState();
                    break;

                case (1):
                    ExecuteConsoleCommand();
                    break;

                case (2):
                    ExecuteQueuedCommand();
                    break;

                case (3):
                    _runspace.Dispose();
                    _threadStopEvent.Close();
                    _writePromptEvent.Close();
                    return;

                case (4):
                    WritePrompt();
                    _writePromptEvent.Reset();
                    break;

                default:
                    break;
                }
            }
        }
Exemplo n.º 2
0
        private void HandleKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && !(e.Shift || e.Alt))
            {
                HandleControlKeyDown(sender, e);
                return;
            }

            e.Handled = true;
            if (Keys.Escape == e.KeyCode)
            {
                Select(_promptPosition, EndOfLinePosition);
                UpdateCountsAndDeleteSelection();
                FlushInputBuffer();
            }
            else if (Keys.Enter == e.KeyCode)
            {
                NotifyCommandEntered();
            }
            else if (Keys.Tab == e.KeyCode)
            {
                if (String.IsNullOrEmpty(_tabExpansionInput))
                {
                    Select(_promptPosition, SelectionStart);
                    _tabExpansionInput = SelectedText;
                }

                string cmd = String.Empty;
                if (!e.Shift)
                {
                    cmd = AutoCompleteWalker.NextUp(_tabExpansionInput);
                }
                else
                {
                    cmd = AutoCompleteWalker.NextDown(_tabExpansionInput);
                }

                if (String.IsNullOrEmpty(cmd))
                {
                    Select(EndOfLinePosition, EndOfLinePosition);
                    return;
                }

                Select(_promptPosition, EndOfLinePosition);
                InsertText(cmd);
            }
            else if (Keys.PageUp == e.KeyCode)
            {
                string cmd = HistoryStackWalker.Oldest();
                if (String.IsNullOrEmpty(cmd))
                {
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.PageDown == e.KeyCode)
            {
                string cmd = HistoryStackWalker.Newest();
                if (String.IsNullOrEmpty(cmd))
                {
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.Up == e.KeyCode)
            {
                string cmd = HistoryStackWalker.NextUp();
                if (String.IsNullOrEmpty(cmd))
                {
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.Down == e.KeyCode)
            {
                string cmd = HistoryStackWalker.NextDown();
                if (String.IsNullOrEmpty(cmd))
                {
                    MoveCaretEOL(false);
                    return;
                }
                InsertHistoryItem(cmd.Trim());
            }
            else if (Keys.Back == e.KeyCode)
            {
                if (SelectionStart >= _promptPosition)
                {
                    if (SelectionStart != _promptPosition && SelectionLength == 0)
                    {
                        int index = SelectionStart - 1;
                        Select(index, 1);
                    }
                    UpdateCountsAndDeleteSelection();
                }
            }
            else if (Keys.Delete == e.KeyCode)
            {
                if (EndOfLinePosition > SelectionStart)
                {
                    if (SelectionLength == 0)
                    {
                        int index = SelectionStart;
                        Select(index, 1);
                    }
                    UpdateCountsAndDeleteSelection();
                }
            }
            else if (Keys.Home == e.KeyCode)
            {
                MoveCaretHome(e.Shift);
            }
            else if (Keys.End == e.KeyCode)
            {
                MoveCaretEOL(e.Shift);
            }
            else
            {
                e.Handled = false;
            }
        }