控制台进度条

一、进度条 progress bar

  1. 安装 Nuget
    # 安装 Nuget 包
    $ NuGet\Install-Package ShellProgressBar -Version 5.2.0
    
  2. 进度条
     // 总数
     int total = 100;
    
     // 进度条
     using var pbar = new ProgressBar(total, $"(1/{total}) part(1) count({total})", new ProgressBarOptions
     {
         ForegroundColor = ConsoleColor.Blue,
         BackgroundColor = ConsoleColor.DarkBlue,
         ProgressCharacter = '─',
         CollapseWhenFinished = false
     });
    
     // 记录进度
     for (int i = 1; i <= total; i++)
     {
         pbar.Tick($"({i}/{100})");
     }
    
  3. 下级进度条
     // 总数
     int total = 100;
    
     // 子进度总数
     int child = 50;
    
     using (var pbar = new ProgressBar(total, $"(1/{total}) part(1) count({total})", new ProgressBarOptions
     {
         ForegroundColor = ConsoleColor.Blue,
         BackgroundColor = ConsoleColor.DarkBlue,
         ProgressCharacter = '─',
         CollapseWhenFinished = false
     }))
     {
    
         for (int i = 1; i <= total; i++)
         {
             for (int k = 1; k <= child; k++)
             {
                 using var childBar = pbar.Spawn(1, $"{k}", new ProgressBarOptions
                 {
                     ForegroundColor = ConsoleColor.Green,
                     BackgroundColor = ConsoleColor.DarkGreen,
                     ProgressCharacter = '─',
                     CollapseWhenFinished = false
                 });
    
                 childBar.Tick();
                 pbar.Tick($"({i}/{100}) ");
             }
         }
     }
    

二、参考

  1. [官网]
posted @ 2024-03-18 14:41  1764564459  阅读(27)  评论(0编辑  收藏  举报