Example #1
0
 /// <summary>
 /// Evaluates the evaluation expression against a data context, optionally setting the console color if the expression contains those parameters
 /// </summary>
 /// <param name="context">The datta context to evaluate against</param>
 /// <returns>The result of the evaluation as a ConsoleString</returns>
 public ConsoleString Evaluate(DocumentRendererContext context)
 {
     context.LocalVariables.PushConsoleColors(FG, BG);
     try
     {
         var eval = context.EvaluateExpression(this.EvalToken.Value);
         if (eval == null)
         {
             return ConsoleString.Empty;
         }
         else
         {
             if (eval is ConsoleString)
             {
                 return (ConsoleString)eval;
             }
             else if(eval is string)
             {
                 return new ConsoleString(eval as string, context.LocalVariables.CurrentForegroundColor, context.LocalVariables.CurrentBackgroundColor);
             }
             else
             {
                 var result = eval.ToString();
                 var ret = context.RenderDynamicContent(result, this.EvalToken);
                 return ret;
             }
         }
     }
     finally
     {
         context.LocalVariables.PopConsoleColors();
     }
 }
Example #2
0
        /// <summary>
        /// Evaluates the each loop
        /// </summary>
        /// <param name="context">The context that contains information about the document being rendered</param>
        /// <returns>The rendered contents of the each loop</returns>
        public ConsoleString Evaluate(DocumentRendererContext context)
        {
            var collection = context.EvaluateExpression(this.CollectionVariableExpressionToken.Value);
            if (collection == null)
            {
                throw new DocumentRenderException("'" + this.CollectionVariableExpressionToken.Value + "' resolved to a null reference", this.CollectionVariableExpressionToken);
            }

            if(collection is IEnumerable == false)
            {
                throw new DocumentRenderException("'" + this.CollectionVariableExpressionToken.Value + "' does not resolve to a collection", this.CollectionVariableExpressionToken);
            }
            ConsoleString ret = ConsoleString.Empty;
            int index = 0;
            var iterationVariableName = this.IterationVariableNameToken.Value + "-index";
            foreach(var item in (IEnumerable)collection)
            {
                context.LocalVariables.Add(this.IterationVariableNameToken, item);
                context.LocalVariables.Force(iterationVariableName, index);
                ret+= context.RenderBody(this.Body);
                context.LocalVariables.Remove(this.IterationVariableNameToken);
                context.LocalVariables.ForceClear(iterationVariableName);
                index++;
            }
            return ret;
        }
Example #3
0
 /// <summary>
 /// Evaluates the conditional expression against the given data context, rendering the body only if it is true.
 /// </summary>
 /// <param name="context">The data context to use when evaluating the conditional expression</param>
 /// <returns>The rendered body if the conditional was true, an empty string otherwise</returns>
 public virtual ConsoleString Evaluate(DocumentRendererContext context)
 {
     var eval = context.EvaluateExpression(this.IfExpressionToken.Value);
     if(true.Equals(eval) || 1.Equals(eval))
     {
         return context.RenderBody(this.Body);
     }
     else
     {
         return ConsoleString.Empty;
     }
 }
 /// <summary>
 /// Finds the matching template from the data context, evaluates the data expression, then renders
 /// the template against the data.  The rendered document is inserted into the parent document.
 /// </summary>
 /// <param name="context">The data context used to find the named template and to evaluate the data expression</param>
 /// <returns>The rendered child document to be inserted into the parent document</returns>
 public ConsoleString Evaluate(DocumentRendererContext context)
 {
     DocumentTemplateInfo target = context.DocumentRenderer.GetTemplate(this.IdToken);
     var eval = context.EvaluateExpression(this.EvalToken.Value);
     return context.DocumentRenderer.Render(target.Value, eval, target.SourceLocation);
 }
Example #5
0
 /// <summary>
 /// Removes the named variable from the context's local variable set
 /// </summary>
 /// <param name="context">the context that should contain the local variable to remove</param>
 /// <returns>an empty string</returns>
 public ConsoleString Evaluate(DocumentRendererContext context)
 {
     context.LocalVariables.Remove(NameToken);
     return ConsoleString.Empty;
 }
