예제 #1
0
        private static CHAR_INFO[] CreateCompletionMenu(System.Collections.ObjectModel.Collection <CompletionResult> matches, IConsole console, bool showToolTips, string currentCompletionText, out int currentCompletionIndex, out int MenuColumnWidth, out int DisplayRows)
        {
            var minColWidth = matches.Max(c => c.ListItemText.Length);

            minColWidth += 2;
            var bufferWidth    = console.BufferWidth;
            var displayColumns = Math.Max(1, bufferWidth / minColWidth);

            currentCompletionIndex = 0;

            ConsoleBufferBuilder cb;

            if (displayColumns == 1 || showToolTips)
            {
                const string seperator       = "- ";
                var          maxTooltipWidth = bufferWidth - minColWidth - seperator.Length;
                // switch off tooltips if it too short
                if (maxTooltipWidth < 5)
                {
                    minColWidth  = bufferWidth;
                    showToolTips = false;
                }
                DisplayRows = matches.Count;
                cb          = new ConsoleBufferBuilder(DisplayRows * bufferWidth, console);
                for (int index = 0; index < matches.Count; index++)
                {
                    var match = matches[index];
                    if (match.CompletionText.Equals(currentCompletionText))
                    {
                        currentCompletionIndex = index;
                    }
                    var listItemText = ShortenLongCompletions(HandleNewlinesForPossibleCompletions(match.ListItemText), minColWidth);
                    cb.Append(listItemText);
                    var spacesNeeded = minColWidth - listItemText.Length;
                    if (spacesNeeded > 0)
                    {
                        cb.Append(' ', spacesNeeded);
                    }
                    if (showToolTips)
                    {
                        cb.Append(seperator);
                        var toolTip = HandleNewlinesForPossibleCompletions(match.ToolTip);
                        toolTip = toolTip.Length <= maxTooltipWidth
                                      ? toolTip
                                      : toolTip.Substring(0, maxTooltipWidth);
                        cb.Append(toolTip);
                    }

                    // Make sure we always write out exactly 1 buffer width
                    spacesNeeded = (bufferWidth * (index + 1)) - cb.Length;
                    if (spacesNeeded > 0)
                    {
                        cb.Append(' ', spacesNeeded);
                    }
                }
                MenuColumnWidth = bufferWidth;
            }
            else
            {
                DisplayRows = (matches.Count + displayColumns - 1) / displayColumns;
                cb          = new ConsoleBufferBuilder(DisplayRows * bufferWidth, console);
                for (var row = 0; row < DisplayRows; row++)
                {
                    for (var col = 0; col < displayColumns; col++)
                    {
                        var index = row + (DisplayRows * col);
                        if (index >= matches.Count)
                        {
                            break;
                        }
                        var match = matches[index];
                        if (match.CompletionText.Equals(currentCompletionText))
                        {
                            currentCompletionIndex = index;
                        }
                        var item = HandleNewlinesForPossibleCompletions(match.ListItemText);
                        cb.Append(item);
                        cb.Append(' ', minColWidth - item.Length);
                    }

                    // Make sure we always write out exactly 1 buffer width
                    var spacesNeeded = (bufferWidth * (row + 1)) - cb.Length;
                    if (spacesNeeded > 0)
                    {
                        cb.Append(' ', spacesNeeded);
                    }
                }
                MenuColumnWidth = minColWidth;
            }

            return(cb.ToArray());
        }