-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
48 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace SonicLair.Cli.Tools | ||
{ | ||
public static class StringExtension | ||
{ | ||
public static int StandardizedStringLength(this string input) | ||
{ | ||
// each CJK rune is 2 letter width when using a monospaced font | ||
int length = 0; | ||
foreach (var c in input) | ||
{ | ||
length += Rune.IsWideChar(c) ? 2 : 1; | ||
} | ||
return length; | ||
} | ||
public static string RunePadRight(this string stringToPad, int totalWidth, char paddingChar = ' ') | ||
{ | ||
int standardizedStringLength = stringToPad.StandardizedStringLength(); | ||
if (standardizedStringLength >= totalWidth) | ||
{ | ||
return stringToPad; | ||
} | ||
return stringToPad.PadRight(totalWidth - (standardizedStringLength - stringToPad.Length), paddingChar); | ||
} | ||
public static string RunePadLeft(this string stringToPad, int totalWidth, char paddingChar = ' ') | ||
{ | ||
int standardizedStringLength = stringToPad.StandardizedStringLength(); | ||
if (standardizedStringLength >= totalWidth) | ||
{ | ||
return stringToPad; | ||
} | ||
return stringToPad.PadLeft(totalWidth - (standardizedStringLength - stringToPad.Length), paddingChar); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters