forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleHostUserInterface.cs
More file actions
2286 lines (1957 loc) · 77.3 KB
/
Copy pathConsoleHostUserInterface.cs
File metadata and controls
2286 lines (1957 loc) · 77.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Host;
using System.Security;
using Dbg = System.Management.Automation.Diagnostics;
#if !UNIX
using ConsoleHandle = Microsoft.Win32.SafeHandles.SafeFileHandle;
#endif
namespace Microsoft.PowerShell
{
using PowerShell = System.Management.Automation.PowerShell;
/// <summary>
///
/// ConsoleHostUserInterface implements console-mode user interface for powershell.exe
///
/// </summary>
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
internal partial class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInterface
{
/// <summary>
/// Command completion implementation object
/// </summary>
private PowerShell _commandCompletionPowerShell;
/// <summary>
/// This is a test hook for programmatically reading and writing ConsoleHost I/O.
/// </summary>
private static PSHostUserInterface s_h = null;
/// <summary>
/// Return true if the console supports a VT100 like virtual terminal
/// </summary>
public override bool SupportsVirtualTerminal { get; }
/// <summary>
///
/// Constructs an instance
///
/// </summary>
/// <param name="parent"></param>
/// <exception/>
internal ConsoleHostUserInterface(ConsoleHost parent)
{
Dbg.Assert(parent != null, "parent may not be null");
_parent = parent;
_rawui = new ConsoleHostRawUserInterface(this);
#if UNIX
SupportsVirtualTerminal = true;
#else
try
{
// Turn on virtual terminal if possible.
// This might throw - not sure how exactly (no console), but if it does, we shouldn't fail to start.
var handle = ConsoleControl.GetActiveScreenBufferHandle();
var m = ConsoleControl.GetMode(handle);
if (ConsoleControl.NativeMethods.SetConsoleMode(handle.DangerousGetHandle(), (uint)(m | ConsoleControl.ConsoleModes.VirtualTerminal)))
{
// We only know if vt100 is supported if the previous call actually set the new flag, older
// systems ignore the setting.
m = ConsoleControl.GetMode(handle);
this.SupportsVirtualTerminal = (m & ConsoleControl.ConsoleModes.VirtualTerminal) != 0;
}
}
catch
{
}
#endif
_isInteractiveTestToolListening = false;
}
/// <summary>
///
/// Supplies an implementation of PSHostRawUserInterface that provides low-level console mode UI facilities.
///
/// </summary>
/// <value></value>
/// <exception/>
public override PSHostRawUserInterface RawUI
{
get
{
Dbg.Assert(_rawui != null, "rawui should have been created by ctor");
// no locking because this is read-only, and allocated in the ctor.
return _rawui;
}
}
// deadcode; but could be needed in the future.
///// <summary>
///// gets the PSHost instance that uses this ConsoleHostUserInterface instance
///// </summary>
///// <value></value>
///// <exception/>
//internal
//PSHost
//Parent
//{
// get
// {
// using (tracer.TraceProperty())
// {
// // no locking because this is read-only and set in the ctor.
// return parent;
// }
// }
//}
/// <summary>
///
/// true if command completion is currently running
///
/// </summary>
internal bool IsCommandCompletionRunning
{
get
{
return _commandCompletionPowerShell != null &&
_commandCompletionPowerShell.InvocationStateInfo.State == PSInvocationState.Running;
}
}
/// <summary>
///
/// true if the Read* functions should read from the stdin stream instead of from the win32 console.
///
/// </summary>
internal bool ReadFromStdin { get; set; }
/// <summary>
///
/// true if the host shouldn't write out prompts.
///
/// </summary>
internal bool NoPrompt { get; set; }
#region Line-oriented interaction
/// <summary>
///
/// See base class
///
/// </summary>
/// <returns></returns>
/// <exception cref="HostException">
///
/// If Win32's SetConsoleMode fails
/// OR
/// Win32's ReadConsole fails
/// OR
/// obtaining information about the buffer failed
/// OR
/// Win32's SetConsoleCursorPosition failed
///
/// </exception>
public override string ReadLine()
{
HandleThrowOnReadAndPrompt();
// call our internal version such that it does not end input on a tab
ReadLineResult unused;
return ReadLine(false, "", out unused, true, true);
}
/// <summary>
///
/// See base class
///
/// </summary>
/// <returns></returns>
/// <exception cref="HostException">
///
/// If obtaining a handle to the active screen buffer failed
/// OR
/// Win32's setting input buffer mode to disregard window and mouse input failed
/// OR
/// Win32's ReadConsole failed
///
///
/// </exception>
/// <exception cref="PipelineStoppedException">
///
/// If Ctrl-C is entered by user
///
/// </exception>
public override SecureString ReadLineAsSecureString()
{
HandleThrowOnReadAndPrompt();
const char printToken = '*'; // This is not localizable
// we lock here so that multiple threads won't interleave the various reads and writes here.
object result = null;
lock (_instanceLock)
{
result = ReadLineSafe(true, printToken);
}
SecureString secureResult = result as SecureString;
System.Management.Automation.Diagnostics.Assert(secureResult != null, "ReadLineSafe did not return a SecureString");
return secureResult;
}
/// <summary>
///
/// Implementation based on NT CredUI's GetPasswdStr.
/// Use Win32.ReadConsole to construct a SecureString. The advantage of ReadConsole over ReadKey is
/// Alt-ddd where d is {0-9} is allowed.
/// It also manages the cursor as keys are entered and "backspaced". However, it is possible that
/// while this method is running, the console buffer contents could change. Then, its cursor mgmt
/// will likely be messed up.
///
/// Secondary implementation for Unix based on Console.ReadKey(), where
/// the advantage is portability through abstraction. Does not support
/// arrow key movement, but supports backspace.
///
/// </summary>
///<param name="isSecureString">
///
/// True to specify reading a SecureString; false reading a string
///
/// </param>
/// <param name="printToken">
///
/// string for output echo
///
/// </param>
/// <returns></returns>
/// <exception cref="HostException">
///
/// If obtaining a handle to the active screen buffer failed
/// OR
/// Win32's setting input buffer mode to disregard window and mouse input failed
/// OR
/// Win32's ReadConsole failed
/// OR
/// obtaining information about the buffer failed
/// OR
/// Win32's SetConsoleCursorPosition failed
///
/// </exception>
/// <exception cref="PipelineStoppedException">
///
/// If Ctrl-C is entered by user
///
/// </exception>
private object ReadLineSafe(bool isSecureString, char? printToken)
{
// Don't lock (instanceLock) in here -- the caller needs to do that...
PreRead();
string printTokenString = printToken.HasValue ?
printToken.ToString() :
null;
SecureString secureResult = new SecureString();
StringBuilder result = new StringBuilder();
#if UNIX
bool treatControlCAsInput = Console.TreatControlCAsInput;
#else
ConsoleHandle handle = ConsoleControl.GetConioDeviceHandle();
ConsoleControl.ConsoleModes originalMode = ConsoleControl.GetMode(handle);
bool isModeChanged = true; // assume ConsoleMode is changed so that if ReadLineSetMode
// fails to return the value correctly, the original mode is
// restored.
#endif
try
{
#if UNIX
Console.TreatControlCAsInput = true;
#else
// Ensure that we're in the proper line-input mode.
ConsoleControl.ConsoleModes desiredMode =
ConsoleControl.ConsoleModes.Extended |
ConsoleControl.ConsoleModes.QuickEdit;
ConsoleControl.ConsoleModes m = originalMode;
bool shouldUnsetEchoInput = shouldUnsetMode(ConsoleControl.ConsoleModes.EchoInput, ref m);
bool shouldUnsetLineInput = shouldUnsetMode(ConsoleControl.ConsoleModes.LineInput, ref m);
bool shouldUnsetMouseInput = shouldUnsetMode(ConsoleControl.ConsoleModes.MouseInput, ref m);
bool shouldUnsetProcessInput = shouldUnsetMode(ConsoleControl.ConsoleModes.ProcessedInput, ref m);
if ((m & desiredMode) != desiredMode ||
shouldUnsetMouseInput ||
shouldUnsetEchoInput ||
shouldUnsetLineInput ||
shouldUnsetProcessInput)
{
m |= desiredMode;
ConsoleControl.SetMode(handle, m);
}
else
{
isModeChanged = false;
}
_rawui.ClearKeyCache();
#endif
Coordinates originalCursorPos = _rawui.CursorPosition;
do
{
//
// read one char at a time so that we don't
// end up having a immutable string holding the
// secret in memory.
//
#if UNIX
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
#else
uint unused = 0;
string key = ConsoleControl.ReadConsole(handle, string.Empty, 1, false, out unused);
#endif
#if UNIX
// Handle Ctrl-C ending input
if (keyInfo.Key == ConsoleKey.C && keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control))
#else
if (string.IsNullOrEmpty(key) || (char)3 == key[0])
#endif
{
PipelineStoppedException e = new PipelineStoppedException();
throw e;
}
#if UNIX
if (keyInfo.Key == ConsoleKey.Enter)
#else
if ((char)13 == key[0])
#endif
{
//
// we are done if user presses ENTER key
//
break;
}
#if UNIX
if (keyInfo.Key == ConsoleKey.Backspace)
#else
if ((char)8 == key[0])
#endif
{
//
// for backspace, remove last char appended
//
if (isSecureString && secureResult.Length > 0)
{
secureResult.RemoveAt(secureResult.Length - 1);
WriteBackSpace(originalCursorPos);
}
else if (result.Length > 0)
{
result.Remove(result.Length - 1, 1);
WriteBackSpace(originalCursorPos);
}
}
#if UNIX
else if (Char.IsControl(keyInfo.KeyChar))
{
// blacklist control characters
continue;
}
#endif
else
{
//
// append the char to our string
//
if (isSecureString)
{
#if UNIX
secureResult.AppendChar(keyInfo.KeyChar);
#else
secureResult.AppendChar(key[0]);
#endif
}
else
{
#if UNIX
result.Append(keyInfo.KeyChar);
#else
result.Append(key);
#endif
}
if (!string.IsNullOrEmpty(printTokenString))
{
WritePrintToken(printTokenString, ref originalCursorPos);
}
}
}
while (true);
}
#if UNIX
catch (InvalidOperationException)
{
// ReadKey() failed so we stop
throw new PipelineStoppedException();
}
#endif
finally
{
#if UNIX
Console.TreatControlCAsInput = treatControlCAsInput;
#else
if (isModeChanged)
{
ConsoleControl.SetMode(handle, originalMode);
}
#endif
}
WriteLineToConsole();
PostRead(result.ToString());
if (isSecureString)
{
return secureResult;
}
else
{
return result;
}
}
/// <summary>
///
/// Handle writing print token with proper cursor adjustment for ReadLineSafe
///
/// </summary>
/// <param name="printToken">
///
/// token output for each char input. It must be a one-char string
///
/// </param>
/// <param name="originalCursorPosition">
///
/// it is the cursor position where ReadLineSafe begins
///
/// </param>
/// <exception cref="HostException">
///
/// If obtaining information about the buffer failed
/// OR
/// Win32's SetConsoleCursorPosition failed
///
/// </exception>
private void WritePrintToken(
string printToken,
ref Coordinates originalCursorPosition)
{
Dbg.Assert(!string.IsNullOrEmpty(printToken),
"Calling WritePrintToken with printToken being null or empty");
Dbg.Assert(printToken.Length == 1,
"Calling WritePrintToken with printToken's Length being " + printToken.Length);
Size consoleBufferSize = _rawui.BufferSize;
Coordinates currentCursorPosition = _rawui.CursorPosition;
// if the cursor is currently at the lower right corner, this write will cause the screen buffer to
// scroll up. So, it is necessary to adjust the original cursor position one row up.
if (currentCursorPosition.Y >= consoleBufferSize.Height - 1 && // last row
currentCursorPosition.X >= consoleBufferSize.Width - 1) // last column
{
if (originalCursorPosition.Y > 0)
{
originalCursorPosition.Y--;
}
}
WriteToConsole(printToken, false);
}
/// <summary>
///
/// Handle backspace with proper cursor adjustment for ReadLineSafe
///
/// </summary>
/// <param name="originalCursorPosition">
///
/// it is the cursor position where ReadLineSafe begins
///
/// </param>
/// <exception cref="HostException">
///
/// If obtaining information about the buffer failed
/// OR
/// Win32's SetConsoleCursorPosition failed
///
/// </exception>
private void WriteBackSpace(Coordinates originalCursorPosition)
{
Coordinates cursorPosition = _rawui.CursorPosition;
if (cursorPosition == originalCursorPosition)
{
// at originalCursorPosition, don't move
return;
}
if (cursorPosition.X == 0)
{
if (cursorPosition.Y <= originalCursorPosition.Y)
{
return;
}
// BufferSize.Width is 1 larger than cursor position
cursorPosition.X = _rawui.BufferSize.Width - 1;
cursorPosition.Y--;
BlankAtCursor(cursorPosition);
}
else if (cursorPosition.X > 0)
{
cursorPosition.X--;
BlankAtCursor(cursorPosition);
}
// do nothing if cursorPosition.X is left of screen
}
/// <summary>
/// Blank out at and move rawui.CursorPosition to <paramref name="cursorPosition"/>
/// </summary>
/// <param name="cursorPosition">Position to blank out</param>
private void BlankAtCursor(Coordinates cursorPosition)
{
_rawui.CursorPosition = cursorPosition;
WriteToConsole(" ", true);
_rawui.CursorPosition = cursorPosition;
}
#if !UNIX
/// <summary>
///
/// If <paramref name="m"/> is set on <paramref name="flagToUnset"/>, unset it and return true;
/// otherwise return false
///
/// </summary>
/// <param name="flagToUnset">
///
/// a flag in ConsoleControl.ConsoleModes to be unset in <paramref name="m"/>
///
/// </param>
/// <param name="m">
/// </param>
/// <returns>
///
/// true if <paramref name="m"/> is set on <paramref name="flagToUnset"/>
/// false otherwise
///
/// </returns>
private static bool shouldUnsetMode(
ConsoleControl.ConsoleModes flagToUnset,
ref ConsoleControl.ConsoleModes m)
{
if ((m & flagToUnset) > 0)
{
m &= ~flagToUnset;
return true;
}
return false;
}
#endif
#region WriteToConsole
internal void WriteToConsole(string value, bool transcribeResult)
{
#if !UNIX
ConsoleHandle handle = ConsoleControl.GetActiveScreenBufferHandle();
// Ensure that we're in the proper line-output mode. We don't lock here as it does not matter if we
// attempt to set the mode from multiple threads at once.
ConsoleControl.ConsoleModes m = ConsoleControl.GetMode(handle);
const ConsoleControl.ConsoleModes desiredMode =
ConsoleControl.ConsoleModes.ProcessedOutput
| ConsoleControl.ConsoleModes.WrapEndOfLine;
if ((m & desiredMode) != desiredMode)
{
m |= desiredMode;
ConsoleControl.SetMode(handle, m);
}
#endif
PreWrite();
// This is atomic, so we don't lock here...
#if !UNIX
ConsoleControl.WriteConsole(handle, value);
#else
Console.Out.Write(value);
#endif
if (_isInteractiveTestToolListening && Console.IsOutputRedirected)
{
Console.Out.Write(value);
}
if (transcribeResult)
{
PostWrite(value);
}
else
{
PostWrite();
}
}
private void WriteToConsole(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string text)
{
ConsoleColor fg = RawUI.ForegroundColor;
ConsoleColor bg = RawUI.BackgroundColor;
RawUI.ForegroundColor = foregroundColor;
RawUI.BackgroundColor = backgroundColor;
try
{
WriteToConsole(text, true);
}
finally
{
RawUI.ForegroundColor = fg;
RawUI.BackgroundColor = bg;
}
}
private void WriteLineToConsole(string text)
{
WriteToConsole(text, true);
WriteToConsole(Crlf, true);
}
private void WriteLineToConsole()
{
WriteToConsole(Crlf, true);
}
#endregion WriteToConsole
/// <summary>
///
/// See base class.
///
/// </summary>
/// <param name="value"></param>
/// <exception cref="HostException">
///
/// If Win32's CreateFile fails
/// OR
/// Win32's GetConsoleMode fails
/// OR
/// Win32's SetConsoleMode fails
/// OR
/// Win32's WriteConsole fails
///
/// </exception>
public override void Write(string value)
{
if (string.IsNullOrEmpty(value))
{
// do nothing
return;
}
// If the test hook is set, write to it and continue.
if (s_h != null) s_h.Write(value);
TextWriter writer = Console.IsOutputRedirected ? Console.Out : _parent.ConsoleTextWriter;
if (_parent.IsRunningAsync)
{
Dbg.Assert(writer == _parent.OutputSerializer.textWriter, "writers should be the same");
_parent.OutputSerializer.Serialize(value);
}
else
{
writer.Write(value);
}
}
/// <summary>
///
/// See base class
///
/// </summary>
/// <param name="foregroundColor"></param>
/// <param name="backgroundColor"></param>
/// <param name="value"></param>
/// <exception cref="HostException">
///
/// If obtaining information about the buffer failed
/// OR
/// Win32's SetConsoleTextAttribute
/// OR
/// Win32's CreateFile fails
/// OR
/// Win32's GetConsoleMode fails
/// OR
/// Win32's SetConsoleMode fails
/// OR
/// Win32's WriteConsole fails
///
/// </exception>
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
// Sync access so that we don't race on color settings if called from multiple threads.
lock (_instanceLock)
{
ConsoleColor fg = RawUI.ForegroundColor;
ConsoleColor bg = RawUI.BackgroundColor;
RawUI.ForegroundColor = foregroundColor;
RawUI.BackgroundColor = backgroundColor;
try
{
this.Write(value);
}
finally
{
RawUI.ForegroundColor = fg;
RawUI.BackgroundColor = bg;
}
}
}
/// <summary>
///
/// See base class
///
/// </summary>
/// <param name="value"></param>
/// <exception cref="HostException">
///
/// Win32's CreateFile fails
/// OR
/// Win32's GetConsoleMode fails
/// OR
/// Win32's SetConsoleMode fails
/// OR
/// Win32's WriteConsole fails
///
/// </exception>
public override void WriteLine(string value)
{
// lock here so that the newline is written atomically with the value
lock (_instanceLock)
{
this.Write(value);
this.Write(Crlf);
}
}
#region Word Wrapping
/// <summary>
///
/// This is a poor-man's word-wrapping routine. It breaks a single string into segments small enough to fit within a
/// given number of cells. A break is determined by the last occurrence of whitespace that allows all prior characters
/// on a line to be written within a given number of cells. If there is no whitespace found within that span, then the
/// largest span that will fit in the bounds is used.
///
/// The problem is complicated by the fact that a single character may consume more than one cell. Conceptually, this
/// is the same case as placing an upper bound on the length of a line while also having a strlen function that
/// arbitrarily considers the length of any single character to be 1 or greater.
///
/// </summary>
/// <param name="text">
///
/// Text to be emitted.
/// Each tab character in the text is replaced with a space in the results.
///
/// </param>
/// <param name="maxWidthInBufferCells">
///
/// Max width, in buffer cells, of a single line. Note that a single character may consume more than one cell. The
/// number of cells consumed is determined by calling ConsoleHostRawUserInterface.LengthInBufferCells.
///
/// </param>
/// <returns>
///
/// A list of strings representing the text broken into "lines" each of which are guaranteed not to exceed
/// maxWidthInBufferCells.
///
/// </returns>
internal List<string> WrapText(string text, int maxWidthInBufferCells)
{
List<string> result = new List<string>();
List<Word> words = ChopTextIntoWords(text, maxWidthInBufferCells);
if (words.Count < 1)
{
return result;
}
IEnumerator<Word> e = words.GetEnumerator();
bool valid = false;
int cellCounter = 0;
StringBuilder line = new StringBuilder();
string l = null;
do
{
valid = e.MoveNext();
if (!valid)
{
if (line.Length > 0)
{
l = line.ToString();
Dbg.Assert(RawUI.LengthInBufferCells(l) <= maxWidthInBufferCells, "line is too long");
result.Add(l);
}
break;
}
if ((e.Current.Flags & WordFlags.IsNewline) > 0)
{
l = line.ToString();
Dbg.Assert(RawUI.LengthInBufferCells(l) <= maxWidthInBufferCells, "line is too long");
result.Add(l);
// skip the newline "words"
line = new StringBuilder();
cellCounter = 0;
continue;
}
// will the word fit?
if (cellCounter + e.Current.CellCount <= maxWidthInBufferCells)
{
// yes, add it to the line.
line.Append(e.Current.Text);
cellCounter += e.Current.CellCount;
}
else
{
// no: too long. Either start a new line, or pick off as much whitespace as we need.
if ((e.Current.Flags & WordFlags.IsWhitespace) == 0)
{
l = line.ToString();
Dbg.Assert(RawUI.LengthInBufferCells(l) <= maxWidthInBufferCells, "line is too long");
result.Add(l);
line = new StringBuilder(e.Current.Text);
cellCounter = e.Current.CellCount;
continue;
}
// chop the whitespace into bits.
int w = maxWidthInBufferCells - cellCounter;
Dbg.Assert(w < e.Current.CellCount, "width remaining should be less than size of word");
line.Append(e.Current.Text.Substring(0, w));
l = line.ToString();
Dbg.Assert(RawUI.LengthInBufferCells(l) == maxWidthInBufferCells, "line should exactly fit");
result.Add(l);
string remaining = e.Current.Text.Substring(w);
line = new StringBuilder(remaining);
cellCounter = RawUI.LengthInBufferCells(remaining);
}
} while (valid);
return result;
}
/// <summary>
///
/// Struct used by WrapText
///
/// </summary>
[Flags]
internal enum WordFlags
{
IsWhitespace = 0x01,
IsNewline = 0x02
}
internal struct Word
{
internal int CellCount;
internal string Text;
internal WordFlags Flags;
}
/// <summary>
///
/// Chops text into "words," where a word is defined to be a sequence of whitespace characters, or a sequence of
/// non-whitespace characters, each sequence being no longer than a given maximum. Therefore, in the text "this is a
/// string" there are 7 words: 4 sequences of non-whitespace characters and 3 sequences of whitespace characters.
///
/// Whitespace is considered to be spaces or tabs. Each tab character is replaced with a single space.
///
/// </summary>
/// <param name="text">
///
/// The text to be chopped up.
///
/// </param>
/// <param name="maxWidthInBufferCells">
///
/// The maximum number of buffer cells that each word may consume.
///
/// </param>
/// <returns>
///
/// A list of words, in the same order they appear in the source text.
///
/// </returns>
/// <remarks>
///
/// This can be made faster by, instead of creating little strings for each word, creating indices of the start and end
/// range of a word. That would reduce the string allocations.
///
/// </remarks>
internal List<Word> ChopTextIntoWords(string text, int maxWidthInBufferCells)
{
List<Word> result = new List<Word>();
if (String.IsNullOrEmpty(text))
{
return result;
}
if (maxWidthInBufferCells < 1)
{
return result;
}