@@ -9,19 +9,52 @@ namespace Microsoft.PowerShell
9
9
{
10
10
public partial class PSConsoleReadLine
11
11
{
12
+ /// <summary>
13
+ /// Represents a prediction source.
14
+ /// </summary>
15
+ private readonly struct SourceInfo
16
+ {
17
+ internal readonly string SourceName ;
18
+ internal readonly int EndIndex ;
19
+ internal readonly int PrevSourceEndIndex ;
20
+ internal readonly int ItemCount ;
21
+
22
+ internal SourceInfo ( string sourceName , int endIndex , int prevSourceEndIndex )
23
+ {
24
+ SourceName = sourceName ;
25
+ int sourceWidth = LengthInBufferCells ( SourceName ) ;
26
+ if ( sourceWidth > PredictionListView . SourceMaxWidth )
27
+ {
28
+ sourceWidth = PredictionListView . SourceMaxWidth - 1 ;
29
+ int sourceStrLen = SubstringLengthByCells ( sourceName , sourceWidth ) ;
30
+ SourceName = sourceName . Substring ( 0 , sourceStrLen ) + SuggestionEntry . Ellipsis ;
31
+ }
32
+
33
+ EndIndex = endIndex ;
34
+ PrevSourceEndIndex = prevSourceEndIndex ;
35
+ ItemCount = EndIndex - PrevSourceEndIndex ;
36
+ }
37
+ }
38
+
12
39
/// <summary>
13
40
/// This type represents an individual suggestion entry.
14
41
/// </summary>
15
42
private struct SuggestionEntry
16
43
{
44
+ internal const char Ellipsis = '\u2026 ' ;
45
+ internal const string HistorySource = "History" ;
46
+
17
47
internal readonly Guid PredictorId ;
18
48
internal readonly uint ? PredictorSession ;
19
49
internal readonly string Source ;
20
50
internal readonly string SuggestionText ;
21
51
internal readonly int InputMatchIndex ;
22
52
53
+ private string _listItemTextRegular ;
54
+ private string _listItemTextSelected ;
55
+
23
56
internal SuggestionEntry ( string suggestion , int matchIndex )
24
- : this ( source : "History" , predictorId : Guid . Empty , predictorSession : null , suggestion , matchIndex )
57
+ : this ( source : HistorySource , predictorId : Guid . Empty , predictorSession : null , suggestion , matchIndex )
25
58
{
26
59
}
27
60
@@ -32,6 +65,8 @@ internal SuggestionEntry(string source, Guid predictorId, uint? predictorSession
32
65
PredictorSession = predictorSession ;
33
66
SuggestionText = suggestion ;
34
67
InputMatchIndex = matchIndex ;
68
+
69
+ _listItemTextRegular = _listItemTextSelected = null ;
35
70
}
36
71
37
72
/// <summary>
@@ -57,8 +92,19 @@ private static int DivideAndRoundUp(int dividend, int divisor)
57
92
/// <param name="selectionHighlighting">The highlighting sequences for a selected list item.</param>
58
93
internal string GetListItemText ( int width , string input , string selectionHighlighting )
59
94
{
60
- const string ellipsis = "..." ;
61
- const int ellipsisLength = 3 ;
95
+ const int ellipsisLength = 1 ;
96
+
97
+ if ( selectionHighlighting is null )
98
+ {
99
+ if ( _listItemTextRegular is not null )
100
+ {
101
+ return _listItemTextRegular ;
102
+ }
103
+ }
104
+ else if ( _listItemTextSelected is not null )
105
+ {
106
+ return _listItemTextSelected ;
107
+ }
62
108
63
109
// Calculate the 'SOURCE' portion to be rendered.
64
110
int sourceStrLen = Source . Length ;
@@ -119,7 +165,7 @@ internal string GetListItemText(int width, string input, string selectionHighlig
119
165
// The suggestion text doesn't contain the user input.
120
166
int length = SubstringLengthByCells ( SuggestionText , textWidth - ellipsisLength ) ;
121
167
line . Append ( SuggestionText , 0 , length )
122
- . Append ( ellipsis ) ;
168
+ . Append ( Ellipsis ) ;
123
169
break ;
124
170
}
125
171
@@ -136,7 +182,7 @@ internal string GetListItemText(int width, string input, string selectionHighlig
136
182
. Append ( SuggestionText , 0 , input . Length )
137
183
. EndColorSection ( selectionHighlighting )
138
184
. Append ( SuggestionText , input . Length , length - input . Length )
139
- . Append ( ellipsis ) ;
185
+ . Append ( Ellipsis ) ;
140
186
}
141
187
else
142
188
{
@@ -149,7 +195,7 @@ internal string GetListItemText(int width, string input, string selectionHighlig
149
195
int remainingLenInCells = textWidth - ellipsisLength - rightLenInCells ;
150
196
int length = SubstringLengthByCellsFromEnd ( SuggestionText , input . Length - 1 , remainingLenInCells ) ;
151
197
line . Append ( _singleton . _options . EmphasisColor )
152
- . Append ( ellipsis )
198
+ . Append ( Ellipsis )
153
199
. Append ( SuggestionText , input . Length - length , length )
154
200
. EndColorSection ( selectionHighlighting )
155
201
. Append ( SuggestionText , input . Length , SuggestionText . Length - input . Length ) ;
@@ -162,11 +208,11 @@ internal string GetListItemText(int width, string input, string selectionHighlig
162
208
int startIndex = input . Length - leftStrLen ;
163
209
int totalStrLen = SubstringLengthByCells ( SuggestionText , startIndex , textWidth - ellipsisLength * 2 ) ;
164
210
line . Append ( _singleton . _options . EmphasisColor )
165
- . Append ( ellipsis )
211
+ . Append ( Ellipsis )
166
212
. Append ( SuggestionText , startIndex , leftStrLen )
167
213
. EndColorSection ( selectionHighlighting )
168
214
. Append ( SuggestionText , input . Length , totalStrLen - leftStrLen )
169
- . Append ( ellipsis ) ;
215
+ . Append ( Ellipsis ) ;
170
216
}
171
217
}
172
218
@@ -192,7 +238,7 @@ internal string GetListItemText(int width, string input, string selectionHighlig
192
238
. Append ( SuggestionText , InputMatchIndex , input . Length )
193
239
. EndColorSection ( selectionHighlighting )
194
240
. Append ( SuggestionText , rightStartindex , rightStrLen )
195
- . Append ( ellipsis ) ;
241
+ . Append ( Ellipsis ) ;
196
242
break ;
197
243
}
198
244
@@ -201,7 +247,7 @@ internal string GetListItemText(int width, string input, string selectionHighlig
201
247
{
202
248
// Otherwise, if the (mid+right) portions take up to 2/3 of the text width, we just truncate the suggestion text at the beginning.
203
249
int leftStrLen = SubstringLengthByCellsFromEnd ( SuggestionText , InputMatchIndex - 1 , textWidth - midRightLenInCells - ellipsisLength ) ;
204
- line . Append ( ellipsis )
250
+ line . Append ( Ellipsis )
205
251
. Append ( SuggestionText , InputMatchIndex - leftStrLen , leftStrLen )
206
252
. Append ( _singleton . _options . EmphasisColor )
207
253
. Append ( SuggestionText , InputMatchIndex , input . Length )
@@ -223,13 +269,13 @@ internal string GetListItemText(int width, string input, string selectionHighlig
223
269
int leftStrLen = SubstringLengthByCellsFromEnd ( SuggestionText , InputMatchIndex - 1 , leftCellLen - ellipsisLength ) ;
224
270
int rightStrLen = SubstringLengthByCells ( SuggestionText , rightStartindex , rigthCellLen - ellipsisLength ) ;
225
271
226
- line . Append ( ellipsis )
272
+ line . Append ( Ellipsis )
227
273
. Append ( SuggestionText , InputMatchIndex - leftStrLen , leftStrLen )
228
274
. Append ( _singleton . _options . EmphasisColor )
229
275
. Append ( SuggestionText , InputMatchIndex , input . Length )
230
276
. EndColorSection ( selectionHighlighting )
231
277
. Append ( SuggestionText , rightStartindex , rightStrLen )
232
- . Append ( ellipsis ) ;
278
+ . Append ( Ellipsis ) ;
233
279
break ;
234
280
}
235
281
@@ -249,7 +295,7 @@ internal string GetListItemText(int width, string input, string selectionHighlig
249
295
line . Append ( SuggestionText , 0 , InputMatchIndex )
250
296
. Append ( _singleton . _options . EmphasisColor )
251
297
. Append ( SuggestionText , InputMatchIndex , midLeftStrLen )
252
- . Append ( ellipsis )
298
+ . Append ( Ellipsis )
253
299
. Append ( SuggestionText , rightStartindex - midRightStrLen , midRightStrLen )
254
300
. EndColorSection ( selectionHighlighting )
255
301
. Append ( SuggestionText , rightStartindex , SuggestionText . Length - rightStartindex ) ;
@@ -277,11 +323,11 @@ internal string GetListItemText(int width, string input, string selectionHighlig
277
323
line . Append ( SuggestionText , 0 , InputMatchIndex )
278
324
. Append ( _singleton . _options . EmphasisColor )
279
325
. Append ( SuggestionText , InputMatchIndex , midLeftStrLen )
280
- . Append ( ellipsis )
326
+ . Append ( Ellipsis )
281
327
. Append ( SuggestionText , rightStartindex - midRightStrLen , midRightStrLen )
282
328
. EndColorSection ( selectionHighlighting )
283
329
. Append ( SuggestionText , rightStartindex , rightStrLen )
284
- . Append ( ellipsis ) ;
330
+ . Append ( Ellipsis ) ;
285
331
break ;
286
332
}
287
333
@@ -298,11 +344,11 @@ internal string GetListItemText(int width, string input, string selectionHighlig
298
344
int midRightStrLen = SubstringLengthByCellsFromEnd ( SuggestionText , rightStartindex - 1 , midRightCellLen ) ;
299
345
int leftStrLen = SubstringLengthByCellsFromEnd ( SuggestionText , InputMatchIndex - 1 , midRemainingLenInCells ) ;
300
346
301
- line . Append ( ellipsis )
347
+ line . Append ( Ellipsis )
302
348
. Append ( SuggestionText , InputMatchIndex - leftStrLen , leftStrLen )
303
349
. Append ( _singleton . _options . EmphasisColor )
304
350
. Append ( SuggestionText , InputMatchIndex , midLeftStrLen )
305
- . Append ( ellipsis )
351
+ . Append ( Ellipsis )
306
352
. Append ( SuggestionText , rightStartindex - midRightStrLen , midRightStrLen )
307
353
. EndColorSection ( selectionHighlighting )
308
354
. Append ( SuggestionText , rightStartindex , SuggestionText . Length - rightStartindex ) ;
@@ -324,15 +370,15 @@ internal string GetListItemText(int width, string input, string selectionHighlig
324
370
int spacesNeeded = textWidth - midRemainingLenInCells * 3 - ellipsisLength * 3 ;
325
371
string spaces = spacesNeeded > 0 ? Spaces ( spacesNeeded ) : string . Empty ;
326
372
327
- line . Append ( ellipsis )
373
+ line . Append ( Ellipsis )
328
374
. Append ( SuggestionText , InputMatchIndex - leftStrLen , leftStrLen )
329
375
. Append ( _singleton . _options . EmphasisColor )
330
376
. Append ( SuggestionText , InputMatchIndex , midLeftStrLen )
331
- . Append ( ellipsis )
377
+ . Append ( Ellipsis )
332
378
. Append ( SuggestionText , rightStartindex - midRightStrLen , midRightStrLen )
333
379
. EndColorSection ( selectionHighlighting )
334
380
. Append ( SuggestionText , rightStartindex , rightStrLen )
335
- . Append ( ellipsis )
381
+ . Append ( Ellipsis )
336
382
. Append ( spaces ) ;
337
383
break ;
338
384
}
@@ -351,13 +397,29 @@ internal string GetListItemText(int width, string input, string selectionHighlig
351
397
else
352
398
{
353
399
line . Append ( Source , 0 , sourceStrLen )
354
- . Append ( ellipsis ) ;
400
+ . Append ( Ellipsis ) ;
355
401
}
356
402
357
403
line . EndColorSection ( selectionHighlighting )
358
404
. Append ( ']' ) ;
359
405
360
- return line . ToString ( ) ;
406
+ if ( selectionHighlighting is not null )
407
+ {
408
+ // Need to reset at the end if the selection highlighting is being applied.
409
+ line . Append ( VTColorUtils . AnsiReset ) ;
410
+ }
411
+
412
+ string textForRendering = line . ToString ( ) ;
413
+ if ( selectionHighlighting is null )
414
+ {
415
+ _listItemTextRegular = textForRendering ;
416
+ }
417
+ else
418
+ {
419
+ _listItemTextSelected = textForRendering ;
420
+ }
421
+
422
+ return textForRendering ;
361
423
}
362
424
}
363
425
}
0 commit comments