From 0281a0db7b16921d661fd64280b99e804ffaaeab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BA=B7=E5=AF=85=E5=BC=BA?= <86541947@qq.com>
Date: Fri, 19 Feb 2021 18:44:23 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=AD=E6=96=87=E6=94=AF?=
=?UTF-8?q?=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/ShellProgressBar/ProgressBar.cs | 8 ++++++++
src/ShellProgressBar/ProgressBarOptions.cs | 16 +++++++++++++++-
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/src/ShellProgressBar/ProgressBar.cs b/src/ShellProgressBar/ProgressBar.cs
index 4262541..4198c0f 100644
--- a/src/ShellProgressBar/ProgressBar.cs
+++ b/src/ShellProgressBar/ProgressBar.cs
@@ -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);
@@ -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);
diff --git a/src/ShellProgressBar/ProgressBarOptions.cs b/src/ShellProgressBar/ProgressBarOptions.cs
index 6fed7f0..d13a4b1 100644
--- a/src/ShellProgressBar/ProgressBarOptions.cs
+++ b/src/ShellProgressBar/ProgressBarOptions.cs
@@ -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;
+ }
+ }
+
/// The foreground color of the progress bar, message and time
public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.Green;
@@ -83,7 +97,7 @@ public bool EnableTaskBarProgress
}
///
- /// 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.
/// Use case: pretty print or change the console colors, the progressbar will reset back
///