Skip to content

Commit 2b7b7a4

Browse files
committed
终端进度条
1 parent 2bc2be1 commit 2b7b7a4

File tree

5 files changed

+77
-11
lines changed

5 files changed

+77
-11
lines changed

MaiChartManager.CLI/Commands/MakeAcbCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Spectre.Console;
33
using Spectre.Console.Cli;
44
using System.ComponentModel;
5+
using MaiChartManager.CLI.Utils;
56

67
namespace MaiChartManager.CLI.Commands;
78

@@ -74,6 +75,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
7475
private async Task ConvertSingleFile(string source, string output, Settings settings)
7576
{
7677
AnsiConsole.MarkupLine($"[yellow]正在转换:[/] {Path.GetFileName(source)}{Path.GetFileName(output)}.acb");
78+
TerminalProgress.Set(TerminalProgress.Status.Indeterminate);
7779

7880
await AnsiConsole.Status()
7981
.Spinner(Spinner.Known.Dots)
@@ -89,6 +91,7 @@ await Task.Run(() =>
8991
});
9092
});
9193

94+
TerminalProgress.Clear();
9295
AnsiConsole.MarkupLine($"[green]✓ 已保存到: {output}[/]");
9396
}
9497

@@ -103,12 +106,22 @@ await AnsiConsole.Progress()
103106
new SpinnerColumn())
104107
.StartAsync(async ctx =>
105108
{
109+
int doneCount = 0, errorCount = 0;
106110
foreach (var source in settings.Sources)
107111
{
108112
var output = Path.ChangeExtension(source, null);
109113
var task = ctx.AddTask($"[green]{Path.GetFileName(source)}[/]");
110114
task.MaxValue = 100;
111115

116+
if (errorCount > 0)
117+
{
118+
TerminalProgress.Set(TerminalProgress.Status.Warning, (errorCount + doneCount) * 100 / settings.Sources.Length);
119+
}
120+
else
121+
{
122+
TerminalProgress.Set(doneCount * 100 / settings.Sources.Length);
123+
}
124+
112125
try
113126
{
114127
task.StartTask();
@@ -120,15 +133,18 @@ await Task.Run(() =>
120133
padding: settings.Padding
121134
);
122135
});
136+
doneCount++;
123137

124138
task.StopTask();
125139
}
126140
catch (Exception ex)
127141
{
142+
errorCount++;
128143
task.Description = $"[red]{Path.GetFileName(source)} - 失败[/]";
129144
task.StopTask();
130145
}
131146
}
147+
TerminalProgress.Clear();
132148
});
133149
}
134150
}

MaiChartManager.CLI/Commands/MakeUsmCommand.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Spectre.Console;
33
using Spectre.Console.Cli;
44
using System.ComponentModel;
5+
using MaiChartManager.CLI.Utils;
56

67
namespace MaiChartManager.CLI.Commands;
78

@@ -59,7 +60,9 @@ await AnsiConsole.Status()
5960
.Spinner(Spinner.Known.Dots)
6061
.StartAsync("正在检测硬件加速...", async ctx =>
6162
{
63+
TerminalProgress.Set(TerminalProgress.Status.Indeterminate);
6264
await VideoConvert.CheckHardwareAcceleration();
65+
TerminalProgress.Clear();
6366
});
6467

6568
AnsiConsole.MarkupLine($"[green]硬件加速: {VideoConvert.HardwareAcceleration}[/]");
@@ -101,15 +104,20 @@ await AnsiConsole.Progress()
101104
{
102105
var task = ctx.AddTask($"[green]转换 {Path.GetFileName(source)}[/]");
103106
task.MaxValue = 100;
107+
TerminalProgress.Set(TerminalProgress.Status.Indeterminate);
104108

105109
await VideoConvert.ConvertVideoToUsm(
106110
source,
107111
output,
108112
noScale: settings.NoScale,
109113
yuv420p: settings.UseYuv420p,
110-
onProgress: percent => task.Value = percent
111-
);
114+
onProgress: percent =>
115+
{
116+
TerminalProgress.Set(percent);
117+
task.Value = percent;
118+
});
112119

120+
TerminalProgress.Clear();
113121
task.Value = 100;
114122
});
115123

