mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor ToString() on some models
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace CliFx.Models
|
||||
|
||||
foreach (var value in Values)
|
||||
{
|
||||
buffer.Append(' ');
|
||||
buffer.AppendIfEmpty(' ');
|
||||
|
||||
var isEscaped = value.Contains(" ");
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user