Refactor ToString() on some models

This commit is contained in:
Alexey Golub
2019-08-18 01:11:15 +03:00
parent 7d7edaf30f
commit 5a696c181b
4 changed files with 7 additions and 21 deletions

View File

@@ -23,16 +23,8 @@ namespace CliFx.Internal
return index < 0 ? s : s.Substring(0, index);
}
public static StringBuilder Trim(this StringBuilder builder)
{
while (builder.Length > 0 && char.IsWhiteSpace(builder[0]))
builder.Remove(0, 1);
while (builder.Length > 0 && char.IsWhiteSpace(builder[builder.Length - 1]))
builder.Remove(builder.Length - 1, 1);
return builder;
}
public static StringBuilder AppendIfEmpty(this StringBuilder builder, char value) =>
builder.Length == 0 ? builder.Append(value) : builder;
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
dic.TryGetValue(key, out var result) ? result : default;

View File

@@ -59,18 +59,15 @@ namespace CliFx.Models
var buffer = new StringBuilder();
if (!CommandName.IsNullOrWhiteSpace())
{
buffer.Append(CommandName);
buffer.Append(' ');
}
foreach (var option in Options)
{
buffer.AppendIfEmpty(' ');
buffer.Append(option);
buffer.Append(' ');
}
return buffer.Trim().ToString();
return buffer.ToString();
}
}

View File

@@ -54,7 +54,7 @@ namespace CliFx.Models
foreach (var value in Values)
{
buffer.Append(' ');
buffer.AppendIfEmpty(' ');
var isEscaped = value.Contains(" ");

View File

@@ -47,20 +47,17 @@ namespace CliFx.Models
var buffer = new StringBuilder();
if (!Name.IsNullOrWhiteSpace())
{
buffer.Append(Name);
buffer.Append(' ');
}
foreach (var option in Options)
{
buffer.AppendIfEmpty(' ');
buffer.Append('[');
buffer.Append(option);
buffer.Append(']');
buffer.Append(' ');
}
return buffer.Trim().ToString();
return buffer.ToString();
}
}
}