using System;
using System.Collections.Generic;
/// <summary>
/// Writes the specified data, followed by the current line terminator, to the standard output stream, while wrapping lines that would otherwise break words.
/// </summary>
/// <param name="paragraph">The value to write.</param>
/// <param name="tabSize">The value that indicates the column width of tab characters.</param>
/// <param name="intMargin">The value that indicates the margin width.</param>
public static void WriteLineWordWrap(string paragraph, int tabSize = 8, int intMargin)
{
strMargin = "";
//build Margin
for (int i = 0; i < intMargin; i++)
strMargin = strMargin + " ";
//Store words into buffer
string[] lines = paragraph
.Replace("\t", new String(' ', tabSize))
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
//Process buffer into a list for efficient calculations
for (int i = 0; i < lines.Length; i++) {
string process = lines[i];
List<String> wrapped = new List<string>();
//Measure window and calculate each line
while (process.Length > Console.WindowWidth) {
int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1 - intMargin - intMargin, process.Length));
if (wrapAt <= 0) break;
wrapped.Add(process.Substring(0, wrapAt));
process = process.Remove(0, wrapAt + 1);
}
//Print lines + margin
foreach (string wrap in wrapped) {
Console.WriteLine(strMargin + wrap + strMargin);
}
Console.WriteLine(process);
}
}