public ConsoleString Evaluate(DocumentRendererContext context)
{
var eval = context.EvaluateExpression(this.EvalToken.Value);
if(eval == null)
{
throw new DocumentRenderException("NullReference for '" + this.EvalToken.Value + "'", this.EvalToken);
}
else if(eval is IEnumerable == false)
{
throw new DocumentRenderException("'" + this.EvalToken.Value + "' is not enumerable", this.EvalToken);
}
IEnumerable collection = (IEnumerable)eval;
List<ConsoleString> headers = new List<ConsoleString>();
List<List<ConsoleString>> rows = new List<List<ConsoleString>>();
List<ColumnOverflowBehavior> overflows = new List<ColumnOverflowBehavior>();
for (int colIndex = 0; colIndex < Columns.Count; colIndex++ )
{
var col = Columns[colIndex];
var colValue = col.Value;
if (colValue.EndsWith("+"))
{
colValue = colValue.Substring(0, colValue.Length - 1);
overflows.Add(new SmartWrapOverflowBehavior());
if(colIndex != Columns.Count-1)
{
throw new DocumentRenderException("The auto expand indicator '+' can only be used on the last column", col);
}
}
else
{
overflows.Add(new GrowUnboundedOverflowBehavior());
}
if (colValue.Contains(">"))
{
var newColName = colValue.Split('>')[1];
headers.Add(new ConsoleString(newColName, ConsoleColor.Yellow));
}
else
{
headers.Add(new ConsoleString(colValue, ConsoleColor.Yellow));
}
}
foreach(var element in collection)
{
if(element is CommandLineArgument && ((CommandLineArgument)element).OmitFromUsage)
{
continue;
}
var row = new List<ConsoleString>();
foreach (var col in Columns)
{
string propName;
if (col.Value.Contains(">"))
{
propName = col.Value.Split('>')[0];
}
else
{
propName = col.Value;
}
if(propName.EndsWith("+"))
{
propName = propName.Substring(0, propName.Length - 1);
}
var propToGet = element.GetType().GetProperty(propName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
if (propToGet == null) throw new DocumentRenderException("'" + propName + "' is not a valid property for type '" + element.GetType().FullName + "'", col);
var value = propToGet.GetValue(element, null);
ConsoleString valueString;
if(value != null)
{
valueString = new ConsoleString(value.ToString());
if (ShowDefaultValuesForArguments && element is CommandLineArgument && propToGet.Name == "Description" && ((CommandLineArgument)element).DefaultValue != null)
{
valueString+= new ConsoleString(" [Default='" + ((CommandLineArgument)element).DefaultValue.ToString() + "'] ", ConsoleColor.DarkGreen);
}
}
else
{
valueString = ConsoleString.Empty;
}
row.Add(valueString);
}
rows.Add(row);
if(ShowPossibleValuesForArguments && element is CommandLineArgument && ((CommandLineArgument)element).IsEnum)
{
foreach (var val in ((CommandLineArgument)element).EnumValuesAndDescriptions)
{
List<ConsoleString> possibilitiesRow = new List<ConsoleString>();
for (int i = 0; i < Columns.Count - 1; i++)
{
possibilitiesRow.Add(ConsoleString.Empty);
}
possibilitiesRow.Add(new ConsoleString(val, ConsoleColor.DarkGreen));
rows.Add(possibilitiesRow);
}
}
}
string rowPrefix = "";
for(int i = 0; i < indent; i++)
{
rowPrefix += " ";
}
ConsoleTableBuilder builder = new ConsoleTableBuilder();
var tableText = builder.FormatAsTable(headers, rows, rowPrefix: rowPrefix, columnOverflowBehaviors: overflows);
// remove the prefix from the first row
tableText = tableText.Substring(indent);
var tableTextStr = tableText.ToString();
return tableText;
}