Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Chinese Support #82

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -83,7 +97,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