Пример #1
0
        private void SetPrompt(string prompt)
        {
            var options = new SetPSReadLineOption {
                ExtraPromptLineCount = 0
            };

            if (string.IsNullOrEmpty(prompt))
            {
                options.PromptText = new [] { "" };
                PSConsoleReadLine.SetOptions(options);
                return;
            }

            int i;

            for (i = prompt.Length - 1; i >= 0; i--)
            {
                if (!char.IsWhiteSpace(prompt[i]))
                {
                    break;
                }
            }

            options.PromptText = new [] { prompt.Substring(i) };

            var lineCount = 1 + prompt.Count(c => c == '\n');

            if (lineCount > 1)
            {
                options.ExtraPromptLineCount = lineCount - 1;
            }
            PSConsoleReadLine.SetOptions(options);
            _console.Write(prompt);
        }
Пример #2
0
        public void UselessStuffForBetterCoverage()
        {
            // Useless test to just make sure coverage numbers are better, written
            // in the first way I could think of that doesn't warn about doing something useless.
            var options = new SetPSReadLineOption();
            var getKeyHandlerCommand = new GetKeyHandlerCommand();
            var useless = ((object)options.AddToHistoryHandler ?? options).GetHashCode()
                          + options.EditMode.GetHashCode()
                          + ((object)options.ContinuationPrompt ?? options).GetHashCode()
                          + options.HistoryNoDuplicates.GetHashCode()
                          + options.HistorySearchCursorMovesToEnd.GetHashCode()
                          + options.MaximumHistoryCount.GetHashCode()
                          + options.MaximumKillRingCount.GetHashCode()
                          + options.DingDuration.GetHashCode()
                          + options.DingTone.GetHashCode()
                          + options.BellStyle.GetHashCode()
                          + options.ExtraPromptLineCount.GetHashCode()
                          + options.ShowToolTips.GetHashCode()
                          + options.CompletionQueryItems.GetHashCode()
                          + options.HistorySearchCaseSensitive.GetHashCode()
                          + getKeyHandlerCommand.Bound.GetHashCode()
                          + getKeyHandlerCommand.Unbound.GetHashCode();

            // This assertion just avoids annoying warnings about unused variables.
            Assert.NotEqual(Math.PI, useless);
        }
Пример #3
0
        private void SetOptionsInternal(SetPSReadLineOption options)
        {
            if (options.ContinuationPrompt != null)
            {
                Options.ContinuationPrompt = options.ContinuationPrompt;
            }
            if (options._historyNoDuplicates.HasValue)
            {
                Options.HistoryNoDuplicates = options.HistoryNoDuplicates;
            }
            if (options._historySearchCursorMovesToEnd.HasValue)
            {
                Options.HistorySearchCursorMovesToEnd = options.HistorySearchCursorMovesToEnd;
            }
            if (options._addToHistoryHandlerSpecified)
            {
                Options.AddToHistoryHandler = options.AddToHistoryHandler;
            }
            if (options._commandValidationHandlerSpecified)
            {
                Options.CommandValidationHandler = options.CommandValidationHandler;
            }
            if (options._maximumHistoryCount.HasValue)
            {
                Options.MaximumHistoryCount = options.MaximumHistoryCount;
                if (_history != null)
                {
                    var newHistory = new HistoryQueue <HistoryItem>(Options.MaximumHistoryCount);
                    while (_history.Count > Options.MaximumHistoryCount)
                    {
                        _history.Dequeue();
                    }
                    while (_history.Count > 0)
                    {
                        newHistory.Enqueue(_history.Dequeue());
                    }
                    _history             = newHistory;
                    _currentHistoryIndex = _history.Count;
                }
            }
            if (options._maximumKillRingCount.HasValue)
            {
                Options.MaximumKillRingCount = options.MaximumKillRingCount;
                // TODO - make _killRing smaller
            }
            if (options._editMode.HasValue)
            {
                Options.EditMode = options.EditMode;

                // Switching/resetting modes - clear out chord dispatch table
                _chordDispatchTable.Clear();

                SetDefaultBindings(Options.EditMode);
            }
            if (options._showToolTips.HasValue)
            {
                Options.ShowToolTips = options.ShowToolTips;
            }
            if (options._extraPromptLineCount.HasValue)
            {
                Options.ExtraPromptLineCount = options.ExtraPromptLineCount;
            }
            if (options._dingTone.HasValue)
            {
                Options.DingTone = options.DingTone;
            }
            if (options._dingDuration.HasValue)
            {
                Options.DingDuration = options.DingDuration;
            }
            if (options._bellStyle.HasValue)
            {
                Options.BellStyle = options.BellStyle;
            }
            if (options._completionQueryItems.HasValue)
            {
                Options.CompletionQueryItems = options.CompletionQueryItems;
            }
            if (options.WordDelimiters != null)
            {
                Options.WordDelimiters = options.WordDelimiters;
            }
            if (options._historySearchCaseSensitive.HasValue)
            {
                Options.HistorySearchCaseSensitive = options.HistorySearchCaseSensitive;
            }
            if (options._historySaveStyle.HasValue)
            {
                Options.HistorySaveStyle = options.HistorySaveStyle;
            }
            if (options._viModeIndicator.HasValue)
            {
                Options.ViModeIndicator = options.ViModeIndicator;
            }
            if (options.ViModeChangeHandler != null)
            {
                if (Options.ViModeIndicator != ViModeStyle.Script)
                {
                    throw new ParameterBindingException("ViModeChangeHandler option requires ViModeStyle.Script");
                }
                Options.ViModeChangeHandler = options.ViModeChangeHandler;
            }
            if (options.HistorySavePath != null)
            {
                Options.HistorySavePath = options.HistorySavePath;
                _historyFileMutex?.Dispose();
                _historyFileMutex         = new Mutex(false, GetHistorySaveFileMutexName());
                _historyFileLastSavedSize = 0;
            }
            if (options._ansiEscapeTimeout.HasValue)
            {
                Options.AnsiEscapeTimeout = options.AnsiEscapeTimeout;
            }
            if (options.PromptText != null)
            {
                Options.PromptText = options.PromptText;
            }
            if (options.Colors != null)
            {
                IDictionaryEnumerator e = options.Colors.GetEnumerator();
                while (e.MoveNext())
                {
                    if (e.Key is string property)
                    {
                        Options.SetColor(property, e.Value);
                    }
                }
            }
        }
