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.EscapeMarkup());
            composer.LineBreak();

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

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

            composer.LineBreak();

            return(composer);
        }
Ejemplo n.º 2
0
        private static IEnumerable <IRenderable> GetUsage(CommandModel model, CommandInfo?command)
        {
            var composer = new Composer();

            composer.Style("yellow", "USAGE:").LineBreak();
            composer.Tab().Text(model.GetApplicationName());

            var parameters = new List <string>();

            if (command == null)
            {
                parameters.Add("[grey][[OPTIONS]][/]");
                parameters.Add("[aqua]<COMMAND>[/]");
            }
            else
            {
                foreach (var current in command.Flatten())
                {
                    var isCurrent = current == command;

                    if (!current.IsDefaultCommand)
                    {
                        if (isCurrent)
                        {
                            parameters.Add($"[underline]{current.Name.EscapeMarkup()}[/]");
                        }
                        else
                        {
                            parameters.Add($"{current.Name.EscapeMarkup()}");
                        }
                    }

                    if (current.Parameters.OfType <CommandArgument>().Any())
                    {
                        if (isCurrent)
                        {
                            foreach (var argument in current.Parameters.OfType <CommandArgument>()
                                     .Where(a => a.Required).OrderBy(a => a.Position).ToArray())
                            {
                                parameters.Add($"[aqua]<{argument.Value.EscapeMarkup()}>[/]");
                            }
                        }

                        var optionalArguments = current.Parameters.OfType <CommandArgument>().Where(x => !x.Required).ToArray();
                        if (optionalArguments.Length > 0 || !isCurrent)
                        {
                            foreach (var optionalArgument in optionalArguments)
                            {
                                parameters.Add($"[silver][[{optionalArgument.Value.EscapeMarkup()}]][/]");
                            }
                        }
                    }

                    if (isCurrent)
                    {
                        parameters.Add("[grey][[OPTIONS]][/]");
                    }
                }

                if (command.IsBranch)
                {
                    parameters.Add("[aqua]<COMMAND>[/]");
                }
            }

            composer.Join(" ", parameters);
            composer.LineBreak();

            return(new[]
            {
                composer,
            });
        }