示例#1
0
        private static IRenderable CreatePrettyMessage(string arguments, CommandTreeToken token, string message, string details)
        {
            var composer = new Composer();

            var position = token?.Position ?? 0;
            var value    = token?.Representation ?? arguments;

            // Header
            composer.LineBreak();
            composer.Style("red", "Error:");
            composer.Space().Text(message.SafeMarkup());
            composer.LineBreak();

            // Template
            composer.LineBreak();
            composer.Spaces(7).Text(arguments.SafeMarkup());

            // Error
            composer.LineBreak();
            composer.Spaces(7).Spaces(position);

            composer.Style("red", error =>
            {
                error.Repeat('^', value.Length);
                error.Space();
                error.Text(details.TrimEnd('.').SafeMarkup());
                error.LineBreak();
            });

            composer.LineBreak();

            return(composer);
        }
示例#2
0
        private static IEnumerable <IRenderable> GetExamples(CommandModel model, CommandInfo?command)
        {
            var maxExamples = int.MaxValue;

            var examples = command?.Examples ?? model.Examples ?? new List <string[]>();

            if (examples.Count == 0)
            {
                // Since we're not checking direct examples,
                // make sure that we limit the number of examples.
                maxExamples = 5;

                // Get the current root command.
                var root  = command ?? (ICommandContainer)model;
                var queue = new Queue <ICommandContainer>(new[] { root });

                // Traverse the command tree and look for examples.
                // As soon as a node contains commands, bail.
                while (queue.Count > 0)
                {
                    var current = queue.Dequeue();

                    foreach (var cmd in current.Commands)
                    {
                        if (cmd.Examples.Count > 0)
                        {
                            examples.AddRange(cmd.Examples);
                        }

                        queue.Enqueue(cmd);
                    }

                    if (examples.Count >= maxExamples)
                    {
                        break;
                    }
                }
            }

            if (examples.Count > 0)
            {
                var composer = new Composer();
                composer.Style("yellow", "EXAMPLES:").LineBreak();

                for (var index = 0; index < Math.Min(maxExamples, examples.Count); index++)
                {
                    var args = string.Join(" ", examples[index]);
                    composer.Tab().Text(model.GetApplicationName()).Space().Style("grey", args);
                    composer.LineBreak();
                }

                composer.LineBreak();
                return(new[] { composer });
            }

            return(Array.Empty <IRenderable>());
        }
示例#3
0
        private static IRenderable CreatePrettyMessage(string template, TemplateToken?token, string message, string details)
        {
            var composer = new Composer();

            var position = token?.Position ?? 0;
            var value    = token?.Representation ?? template;

            // Header
            composer.LineBreak();
            composer.Style("red", "Error:");
            composer.Space().Text("An error occured when parsing template.");
            composer.LineBreak();
            composer.Spaces(7).Style("yellow", message.SafeMarkup());
            composer.LineBreak();

            if (string.IsNullOrWhiteSpace(template))
            {
                // Error
                composer.LineBreak();
                composer.Style("red", message.SafeMarkup());
                composer.LineBreak();
            }
            else
            {
                // Template
                composer.LineBreak();
                composer.Spaces(7).Text(template.SafeMarkup());

                // Error
                composer.LineBreak();
                composer.Spaces(7).Spaces(position);
                composer.Style("red", error =>
                {
                    error.Repeat('^', value.Length);
                    error.Space();
                    error.Text(details.TrimEnd('.').SafeMarkup());
                    error.LineBreak();
                });
            }

            composer.LineBreak();

            return(composer);
        }