public void TestEmptyBuilderYieldsEmptyMultistring() { var builder = new ColoredMultistringBuilder(); var ms = builder.ToMultistring(); ms.Should().NotBeNull(); ms.IsEmpty().Should().BeTrue(); }
private ColoredMultistring Format(IEnumerable <Section> sections) { // Start composing the content. var builder = new ColoredMultistringBuilder(); // Generate and sequence sections. var firstSection = true; foreach (var section in sections) { if (!firstSection) { for (var i = 0; i < _options.BlankLinesBetweenSections; ++i) { builder.AppendLine(); } } // Add header. if ((_options.SectionHeaders?.Include ?? false) && !string.IsNullOrEmpty(section.Name)) { if (_options.UseColor) { builder.AppendLine(new ColoredString(section.Name, _options.SectionHeaders.Color)); } else { builder.AppendLine(section.Name); } } // Add content. foreach (var entry in section.Entries) { var text = (ColoredMultistring)entry.Wrap( _options.MaxWidth.GetValueOrDefault(ArgumentSetHelpOptions.DefaultMaxWidth), blockIndent: section.BodyIndentWidth, hangingIndent: section.HangingIndentWidth); if (_options.UseColor) { builder.AppendLine(text); } else { builder.AppendLine(text.ToString()); } } firstSection = false; } return(builder.ToMultistring()); }
public void TestAppendingOnePieceToEmptyBuilderYieldsBackCopyOfPiece() { var anyCs = AnyColoredString(); var builder = new ColoredMultistringBuilder(); builder.Append(anyCs); builder.Length.Should().Be(anyCs.Length); builder.ToString().Should().Be(anyCs.ToString()); var ms = builder.ToMultistring(); ms.Should().NotBeNull(); ms.Length.Should().Be(anyCs.Length); ms.Content.Should().ContainSingle(anyCs); }
protected ColoredMultistring Format(IEnumerable <Section> sections) { // Start composing the content. var builder = new ColoredMultistringBuilder(); // Generate and sequence sections. var firstSection = true; foreach (var section in sections) { if (!firstSection) { builder.AppendLine(); } // Add header. if (!string.IsNullOrEmpty(section.Name)) { var header = new ColoredString(section.Name, UseColor ? new ConsoleColor?(HeaderForegroundColor) : null); builder.AppendLine(header); } // Add content. foreach (var entry in section.Entries) { var text = (ColoredMultistring)entry.Wrap( MaxWidth, indent: section.BodyIndentWidth, hangingIndent: section.HangingIndentWidth); if (UseColor) { builder.AppendLine(text); } else { builder.AppendLine(text.ToString()); } } firstSection = false; } return(builder.ToMultistring()); }
private ColoredMultistring FormatEnumValueInfo(IArgumentValue value) { var builder = new ColoredMultistringBuilder(); builder.Append(new ColoredString(value.DisplayName, ParameterNameForegroundColor)); if (!string.IsNullOrEmpty(value.Description)) { builder.Append(" - "); builder.Append(value.Description); } if (!string.IsNullOrEmpty(value.ShortName)) { builder.Append(" ["); builder.Append(new ColoredString(Strings.UsageInfoShortForm, ParameterMetadataForegroundColor)); builder.Append(" "); builder.Append(new ColoredString(value.ShortName, NameForegroundColor)); builder.Append("]"); } return(builder.ToMultistring()); }
private ParameterEntry GetEnumValueInfo(IArgumentValue value) { var syntaxBuilder = new ColoredMultistringBuilder(); if (_options.Arguments.ShortName == ArgumentShortNameHelpMode.IncludeWithLongName && !string.IsNullOrEmpty(value.ShortName)) { syntaxBuilder.Append(new ColoredString(value.ShortName, _options.Arguments?.ArgumentNameColor)); syntaxBuilder.Append(", "); } syntaxBuilder.Append(new ColoredString(value.DisplayName, _options.Arguments?.ArgumentNameColor)); var descBuilder = new ColoredMultistringBuilder(); if (!string.IsNullOrEmpty(value.Description)) { descBuilder.Append(value.Description); } if (_options.Arguments.ShortName == ArgumentShortNameHelpMode.AppendToDescription && !string.IsNullOrEmpty(value.ShortName)) { descBuilder.Append(" ["); descBuilder.Append(new ColoredString(Strings.UsageInfoShortForm, _options.Arguments.MetadataColor)); descBuilder.Append(" "); descBuilder.Append(new ColoredString(value.ShortName, _options.Arguments?.ArgumentNameColor)); descBuilder.Append("]"); } return(new ParameterEntry { Syntax = syntaxBuilder.ToMultistring(), Description = descBuilder.ToMultistring(), InlineEnumEntries = null }); }
private IEnumerable <IEnumerable <ColoredMultistring> > FormatParameterEntriesInOneColumn( IReadOnlyList <ParameterEntry> entries) => entries.Select(e => { var builder = new ColoredMultistringBuilder(); builder.Append(e.Syntax); if (!e.Description.IsEmpty()) { builder.Append(" - "); builder.Append(e.Description); } IEnumerable <ColoredMultistring> composed = new[] { builder.ToMultistring() }; if (e.InlineEnumEntries != null) { if (_options.Arguments.BlankLinesBetweenArguments > 0) { var insertion = new[] { new ColoredMultistring( Enumerable.Repeat(new ColoredString(Environment.NewLine), _options.Arguments.BlankLinesBetweenArguments)) }; composed = composed.Concat(insertion); } // TODO: Let section hanging indent override. var indent = new string(' ', _options.SectionEntryHangingIndentWidth); composed = composed.Concat(e.InlineEnumEntries.Select(iee => indent + iee)); } return(composed); });
private IEnumerable <ColoredMultistring> FormatParameterInfo( ArgumentUsageInfo info, ArgumentSetUsageInfo setInfo, List <IEnumArgumentType> inlineDocumentedEnums) { var builder = new ColoredMultistringBuilder(); var syntax = SimplifyParameterSyntax(info.DetailedSyntax); builder.Append(new ColoredString(syntax, ParameterNameForegroundColor)); if (!string.IsNullOrEmpty(info.Description)) { builder.Append(" - "); builder.Append(info.Description); } var metadataItems = new List <List <ColoredString> >(); if (Options.HasFlag(UsageInfoOptions.IncludeParameterDefaultValues) && !string.IsNullOrEmpty(info.DefaultValue)) { metadataItems.Add(new List <ColoredString> { new ColoredString(Strings.UsageInfoDefaultValue, ParameterMetadataForegroundColor), " ", info.DefaultValue }); } // Append parameter's short name (if it has one). if (Options.HasFlag(UsageInfoOptions.IncludeParameterShortNameAliases) && !string.IsNullOrEmpty(info.ShortName) && !string.IsNullOrEmpty(setInfo.DefaultShortNamePrefix)) { metadataItems.Add(new List <ColoredString> { new ColoredString(Strings.UsageInfoShortForm, ParameterMetadataForegroundColor), " ", new ColoredString(setInfo.DefaultShortNamePrefix + info.ShortName, NameForegroundColor) }); } if (metadataItems.Count > 0) { builder.Append(" ["); builder.Append(metadataItems.InsertBetween(new List <ColoredString> { ", " }).Flatten()); builder.Append("]"); } var formattedInfo = new List <ColoredMultistring> { builder.ToMultistring() }; if (inlineDocumentedEnums?.Count > 0) { foreach (var entry in inlineDocumentedEnums.SelectMany(GetEnumValueEntries)) { var indentedEntry = new ColoredMultistring(new ColoredString[] { new string(' ', Section.DefaultIndent) }.Concat(entry.Content)); formattedInfo.Add(indentedEntry); } } return(formattedInfo); }
private ColoredMultistring FormatParameterDescription( ArgumentUsageInfo info, ArgumentSetUsageInfo setInfo) { var builder = new ColoredMultistringBuilder(); List <ColoredString> defaultValueContent = null; if (_options.Arguments.DefaultValue != ArgumentDefaultValueHelpMode.Omit && !string.IsNullOrEmpty(info.DefaultValue)) { defaultValueContent = new List <ColoredString> { new ColoredString(Strings.UsageInfoDefaultValue, _options.Arguments.MetadataColor), " ", info.DefaultValue }; } if (_options.Arguments.DefaultValue == ArgumentDefaultValueHelpMode.PrependToDescription && defaultValueContent != null) { builder.Append("["); builder.Append(defaultValueContent); builder.Append("]"); } if (_options.Arguments.IncludeDescription && !string.IsNullOrEmpty(info.Description)) { if (builder.Length > 0) { builder.Append(" "); } builder.Append(info.Description); } var metadataItems = new List <List <ColoredString> >(); if (_options.Arguments.DefaultValue == ArgumentDefaultValueHelpMode.AppendToDescription && defaultValueContent != null) { metadataItems.Add(defaultValueContent); } // Append parameter's short name (if it has one). if (_options.Arguments.ShortName == ArgumentShortNameHelpMode.AppendToDescription && !string.IsNullOrEmpty(info.ShortName) && !string.IsNullOrEmpty(setInfo.DefaultShortNamePrefix)) { metadataItems.Add(new List <ColoredString> { new ColoredString(Strings.UsageInfoShortForm, _options.Arguments.MetadataColor), " ", new ColoredString(setInfo.DefaultShortNamePrefix + info.ShortName, _options.Arguments?.ArgumentNameColor) }); } if (metadataItems.Count > 0) { if (builder.Length > 0) { builder.Append(" "); } builder.Append("["); builder.Append(metadataItems.InsertBetween(new List <ColoredString> { ", " }).Flatten()); builder.Append("]"); } return(builder.ToMultistring()); }
public static void ShouldProduce(this ColoredMultistringBuilder builder, params ColoredString[] pieces) => builder.ToMultistring().Content.Should().Equal(pieces);
public static void ShouldProduce(this ColoredMultistringBuilder builder, IEnumerable <ColoredString> pieces) => builder.ToMultistring().Content.Should().Equal(pieces);