Example #6
0
 /// <summary>
 /// Always results in an empty string, but initializes the local value in the data context
 /// </summary>
 /// <param name="context">The data context used to store the newly initialized variable</param>
 /// <returns>an empty string</returns>
 public ConsoleString Evaluate(DocumentRendererContext context)
 {
     if (NameToken.Value == "ConsoleForegroundColor" || NameToken.Value == "ConsoleBackgroundColor")
     {
         ConsoleColor value;
         if(Enum.TryParse<ConsoleColor>(ValueToken.Value,out value) == false)
         {
             throw new DocumentRenderException("Invalid ConsoleColor '" + ValueToken.Value + "'", ValueToken);
         }
         context.LocalVariables.Add(NameToken, value);
     }
     else
     {
         var value = context.EvaluateExpression(ValueToken.Value);
         context.LocalVariables.Add(NameToken, value);
     }
     return ConsoleString.Empty;
 }
Example #7
0
        /// <summary>
        /// Renders the table given a data context
        /// </summary>
        /// <param name="context">the data context</param>
        /// <returns>the console friendly table, as a ConsoleString</returns>
        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 argument && argument.OmitFromUsage) || (element is CommandLineAction action && action.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);
        }
 public ConsoleString Evaluate(DocumentRendererContext context)
 {
     return impl(context);
 }
Example #9
0
 /// <summary>
 /// Evaluates the conditional expression against the given data context, rendering the body only if it is false.
 /// </summary>
 /// <param name="context">The data context to use when evaluating the conditional expression</param>
 /// <returns>The rendered body if the conditional was false, an empty string otherwise</returns>
 public override ConsoleString Evaluate(DocumentRendererContext context)
 {
     var eval = context.EvaluateExpression(this.IfExpressionToken.Value);
     if (false.Equals(eval) == true || 0.Equals(eval))
     {
         return context.RenderBody(this.Body);
     }
     else
     {
         return ConsoleString.Empty;
     }
 }
Example #10
0
        private ConsoleString Evaluate(List<IDocumentExpression> expressions, DocumentRendererContext context)
        {
            ConsoleString ret = new ConsoleString();

            foreach (var expression in expressions)
            {
                var eval = expression.Evaluate(context);
                ret += eval;
            }

            return ret;
        }
        /// <summary>
        /// Renders the table given a data context
        /// </summary>
        /// <param name="context">the data context</param>
        /// <returns>the console friendly table, as a ConsoleString</returns>
        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;
        }
Example #12
0
 internal ConsoleString Render(IEnumerable<DocumentToken> tokens, DocumentRendererContext context)
 {
     var expressions = expressionParser.Parse(tokens);
     var ret = Evaluate(expressions, context);
     return ret;
 }
Example #13
0
 internal ConsoleString Render(string template, DocumentRendererContext context, string sourceFileLocation = null)
 {
     List<DocumentToken> tokens = DocumentToken.Tokenize(template, sourceFileLocation);
     return Render(tokens, context);
 }
        internal ConsoleString Render(string template, DocumentRendererContext context, string sourceFileLocation = null)
        {
            List <DocumentToken> tokens = DocumentToken.Tokenize(template, sourceFileLocation);

            return(Render(tokens, context));
        }
Example #15
0
 /// <summary>
 /// Removes the named variable from the context's local variable set
 /// </summary>
 /// <param name="context">the context that should contain the local variable to remove</param>
 /// <returns>an empty string</returns>
 public ConsoleString Evaluate(DocumentRendererContext context)
 {
     context.LocalVariables.Remove(NameToken);
     return(ConsoleString.Empty);
 }
 internal ConsoleString Render(List<DocumentToken> tokens, DocumentRendererContext context)
 {
     var expressions = expressionParser.Parse(tokens);
     var ret = Evaluate(expressions, context);
     return ret;
 }