/// <summary> /// Fixes format numbers in <see cref="KolorSection.Message"/> /// </summary> /// <param name="sect">Section to fix</param> public static string FixFormats(KolorSection sect) { var ch = ' '; int i = 0; int f = 0; StringBuilder newmsg = new StringBuilder(); while (i < sect.Message.Length) { ch = sect.Message[i++]; if (ch == '{') // Incoming format { if (i < sect.Message.Length && sect.Message[i] == '{') // Format escape { newmsg.Append("{{"); i++; continue; } // We will skip over this and write ours string fmt = ""; while (i < sect.Message.Length && (ch = sect.Message[i]) != '}') { fmt += sect.Message[i++]; } fmt = fmt.Trim(); var split = fmt.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (split.Length == 2) // {2:X4} { newmsg.Append('{'); newmsg.Append(f++); newmsg.Append(':'); newmsg.Append(split[1]); newmsg.Append('}'); } else // {2} { newmsg.Append('{'); newmsg.Append(f++); newmsg.Append('}'); } i++; // Skip '}' continue; } newmsg.Append(ch); } return(newmsg.ToString()); }
/// <summary> /// Generates colored message sections based on given message string /// </summary> /// <param name="msg">Message string to generate sections</param> public static List <KolorSection> GenerateSections(string msg) { var lst = new List <KolorSection>(); KolorSection sect = new KolorSection(""); var ch = ' '; var i = 0; StringBuilder part = new StringBuilder(); while (i < msg.Length) { ch = msg[i++]; if (ch == '{') // A format part { if (i < msg.Length && msg[i] == '{') // Format skip { i++; part.Append("{{"); continue; } sect.Formats++; } if (ch == '&') // A new section { if (i < msg.Length && msg[i] == '&') // A '&' escape { i++; part.Append('%'); continue; } sect.Message = part.ToString(); part = new StringBuilder(); lst.Add(sect); string cl = ""; while (i < msg.Length && (ch = msg[i]) != ';') { cl += msg[i++]; } cl = cl.Trim(); if (cl != "back") { if (!TryFindColor(cl, out ConsoleColor color)) { color = ConsoleColor.Gray; // fallback to gray } sect = new KolorSection(""); sect.Color = color; sect.ColorChange = true; } else { sect = new KolorSection(""); sect.Back = 1; } i++; continue; } if (ch == '%') // A new section { if (i < msg.Length && msg[i] == '%') // A '%' escape { i++; part.Append('%'); continue; } sect.Message = part.ToString(); part = new StringBuilder(); lst.Add(sect); string cl = ""; while (i < msg.Length && (ch = msg[i]) != ';') { cl += msg[i++]; } cl = cl.Trim(); if (!cl.Contains("*")) { if (!int.TryParse(cl, out int code)) { code = 7; // fallback to gray } if (!TryFindColor(code, out ConsoleColor color)) { color = ConsoleColor.Gray; } sect = new KolorSection(""); sect.Color = color; sect.ColorChange = true; } else { sect = new KolorSection(""); sect.Back = Count(cl, '*'); } i++; continue; } part.Append(ch); // Nothing special, save it to message } var str = part.ToString(); if (str != "") // Non-empty section { sect.Message = str; lst.Add(sect); } else if (sect.Back > 0) // Revert color section { lst.Add(sect); } return(lst); }