6
6
using System . Management . Automation ;
7
7
using System . Management . Automation . Language ;
8
8
using System . Text ;
9
+ using System . Text . RegularExpressions ;
9
10
10
11
namespace PSConsoleUtilities
11
12
{
@@ -1113,16 +1114,32 @@ public static void PossibleCompletions(ConsoleKeyInfo? key = null, object arg =
1113
1114
var coords = _singleton . ConvertOffsetToCoordinates ( _singleton . _buffer . Length ) ;
1114
1115
_singleton . PlaceCursor ( 0 , coords . Y + 1 ) ;
1115
1116
1117
+ if ( completions . CompletionMatches . Count >= _singleton . _options . CompletionQueryItems )
1118
+ {
1119
+ if ( ! PromptYesOrNo ( string . Format ( PSReadLineResources . DisplayAllPossibilities , completions . CompletionMatches . Count ) ) )
1120
+ {
1121
+ _singleton . PlaceCursor ( 0 , coords . Y + 1 ) ;
1122
+ WriteLine ( "" ) ;
1123
+ _singleton . PlaceCursor ( coords . X , coords . Y ) ;
1124
+ return ;
1125
+ }
1126
+ }
1127
+
1116
1128
var sb = new StringBuilder ( ) ;
1117
- var minColWidth = completions . CompletionMatches . Max ( c => c . ListItemText . Length ) ;
1129
+ var matches = completions . CompletionMatches . Select (
1130
+ completion =>
1131
+ new { ListItemText = Regex . Replace ( completion . ListItemText , "\n .*" , "..." ) ,
1132
+ ToolTip = Regex . Replace ( completion . ToolTip , "\n .*" , "..." ) } )
1133
+ . ToArray ( ) ;
1134
+ var minColWidth = matches . Max ( c => c . ListItemText . Length ) ;
1118
1135
minColWidth += 2 ;
1119
1136
1120
1137
if ( _singleton . Options . ShowToolTips )
1121
1138
{
1122
1139
const string seperator = "- " ;
1123
1140
var maxTooltipWidth = Console . BufferWidth - minColWidth - seperator . Length ;
1124
1141
1125
- foreach ( var match in completions . CompletionMatches )
1142
+ foreach ( var match in matches )
1126
1143
{
1127
1144
sb . Append ( match . ListItemText ) ;
1128
1145
var spacesNeeded = minColWidth - match . ListItemText . Length ;
@@ -1148,9 +1165,9 @@ public static void PossibleCompletions(ConsoleKeyInfo? key = null, object arg =
1148
1165
for ( var col = 0 ; col < displayColumns ; col ++ )
1149
1166
{
1150
1167
var index = row + ( displayRows * col ) ;
1151
- if ( index >= completions . CompletionMatches . Count )
1168
+ if ( index >= matches . Length )
1152
1169
break ;
1153
- var item = completions . CompletionMatches [ index ] . ListItemText ;
1170
+ var item = matches [ index ] . ListItemText ;
1154
1171
sb . Append ( item ) ;
1155
1172
sb . Append ( ' ' , minColWidth - item . Length ) ;
1156
1173
}
@@ -2070,7 +2087,7 @@ public static void Ding()
2070
2087
// The unit test framework redirects stdout - so it would see Console.WriteLine calls.
2071
2088
// Unfortunately, we are testing exact placement of characters on the screen, so redirection
2072
2089
// doesn't work for us.
2073
- static private void WriteLine ( string s )
2090
+ static private void WriteImpl ( string s )
2074
2091
{
2075
2092
Debug . Assert ( s . Length <= Console . BufferWidth ) ;
2076
2093
@@ -2097,10 +2114,37 @@ static private void WriteLine(string s)
2097
2114
Right = ( short ) s . Length
2098
2115
} ;
2099
2116
NativeMethods . WriteConsoleOutput ( handle , buffer , bufferSize , bufferCoord , ref writeRegion ) ;
2117
+ }
2118
+
2119
+ static private void WriteLine ( string s )
2120
+ {
2121
+ Debug . Assert ( s . Length <= Console . BufferWidth ) ;
2122
+
2123
+ var spaces = Console . BufferWidth - s . Length ;
2124
+ if ( spaces > 0 )
2125
+ {
2126
+ s = s + new string ( ' ' , spaces ) ;
2127
+ }
2128
+ WriteImpl ( s ) ;
2100
2129
2101
2130
_singleton . PlaceCursor ( 0 , Console . CursorTop + 1 ) ;
2102
2131
}
2103
2132
2133
+ static private void Write ( string s )
2134
+ {
2135
+ WriteImpl ( s ) ;
2136
+
2137
+ _singleton . PlaceCursor ( s . Length , Console . CursorTop ) ;
2138
+ }
2139
+
2140
+ static private bool PromptYesOrNo ( string s )
2141
+ {
2142
+ Write ( s ) ;
2143
+
2144
+ var key = ReadKey ( ) ;
2145
+ return key . Key == ConsoleKey . Y ;
2146
+ }
2147
+
2104
2148
private void RenderDemoWindow ( int windowStart )
2105
2149
{
2106
2150
int i ;
@@ -2344,6 +2388,10 @@ private void SetOptionsInternal(SetPSReadlineOption options)
2344
2388
{
2345
2389
Options . BellStyle = options . BellStyle ;
2346
2390
}
2391
+ if ( options . _completionQueryItems . HasValue )
2392
+ {
2393
+ Options . CompletionQueryItems = options . CompletionQueryItems ;
2394
+ }
2347
2395
if ( options . ResetTokenColors )
2348
2396
{
2349
2397
Options . ResetColors ( ) ;
0 commit comments