@@ -129,12 +137,22 @@ await AnsiConsole.Progress()
129137
new SpinnerColumn())
130138
.StartAsync(async ctx =>
131139
{
140+
int doneCount = 0, errorCount = 0;
132141
foreach (var source in settings.Sources)
133142
{
134143
var output = Path.ChangeExtension(source, ".dat");
135144
var task = ctx.AddTask($"[green]{Path.GetFileName(source)}[/]");
136145
task.MaxValue = 100;
137146

147+
if (errorCount > 0)
148+
{
149+
TerminalProgress.Set(TerminalProgress.Status.Warning, (errorCount + doneCount) * 100 / settings.Sources.Length);
150+
}
151+
else
152+
{
153+
TerminalProgress.Set(doneCount * 100 / settings.Sources.Length);
154+
}
155+
138156
try
139157
{
140158
await VideoConvert.ConvertVideoToUsm(
@@ -145,17 +163,18 @@ await VideoConvert.ConvertVideoToUsm(
145163
onProgress: percent => task.Value = percent
146164
);
147165

166+
doneCount++;
148167
task.Value = 100;
149-
AnsiConsole.MarkupLine($"[green]✓ {Path.GetFileName(source)}{Path.GetFileName(output)}[/]");
150168
}
151169
catch (Exception ex)
152170
{
171+
errorCount++;
153172
task.Description = $"[red]{Path.GetFileName(source)} - 失败[/]";
154173
task.Value = 100;
155174
task.StopTask();
156-
AnsiConsole.MarkupLine($"[red]✗ 转换失败 {Path.GetFileName(source)}: {ex.Message}[/]");
157175
}
158176
}
177+
TerminalProgress.Clear();
159178
});
160179
}
161180
}

MaiChartManager.CLI/MaiChartManager.CLI.csproj

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@
88
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
99
<SelfContained>true</SelfContained>
1010
<ValidateExecutableReferencesMatchSelfContained>False</ValidateExecutableReferencesMatchSelfContained>
11+
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
12+
<CETCompat>false</CETCompat>
13+
<PublishTrimmed>False</PublishTrimmed>
14+
<PublishAot>False</PublishAot>
15+
<PublishReadyToRun>true</PublishReadyToRun>
1116
</PropertyGroup>
1217

1318
<ItemGroup>
1419
<ProjectReference Include="..\MaiChartManager\MaiChartManager.csproj"/>
1520
</ItemGroup>
1621

17-
<ItemGroup>
18-
<Reference Include="System.Console">
19-
<HintPath>..\MaiChartManager\bin\x64\Debug\net9.0-windows10.0.17763.0\win-x64\System.Console.dll</HintPath>
20-
</Reference>
21-
</ItemGroup>
22-
2322
<ItemGroup>
2423
<PackageReference Include="Spectre.Console.Cli" Version="0.53.0"/>
25-
<PackageReference Include="Unknown6656.Terminal" Version="1.5.434.8968"/>
2624
</ItemGroup>
2725

2826
</Project>

MaiChartManager.CLI/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Text;
22
using MaiChartManager.CLI.Commands;
3+
using MaiChartManager.CLI.Utils;
34
using Spectre.Console.Cli;
45

56
Console.OutputEncoding = Encoding.UTF8;
7+
Console.CancelKeyPress += (_, _) => TerminalProgress.Clear();
68

79
var app = new CommandApp();
810

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace MaiChartManager.CLI.Utils;
2+
3+
/// <summary>
4+
/// https://learn.microsoft.com/zh-cn/windows/terminal/tutorials/progress-bar-sequences
5+
/// </summary>
6+
public static class TerminalProgress
7+
{
8+
public enum Status
9+
{
10+
Hidden,
11+
Default,
12+
Error,
13+
Indeterminate,
14+
Warning,
15+
}
16+
17+
public static void Set(Status status = Status.Default, int progress = 0)
18+
{
19+
Console.Write($"\e]9;4;{status:D};{progress}\a");
20+
}
21+
22+
public static void Set(int progress)
23+
{
24+
Set(Status.Default, progress);
25+
}
26+
27+
public static void Clear()
28+
{
29+
Set(Status.Hidden);
30+
}
31+
}

0 commit comments

Comments
 (0)