public Cursor Print(ColoredString text)
{
// If we don't want the pretty print, or we're printing a single character (for example, from keyboard input)
// Then use the pretty print system.
if (!DisableWordBreak && text.String.Length != 1)
{
// Prep
var console = (SurfaceEditor)_console.Target;
ColoredGlyph glyph;
ColoredGlyph spaceGlyph = text[0].Clone();
spaceGlyph.Glyph = ' ';
string stringText = text.String.TrimEnd(' ');
// Pull any starting spaces off
var newStringText = stringText.TrimStart(' ');
int spaceCount = stringText.Length - newStringText.Length;
for (int i = 0; i < spaceCount; i++)
PrintGlyph(spaceGlyph, text);
if (spaceCount != 0)
text = text.SubString(spaceCount, text.Count - spaceCount);
stringText = newStringText;
string[] parts = stringText.Split(' ');
// Start processing the string
int c = 0;
for (int wordMajor = 0; wordMajor < parts.Length; wordMajor++)
{
// Words broken up by spaces = parts
if (parts[wordMajor].Length != 0)
{
// Parts broken by new lines = newLineParts
string[] newlineParts = parts[wordMajor].Split('\n');
for (int indexNL = 0; indexNL < newlineParts.Length; indexNL++)
{
if (newlineParts[indexNL].Length != 0)
{
int currentLine = _position.Y;
// New line parts broken up by carriage returns = returnParts
string[] returnParts = newlineParts[indexNL].Split('\r');
for (int indexR = 0; indexR < returnParts.Length; indexR++)
{
// If the text we'll print will move off the edge, fill with spaces to get a fresh line
if (returnParts[indexR].Length > console.Width - _position.X && _position.X != 0)
{
var spaces = console.Width - _position.X;
// Fill rest of line with spaces
for (int i = 0; i < spaces; i++)
PrintGlyph(spaceGlyph, text);
}
// Print the rest of the text as normal.
for (int i = 0; i < returnParts[indexR].Length; i++)
{
glyph = text[c];
PrintGlyph(glyph, text);
c++;
}
// If we had a \r in the string, handle it by going back
if (returnParts.Length != 1 && indexR != returnParts.Length - 1)
{
// Wrapped to a new line through print glyph, which triggerd \r\n. We don't want the \n so return back.
if (_position.X == 0 && _position.Y != currentLine)
_position.Y -= 1;
else
CarriageReturn();
c++;
}
}
}
// We had \n in the string, handle them.
if (newlineParts.Length != 1 && indexNL != newlineParts.Length - 1)
{
if (!UseLinuxLineEndings)
LineFeed();
else
NewLine();
c++;
}
}
}
// Not last part
if (wordMajor != parts.Length - 1 && _position.X != 0)
{
PrintGlyph(spaceGlyph, text);
c++;
}
else
c++;
}
}
else
{
bool movedLines = false;
int oldLine = _position.Y;
foreach (var glyph in text)
{
// Check if the previous print moved us down a line (from print at end of the line) and move use back for the \r
if (movedLines)
{
if (_position.X == 0 && glyph.Glyph == '\r')
{
_position.Y -= 1;
continue;
}
else
movedLines = false;
}
if (glyph.Glyph == '\r')
CarriageReturn();
else if (glyph.Glyph == '\n')
{
if (!UseLinuxLineEndings)
LineFeed();
else
NewLine();
}
else
{
PrintGlyph(glyph, text);
// Lines changed and it wasn't a \n that caused it, so it was a print that did it.
movedLines = _position.Y != oldLine;
}
}
}
return this;
}