Пример #4
0
 /// <summary>
 /// Helper function for the Set-PSReadLineOption cmdlet.
 /// </summary>
 public static void SetOptions(SetPSReadLineOption options)
 {
     _singleton.SetOptionsInternal(options);
 }
Пример #5
0
        private void TestSetup(KeyMode keyMode, params KeyHandler[] keyHandlers)
        {
            Skip.If(WindowsConsoleFixtureHelper.GetKeyboardLayout() != this.Fixture.Lang,
                    $"Keyboard layout must be set to {this.Fixture.Lang}");

            _console       = new TestConsole(_);
            _mockedMethods = new MockedMethods();
            var instance = (PSConsoleReadLine)typeof(PSConsoleReadLine)
                           .GetField("_singleton", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

            typeof(PSConsoleReadLine)
            .GetField("_mockableMethods", BindingFlags.Instance | BindingFlags.NonPublic)
            .SetValue(instance, _mockedMethods);
            typeof(PSConsoleReadLine)
            .GetField("_console", BindingFlags.Instance | BindingFlags.NonPublic)
            .SetValue(instance, _console);

            PSConsoleReadLine.ClearHistory();
            PSConsoleReadLine.ClearKillRing();

            var options = new SetPSReadLineOption
            {
                AddToHistoryHandler           = PSConsoleReadLineOptions.DefaultAddToHistoryHandler,
                AnsiEscapeTimeout             = 0,
                BellStyle                     = PSConsoleReadLineOptions.DefaultBellStyle,
                CompletionQueryItems          = PSConsoleReadLineOptions.DefaultCompletionQueryItems,
                ContinuationPrompt            = PSConsoleReadLineOptions.DefaultContinuationPrompt,
                DingDuration                  = 1,      // Make tests virtually silent when they ding
                DingTone                      = 37,     // Make tests virtually silent when they ding
                ExtraPromptLineCount          = PSConsoleReadLineOptions.DefaultExtraPromptLineCount,
                HistoryNoDuplicates           = PSConsoleReadLineOptions.DefaultHistoryNoDuplicates,
                HistorySaveStyle              = HistorySaveStyle.SaveNothing,
                HistorySearchCaseSensitive    = PSConsoleReadLineOptions.DefaultHistorySearchCaseSensitive,
                HistorySearchCursorMovesToEnd = PSConsoleReadLineOptions.DefaultHistorySearchCursorMovesToEnd,
                MaximumHistoryCount           = PSConsoleReadLineOptions.DefaultMaximumHistoryCount,
                MaximumKillRingCount          = PSConsoleReadLineOptions.DefaultMaximumKillRingCount,
                ShowToolTips                  = PSConsoleReadLineOptions.DefaultShowToolTips,
                WordDelimiters                = PSConsoleReadLineOptions.DefaultWordDelimiters,
                PromptText                    = new [] { "" },
                Colors = new Hashtable {
                    { "ContinuationPrompt", MakeCombinedColor(_console.ForegroundColor, _console.BackgroundColor) },
                    { "Emphasis", MakeCombinedColor(PSConsoleReadLineOptions.DefaultEmphasisColor, _console.BackgroundColor) },
                    { "Error", MakeCombinedColor(ConsoleColor.Red, ConsoleColor.DarkRed) },
                }
            };

            switch (keyMode)
            {
            case KeyMode.Cmd:
                options.EditMode = EditMode.Windows;
                break;

            case KeyMode.Emacs:
                options.EditMode = EditMode.Emacs;
                break;

            case KeyMode.Vi:
                options.EditMode = EditMode.Vi;
                break;
            }

            PSConsoleReadLine.SetOptions(options);

            foreach (var keyHandler in keyHandlers)
            {
                PSConsoleReadLine.SetKeyHandler(new [] { keyHandler.Chord }, keyHandler.Handler, "", "");
            }

            var tokenTypes = new[]
            {
                "Default", "Comment", "Keyword", "String", "Operator", "Variable",
                "Command", "Parameter", "Type", "Number", "Member", "Selection",
                "InlinePrediction", "ListPrediction", "ListPredictionSelected"
            };
            var colors = new Hashtable();

            for (var i = 0; i < tokenTypes.Length; i++)
            {
                colors.Add(tokenTypes[i], MakeCombinedColor(Colors[i], BackgroundColors[i]));
            }
            var colorOptions = new SetPSReadLineOption {
                Colors = colors
            };

            PSConsoleReadLine.SetOptions(colorOptions);
        }
Пример #6
0
        public void MultiLine_ScreenCheck()
        {
            TestSetup(KeyMode.Cmd);

            var defaultContinuationPrompt       = PSConsoleReadLineOptions.DefaultContinuationPrompt;
            var defaultContinuationPromptColors = Tuple.Create(_console.ForegroundColor, _console.BackgroundColor);

            Test("@'\n\n hello\n\n world\n'@",
                 Keys(
                     "@'", _.Enter, _.Enter,
                     " hello", _.Enter, _.Enter,
                     " world", _.Enter, "'@",
                     CheckThat(() => AssertScreenIs(6,
                                                    TokenClassification.String, "@'",
                                                    NextLine,
                                                    defaultContinuationPromptColors, defaultContinuationPrompt,
                                                    NextLine,
                                                    defaultContinuationPromptColors, defaultContinuationPrompt,
                                                    TokenClassification.String, " hello",
                                                    NextLine,
                                                    defaultContinuationPromptColors, defaultContinuationPrompt,
                                                    NextLine,
                                                    defaultContinuationPromptColors, defaultContinuationPrompt,
                                                    TokenClassification.String, " world",
                                                    NextLine,
                                                    defaultContinuationPromptColors, defaultContinuationPrompt,
                                                    TokenClassification.String, "'@"))
                     ));

            // Set the continuation prompt to be an empty string.
            var setOption = new SetPSReadLineOption {
                ContinuationPrompt = string.Empty
            };

            PSConsoleReadLine.SetOptions(setOption);

            try
            {
                Test("@'\n\n hello\n\n world\n'@",
                     Keys(
                         "@'", _.Enter, _.Enter,
                         " hello", _.Enter, _.Enter,
                         " world", _.Enter, "'@",
                         CheckThat(() => AssertScreenIs(6,
                                                        TokenClassification.String, "@'",
                                                        NextLine,
                                                        NextLine,
                                                        TokenClassification.String, " hello",
                                                        NextLine,
                                                        NextLine,
                                                        TokenClassification.String, " world",
                                                        NextLine,
                                                        TokenClassification.String, "'@"))
                         ));
            }
            finally
            {
                // Restore to the default continuation prompt.
                setOption.ContinuationPrompt = defaultContinuationPrompt;
                PSConsoleReadLine.SetOptions(setOption);
            }
        }
Пример #7
0
        private void SetOptionsInternal(SetPSReadLineOption options)
        {
            if (options.ContinuationPrompt != null)
            {
                Options.ContinuationPrompt = options.ContinuationPrompt;
            }
            if (options._historyNoDuplicates.HasValue)
            {
                Options.HistoryNoDuplicates = options.HistoryNoDuplicates;
            }
            if (options._historySearchCursorMovesToEnd.HasValue)
            {
                Options.HistorySearchCursorMovesToEnd = options.HistorySearchCursorMovesToEnd;
            }
            if (options._addToHistoryHandlerSpecified)
            {
                Options.AddToHistoryHandler = options.AddToHistoryHandler;
            }
            if (options._commandValidationHandlerSpecified)
            {
                Options.CommandValidationHandler = options.CommandValidationHandler;
            }
            if (options._maximumHistoryCount.HasValue)
            {
                Options.MaximumHistoryCount = options.MaximumHistoryCount;
                if (_history != null)
                {
                    var newHistory = new HistoryQueue <HistoryItem>(Options.MaximumHistoryCount);
                    while (_history.Count > Options.MaximumHistoryCount)
                    {
                        _history.Dequeue();
                    }
                    while (_history.Count > 0)
                    {
                        newHistory.Enqueue(_history.Dequeue());
                    }
                    _history             = newHistory;
                    _currentHistoryIndex = _history.Count;
                }
            }
            if (options._maximumKillRingCount.HasValue)
            {
                Options.MaximumKillRingCount = options.MaximumKillRingCount;
                // TODO - make _killRing smaller
            }
            if (options._editMode.HasValue)
            {
                Options.EditMode = options.EditMode;

                // Switching/resetting modes - clear out chord dispatch table
                _chordDispatchTable.Clear();

                SetDefaultBindings(Options.EditMode);
            }
            if (options._showToolTips.HasValue)
            {
                Options.ShowToolTips = options.ShowToolTips;
            }
            if (options._extraPromptLineCount.HasValue)
            {
                Options.ExtraPromptLineCount = options.ExtraPromptLineCount;
            }
            if (options._dingTone.HasValue)
            {
                Options.DingTone = options.DingTone;
            }
            if (options._dingDuration.HasValue)
            {
                Options.DingDuration = options.DingDuration;
            }
            if (options._bellStyle.HasValue)
            {
                Options.BellStyle = options.BellStyle;
            }
            if (options._completionQueryItems.HasValue)
            {
                Options.CompletionQueryItems = options.CompletionQueryItems;
            }
            if (options.WordDelimiters != null)
            {
                Options.WordDelimiters = options.WordDelimiters;
            }
            if (options._historySearchCaseSensitive.HasValue)
            {
                Options.HistorySearchCaseSensitive = options.HistorySearchCaseSensitive;
            }
            if (options._historySaveStyle.HasValue)
            {
                Options.HistorySaveStyle = options.HistorySaveStyle;
            }
            if (options._viModeIndicator.HasValue)
            {
                Options.ViModeIndicator = options.ViModeIndicator;
            }
            if (options.ViModeChangeHandler != null)
            {
                if (Options.ViModeIndicator != ViModeStyle.Script)
                {
                    throw new ParameterBindingException("ViModeChangeHandler option requires ViModeStyle.Script");
                }
                Options.ViModeChangeHandler = options.ViModeChangeHandler;
            }
            if (options.HistorySavePath != null)
            {
                Options.HistorySavePath = options.HistorySavePath;
                _historyFileMutex?.Dispose();
                _historyFileMutex         = new Mutex(false, GetHistorySaveFileMutexName());
                _historyFileLastSavedSize = 0;
            }
            if (options._ansiEscapeTimeout.HasValue)
            {
                Options.AnsiEscapeTimeout = options.AnsiEscapeTimeout;
            }
            if (options.PromptText != null)
            {
                Options.PromptText = options.PromptText;
            }
            if (options._predictionSource.HasValue)
            {
                if (_console is PlatformWindows.LegacyWin32Console && options.PredictionSource != PredictionSource.None)
                {
                    throw new ArgumentException(PSReadLineResources.PredictiveSuggestionNotSupported);
                }

                bool notTest = ReferenceEquals(_mockableMethods, this);
                if ((options.PredictionSource & PredictionSource.Plugin) != 0 && Environment.Version.Major < 5 && notTest)
                {
                    throw new ArgumentException(PSReadLineResources.PredictionPluginNotSupported);
                }

                Options.PredictionSource = options.PredictionSource;
            }
            if (options._predictionViewStyle.HasValue)
            {
                WarnWhenWindowSizeTooSmallForView(options.PredictionViewStyle, options);
                Options.PredictionViewStyle = options.PredictionViewStyle;
                _prediction.SetViewStyle(options.PredictionViewStyle);
            }
            if (options.Colors != null)
            {
                IDictionaryEnumerator e = options.Colors.GetEnumerator();
                while (e.MoveNext())
                {
                    if (e.Key is string property)
                    {
                        Options.SetColor(property, e.Value);
                    }
                }
            }
        }