Skip to content

Commit

Permalink
添加中文支持 (Mpdreamz#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teastears authored Aug 25, 2021
1 parent 10814f4 commit f25b0ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/ShellProgressBar/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private static void CondensedProgressBar(
var truncatedMessage = StringExtensions.Excerpt(message, messageWidth - 2) + " ";
var width = (Console.WindowWidth - (depth * 2) + 2) - truncatedMessage.Length;

if (!string.IsNullOrWhiteSpace(ProgressBarOptions.ProgressMessageEncodingName))
{
width = width + message.Length - System.Text.Encoding.GetEncoding(ProgressBarOptions.ProgressMessageEncodingName).GetBytes(message).Length;
}

var newWidth = (int) ((width * percentage) / 100d);
var progBar = new string(progressCharacter, newWidth);
Expand Down Expand Up @@ -180,6 +184,10 @@ private static void ProgressBarBottomHalf(double percentage, DateTime startDate,
var column1Width = Console.WindowWidth - durationString.Length - (depth * 2) + 2;
var column2Width = durationString.Length;

if (!string.IsNullOrWhiteSpace(ProgressBarOptions.ProgressMessageEncodingName))
{
column1Width = column1Width + message.Length - System.Text.Encoding.GetEncoding(ProgressBarOptions.ProgressMessageEncodingName).GetBytes(message).Length;
}

if (progressBarOnBottom)
DrawTopHalfPrefix(indentation, depth);
Expand Down
16 changes: 15 additions & 1 deletion src/ShellProgressBar/ProgressBarOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public class ProgressBarOptions
private bool _enableTaskBarProgress;
public static readonly ProgressBarOptions Default = new ProgressBarOptions();

public static string ProgressMessageEncodingName { get; set; }

public string MessageEncodingName
{
get
{
return ProgressMessageEncodingName;
}
set
{
ProgressMessageEncodingName = value;
}
}

/// <summary> The foreground color of the progress bar, message and time</summary>
public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.Green;

Expand Down Expand Up @@ -86,7 +100,7 @@ public bool EnableTaskBarProgress
}

/// <summary>
/// Take ownership of writing a message that is intended to be displayed above the progressbar.
/// Take ownership of writing a message that is intended to be displayed above the progressbar.
/// The delegate is expected to return the number of messages written to the console as a result of the string argument.
/// <para>Use case: pretty print or change the console colors, the progressbar will reset back</para>
/// </summary>
Expand Down

0 comments on commit f25b0ba

Please sign in to comment.