I have used some code from another question to get word wrapping to affect whole words and not split them.
I would like to have additional strings included that contain formatting, but have not been able to figure it out or find anything on the Google machine.
See code below.
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>
public static void WordWrap(string paragraph, int tabSize = 8)
{
string[] lines = paragraph
.Replace("\t", new String(' ', tabSize))
.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < lines.Length; i++) {
string process = lines[i];
List<String> wrapped = new List<string>();
while (process.Length > Console.WindowWidth) {
int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1, process.Length));
if (wrapAt <= 0) break;
wrapped.Add(process.Substring(0, wrapAt));
process = process.Remove(0, wrapAt + 1);
}
foreach (string wrap in wrapped) {
Console.WriteLine(wrap);
}
Console.WriteLine(process);
}
}
The formatting I would like to use is simply changing the colour for a dialogue when someone is speaking, or for certain key words (place names, items and character names etc).
See code below.
public static void townName(string town)
{
Console.ForegroundColor = ConsoleColor.Magenta;
Game.WordWrap(town);
Console.ResetColor();
}
public static void Dialog(string message)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Game.WordWrap(message);
Console.ResetColor();
}
public static void Villain()
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Game.WordWrap("Zanbar Bone");
Console.ResetColor();
}
Any help is greatly appreciated, be gentle with me though as I am still learning. Layman's terms would be very helpful :)