Example #1
0
        private void RenderSearchChanges(SearchHistory?searchHistory = null)
        {
            var totalItems  = !searchHistory.HasValue ? shell.History.Count : searchHistory.Value.SearchResults.Count;
            var currentItem = !searchHistory.HasValue ? 0 : searchHistory.Value.SelectedItem + 1;
            var match       = !searchHistory.HasValue || searchHistory.Value.SearchResults.Count == 0 ? string.Empty : searchHistory.Value.SearchResults[searchHistory.Value.SelectedItem];

            var minimalPrompt = "[" + currentItem + "/" + totalItems + "]: ";

            var matchedEntryMaxLength = implementation.WindowWidth - minimalPrompt.Length;

            if (match.Length > matchedEntryMaxLength)
            {
                match = match.Substring(0, matchedEntryMaxLength);
            }

            ColorString highlightedLine = string.Empty;

            if (searchHistory.HasValue && !string.IsNullOrWhiteSpace(match))
            {
                var notMatchedString  = new StringBuilder();
                var matchingPositions = FindAllIndexesOf(match.ToLowerInvariant(), searchHistory.Value.Term.ToLowerInvariant());
                for (int posInStr = 0; searchHistory != null && posInStr < match.Length; posInStr++)
                {
                    if (matchingPositions.Contains(posInStr))
                    {
                        highlightedLine += notMatchedString.ToString() + new ColorString(searchHistory.Value.Term, Color.Green);
                        notMatchedString.Clear();
                        posInStr += searchHistory.Value.Term.Length - 1;
                    }
                    else
                    {
                        notMatchedString.Append(match[posInStr]);
                    }
                }

                highlightedLine += notMatchedString.ToString();
            }

            // need to use the implementation functions as we are writing off the current line
            var pos = implementation.CursorLeft;

            implementation.CursorVisible = false;
            implementation.WriteLine();
            implementation.Write(new string(' ', implementation.WindowWidth - 1));
            implementation.CursorLeft = 0;

            implementation.Write(minimalPrompt);

            // our write function sets both implementation.CursorLeft and CursorPosition
            // we need to restore these to ensure we dont go out of sync
            var cpOld = ci.CursorPosition;

            ci.CursorPosition = minimalPrompt.Length;
            ci.Write(highlightedLine);
            ci.CursorPosition = cpOld;

            implementation.CursorTop--;
            implementation.CursorLeft    = pos;
            implementation.CursorVisible = true;
        }
        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());
            }
        }