Changed table formatting to typically exclude hidden keys

Fixed issue with string centering (Closes #4)
This commit is contained in:
Brychan Dempsey 2021-03-15 17:28:20 +13:00
parent ff4eaa0997
commit 10bd29c296

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Collections.ObjectModel;
namespace Assignment_1 namespace Assignment_1
{ {
@ -216,25 +218,38 @@ namespace Assignment_1
/// <summary> /// <summary>
/// Creates and prints a list of all defined variables /// Creates and prints a list of all defined variables
/// </summary> /// </summary>
void List() void List(bool printUnprint = false)
{ {
Console.WriteLine("┌" + new string('─', 15) + "┬" + new string('─', 25) + "┬" + new string('─', 9) + "┐"); int keyWidth = 10;
Console.WriteLine("│{0}│{1}│{2}│", CenterString("Symbol",15), CenterString("Value",25), CenterString("Flags",9)); int valueWidth = 50;
Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤"); int flagWidth = 9;
int keyPos = 0; Console.WriteLine("┌" + new string('─', keyWidth) + "┬" + new string('─', valueWidth) + "┬" + new string('─', flagWidth) + "┐");
foreach (var item in Symbols) Console.WriteLine("│{0}│{1}│{2}│", CenterString("Symbol", keyWidth), CenterString("Value", valueWidth), CenterString("Flags", flagWidth));
// Figure out how many symbols are eligible for printing
List<string> eligibleKeys = new List<string>(Symbols.Count);
foreach (var item in Symbols.Keys)
{ {
Console.WriteLine("│{0,-15}│{1,-25}│{2,9}│", item.Key, item.Value.Item1.Replace("\r","\\r").Replace("\n","\\n").Replace("\t", "\\t"), Convert.ToString((byte)item.Value.Item2,2).PadLeft(8,'0')); if (!Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) || (Symbols[item].Item2.HasFlag(VariableFlags.NoPrint) && printUnprint))
if (keyPos == Symbols.Count-1)
{ {
Console.WriteLine("└" + new string('─', 15) + "┴" + new string('─', 25) + "┴" + new string('─', 9) + "┘"); eligibleKeys.Add(item);
} }
else
{
Console.WriteLine("├" + new string('─', 15) + "┼" + new string('─', 25) + "┼" + new string('─', 9) + "┤");
}
keyPos++;
} }
// Control printing based on how many keys are available
if (eligibleKeys.Count > 0)
{
Console.WriteLine("├" + new string('─', keyWidth) + "┼" + new string('─', valueWidth) + "┼" + new string('─', flagWidth) + "┤");
for (int i = 0; i < eligibleKeys.Count; i++)
{
string entryFormat = "│{0," + -1*keyWidth + "}│{1," + -1*valueWidth + "}│{2," + -1*flagWidth + "}│";
Console.WriteLine(entryFormat, eligibleKeys[i], Symbols[eligibleKeys[i]].Item1.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t"), Convert.ToString((byte)Symbols[eligibleKeys[i]].Item2, 2).PadLeft(8, '0'));
if (i + 1 < eligibleKeys.Count)
{
Console.WriteLine("├" + new string('─', keyWidth) + "┼" + new string('─', valueWidth) + "┼" + new string('─', flagWidth) + "┤");
}
}
}
Console.WriteLine("└" + new string('─', keyWidth) + "┴" + new string('─', valueWidth) + "┴" + new string('─', flagWidth) + "┘");
} }
void Exit() void Exit()
{ {
@ -720,7 +735,7 @@ namespace Assignment_1
int leftHalfPad = (int)Math.Floor(totalPadding / 2.0); int leftHalfPad = (int)Math.Floor(totalPadding / 2.0);
int rightHalfPad = (int)Math.Ceiling(totalPadding / 2.0); int rightHalfPad = (int)Math.Ceiling(totalPadding / 2.0);
string t = "{0," + leftHalfPad + "}{1," + -1 * rightHalfPad + "}"; string t = "{0," + leftHalfPad + "}{1," + -1 * rightHalfPad + "}";
string result = string.Format(t, source.Substring(0,leftHalf+1), source.Substring(rightHalf,source.Length-rightHalf)); string result = string.Format(t, source[..rightHalf], source[rightHalf..]);
return result; return result;
} }
#endregion #endregion