mirror of
https://github.com/spectreconsole/spectre.console.git
synced 2025-10-25 15:19:23 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e280b82679 | ||
|
|
6932c95731 | ||
|
|
ee305702e8 | ||
|
|
63abcc92ba | ||
|
|
acf01e056f | ||
|
|
501db5d287 | ||
|
|
cbed41e637 | ||
|
|
3c504155bc | ||
|
|
ae32785f21 | ||
|
|
c61e386440 | ||
|
|
b7cd7dd53e | ||
|
|
3e1251b86a |
28
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
28
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Information**
|
||||
- OS: [eg Windows/Linux/MacOS]
|
||||
- Version: [e.g. 0.33.0]
|
||||
- Terminal: [e.g Windows Terminal]
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior.
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: enhancement
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
9
.github/workflows/ci.yaml
vendored
9
.github/workflows/ci.yaml
vendored
@@ -69,14 +69,7 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
dotnet tool restore
|
||||
dotnet example info
|
||||
dotnet example tables
|
||||
dotnet example grids
|
||||
dotnet example panels
|
||||
dotnet example colors
|
||||
dotnet example emojis
|
||||
dotnet example exceptions
|
||||
dotnet example calendars
|
||||
dotnet example --all
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
|
||||
65
README.zh.md
Normal file
65
README.zh.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# `Spectre.Console`
|
||||
|
||||
_[](https://www.nuget.org/packages/spectre.console)_
|
||||
|
||||
`Spectre.Console`是一个 .NET 5/.NET Standard 2.0 的库,能让您在终端里更方便地生成精美的界面。
|
||||
|
||||
深受 [Rich](https://github.com/willmcgugan/rich) 这个优秀库的启发。
|
||||
|
||||
## 目录
|
||||
|
||||
1. [功能](#features)
|
||||
2. [安装](#installing)
|
||||
3. [文档](#documentation)
|
||||
4. [例子](#examples)
|
||||
5. [License](#license)
|
||||
|
||||
## 功能
|
||||
|
||||
* 编写时考虑到了单元测试。
|
||||
* 支持 tables、grid、panel 和 [rich](https://github.com/willmcgugan/rich) 所支持的标记语言。
|
||||
* 支持大部分的 SRG 参数,包括粗体、暗淡字、斜体、下划线、删除线和闪烁文本。
|
||||
* 支持终端显示 3/4/8/24 位色。自动检测终端类型,自适应颜色范围。
|
||||
|
||||

|
||||
|
||||
## 安装
|
||||
|
||||
最快的安装方式,就是用NuGet包管理直接安装Spectre.Console。
|
||||
|
||||
```csharp
|
||||
dotnet add package Spectre.Console
|
||||
```
|
||||
|
||||
## 文档
|
||||
|
||||
`Spectre.Console`的文档可以在这里查看
|
||||
https://spectresystems.github.io/spectre.console/
|
||||
|
||||
## 例子
|
||||
|
||||
如果想直接运行`Spectre.Console`的例子,则需要安装[dotnet-example](https://github.com/patriksvensson/dotnet-example)工具。
|
||||
|
||||
```
|
||||
> dotnet tool restore
|
||||
```
|
||||
|
||||
然后你可以列出仓库里的所有例子:
|
||||
|
||||
```
|
||||
> dotnet example
|
||||
```
|
||||
|
||||
跑一个看看效果:
|
||||
|
||||
```
|
||||
> dotnet example tables
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
版权所有 © Spectre Systems。
|
||||
|
||||
Spectre.Console 基于 MIT 协议提供。查看 LICENSE 文件了解更多信息。
|
||||
|
||||
* SixLabors.ImageSharp 的协议请查看 https://github.com/SixLabors/ImageSharp/blob/master/LICENSE
|
||||
43
docs/input/appendix/spinners.md
Normal file
43
docs/input/appendix/spinners.md
Normal file
@@ -0,0 +1,43 @@
|
||||
Title: Spinners
|
||||
Order: 4
|
||||
---
|
||||
|
||||
For all available spinners, see https://jsfiddle.net/sindresorhus/2eLtsbey/embedded/result/
|
||||
|
||||
# Usage
|
||||
|
||||
Spinners can be used with [Progress](xref:progress) and [Status](xref:status).
|
||||
|
||||
```csharp
|
||||
AnsiConsole.Status()
|
||||
.Spinner(Spinner.Known.Star)
|
||||
.Start("Thinking...", ctx => {
|
||||
// Omitted
|
||||
});
|
||||
```
|
||||
|
||||
# Implementing a spinner
|
||||
|
||||
To implement your own spinner, all you have to do is
|
||||
inherit from the `Spinner` base class.
|
||||
|
||||
In the example below, the spinner will alterate between
|
||||
the characters `A`, `B` and `C` every 100 ms.
|
||||
|
||||
```csharp
|
||||
public sealed class MySpinner : Spinner
|
||||
{
|
||||
// The interval for each frame
|
||||
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
|
||||
|
||||
// Whether or not the spinner contains unicode characters
|
||||
public override bool IsUnicode => false;
|
||||
|
||||
// The individual frames of the spinner
|
||||
public override IReadOnlyList<string> Frames =>
|
||||
new List<string>
|
||||
{
|
||||
"A", "B", "C",
|
||||
};
|
||||
}
|
||||
```
|
||||
BIN
docs/input/assets/images/progress.gif
Normal file
BIN
docs/input/assets/images/progress.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 164 KiB |
BIN
docs/input/assets/images/progress.png
Normal file
BIN
docs/input/assets/images/progress.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
BIN
docs/input/assets/images/progress_fallback.png
Normal file
BIN
docs/input/assets/images/progress_fallback.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
BIN
docs/input/assets/images/status.gif
Normal file
BIN
docs/input/assets/images/status.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 257 KiB |
@@ -31,5 +31,4 @@ $(document).ready(function () {
|
||||
}; // keyup
|
||||
})
|
||||
|
||||
|
||||
}); // ready
|
||||
78
docs/input/progress.md
Normal file
78
docs/input/progress.md
Normal file
@@ -0,0 +1,78 @@
|
||||
Title: Progress
|
||||
Order: 5
|
||||
---
|
||||
|
||||
Spectre.Console can display information about long running tasks in the console.
|
||||
|
||||
<img src="assets/images/progress.png" style="max-width: 100%;margin-bottom:20px;">
|
||||
|
||||
If the current terminal isn't considered "interactive", such as when running
|
||||
in a continuous integration system, or the terminal can't display
|
||||
ANSI control sequence, any progress will be displayed in a simpler way.
|
||||
|
||||
<img src="assets/images/progress_fallback.png" style="max-width: 100%;">
|
||||
|
||||
# Usage
|
||||
|
||||
```csharp
|
||||
// Synchronous
|
||||
AnsiConsole.Progress()
|
||||
.Start(ctx =>
|
||||
{
|
||||
// Define tasks
|
||||
var task1 = ctx.AddTask("[green]Reticulating splines[/]");
|
||||
var task2 = ctx.AddTask("[green]Folding space[/]");
|
||||
|
||||
while(!ctx.IsFinished)
|
||||
{
|
||||
task1.Increment(1.5);
|
||||
task2.Increment(0.5);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## Asynchronous progress
|
||||
|
||||
If you prefer to use async/await, you can use `StartAsync` instead of `Start`.
|
||||
|
||||
```csharp
|
||||
// Asynchronous
|
||||
await AnsiConsole.Progress()
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
// Define tasks
|
||||
var task1 = ctx.AddTask("[green]Reticulating splines[/]");
|
||||
var task2 = ctx.AddTask("[green]Folding space[/]");
|
||||
|
||||
while (!ctx.IsFinished)
|
||||
{
|
||||
// Simulate some work
|
||||
await Task.Delay(250);
|
||||
|
||||
// Increment
|
||||
task1.Increment(1.5);
|
||||
task2.Increment(0.5);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
# Configure
|
||||
|
||||
```csharp
|
||||
// Asynchronous
|
||||
AnsiConsole.Progress()
|
||||
.AutoRefresh(false) // Turn off auto refresh
|
||||
.AutoClear(false) // Do not remove the task list when done
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(), // Task description
|
||||
new ProgressBarColumn(), // Progress bar
|
||||
new PercentageColumn(), // Percentage
|
||||
new RemainingTimeColumn(), // Remaining time
|
||||
new SpinnerColumn(), // Spinner
|
||||
})
|
||||
.Start(ctx =>
|
||||
{
|
||||
// Omitted
|
||||
});
|
||||
```
|
||||
60
docs/input/status.md
Normal file
60
docs/input/status.md
Normal file
@@ -0,0 +1,60 @@
|
||||
Title: Status
|
||||
Order: 6
|
||||
---
|
||||
|
||||
Spectre.Console can display information about long running tasks in the console.
|
||||
|
||||
<img src="assets/images/status.gif" style="max-width: 100%;margin-bottom:20px;">
|
||||
|
||||
If the current terminal isn't considered "interactive", such as when running
|
||||
in a continuous integration system, or the terminal can't display
|
||||
ANSI control sequence, any progress will be displayed in a simpler way.
|
||||
|
||||
# Usage
|
||||
|
||||
```csharp
|
||||
// Synchronous
|
||||
AnsiConsole.Status()
|
||||
.Start("Thinking...", ctx =>
|
||||
{
|
||||
// Simulate some work
|
||||
AnsiConsole.MarkupLine("Doing some work...");
|
||||
Thread.Sleep(1000);
|
||||
|
||||
// Update the status and spinner
|
||||
ctx.Status("Thinking some more");
|
||||
ctx.Spinner(Spinner.Known.Star);
|
||||
ctx.SpinnerStyle(Style.Parse("green"));
|
||||
|
||||
// Simulate some work
|
||||
AnsiConsole.MarkupLine("Doing some more work...");
|
||||
Thread.Sleep(2000);
|
||||
});
|
||||
```
|
||||
|
||||
## Asynchronous progress
|
||||
|
||||
If you prefer to use async/await, you can use `StartAsync` instead of `Start`.
|
||||
|
||||
```csharp
|
||||
// Asynchronous
|
||||
await AnsiConsole.Status()
|
||||
.StartAsync("Thinking...", async ctx =>
|
||||
{
|
||||
// Omitted
|
||||
});
|
||||
```
|
||||
|
||||
# Configure
|
||||
|
||||
```csharp
|
||||
AnsiConsole.Status()
|
||||
.AutoRefresh(false)
|
||||
.Spinner(Spinner.Known.Star)
|
||||
.SpinnerStyle(Style.Parse("green bold"))
|
||||
.Start("Thinking...", ctx =>
|
||||
{
|
||||
// Omitted
|
||||
ctx.Refresh();
|
||||
});
|
||||
```
|
||||
@@ -51,8 +51,8 @@ AnsiConsole.Render(image);
|
||||
|
||||
# Manipulating images
|
||||
|
||||
You can take full advantage of using [ImageSharp](https://github.com/SixLabors/ImageSharp)
|
||||
and manipulate images directly via the [ImageSharp Processing API](https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Processing.html).
|
||||
You can take full advantage of [ImageSharp](https://github.com/SixLabors/ImageSharp)
|
||||
and manipulate images directly via it's [Processing API](https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Processing.html).
|
||||
|
||||
```csharp
|
||||
// Load an image
|
||||
@@ -69,7 +69,7 @@ image.Mutate(ctx => ctx.Grayscale().Rotate(-45).EntropyCrop());
|
||||
AnsiConsole.Render(image);
|
||||
```
|
||||
|
||||
# Result
|
||||
## Result
|
||||
|
||||
<pre style="font-size:90%;font-family:consolas,'Courier New',monospace;line-height: normal; padding: 0px;background-color: #222222; padding: 20px;">
|
||||
<span> </span><span style="background-color: #282828"> </span><span style="background-color: #222222"> </span><span style="background-color: #232323"> </span><span style="background-color: #353535"> </span><span style="background-color: #4B4B4B"> </span><span style="background-color: #595959"> </span><span style="background-color: #3B3B3B"> </span><span style="background-color: #202020"> </span><span style="background-color: #191919"> </span><span> </span>
|
||||
|
||||
@@ -1,39 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace ColumnsExample
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
public static void Main()
|
||||
{
|
||||
// Download some random users
|
||||
using var client = new HttpClient();
|
||||
dynamic users = JObject.Parse(
|
||||
await client.GetStringAsync("https://randomuser.me/api/?results=15"));
|
||||
|
||||
// Create a card for each user
|
||||
var cards = new List<Panel>();
|
||||
foreach(var user in users.results)
|
||||
foreach(var user in User.LoadUsers())
|
||||
{
|
||||
cards.Add(new Panel(GetCardContent(user))
|
||||
.Header($"{user.location.country}")
|
||||
.RoundedBorder().Expand());
|
||||
cards.Add(
|
||||
new Panel(GetCardContent(user))
|
||||
.Header($"{user.Country}")
|
||||
.RoundedBorder().Expand());
|
||||
}
|
||||
|
||||
// Render all cards in columns
|
||||
AnsiConsole.Render(new Columns(cards));
|
||||
}
|
||||
|
||||
private static string GetCardContent(dynamic user)
|
||||
private static string GetCardContent(User user)
|
||||
{
|
||||
var name = $"{user.name.first} {user.name.last}";
|
||||
var country = $"{user.location.city}";
|
||||
var name = $"{user.FirstName} {user.LastName}";
|
||||
var city = $"{user.City}";
|
||||
|
||||
return $"[b]{name}[/]\n[yellow]{country}[/]";
|
||||
return $"[b]{name}[/]\n[yellow]{city}[/]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
89
examples/Columns/User.cs
Normal file
89
examples/Columns/User.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ColumnsExample
|
||||
{
|
||||
public sealed class User
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string City { get; set; }
|
||||
public string Country { get; set; }
|
||||
|
||||
public static List<User> LoadUsers()
|
||||
{
|
||||
return new List<User>
|
||||
{
|
||||
new User
|
||||
{
|
||||
FirstName = "Andrea",
|
||||
LastName = "Johansen",
|
||||
City = "Hornbæk",
|
||||
Country = "Denmark",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Brandon",
|
||||
LastName = "Cole",
|
||||
City = "Washington",
|
||||
Country = "United States",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Patrik",
|
||||
LastName = "Svensson",
|
||||
City = "Stockholm",
|
||||
Country = "Sweden",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Freya",
|
||||
LastName = "Thompson",
|
||||
City = "Rotorua",
|
||||
Country = "New Zealand",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "طاها",
|
||||
LastName = "رضایی",
|
||||
City = "اهواز",
|
||||
Country = "Iran",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Yara",
|
||||
LastName = "Simon",
|
||||
City = "Develier",
|
||||
Country = "Switzerland",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Giray",
|
||||
LastName = "Erbay",
|
||||
City = "Karabük",
|
||||
Country = "Turkey",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Miodrag",
|
||||
LastName = "Schaffer",
|
||||
City = "Möckern",
|
||||
Country = "Germany",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Carmela",
|
||||
LastName = "Lo Castro",
|
||||
City = "Firenze",
|
||||
Country = "Italy",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Roberto",
|
||||
LastName = "Sims",
|
||||
City = "Mallow",
|
||||
Country = "Ireland",
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace InfoExample
|
||||
@@ -13,7 +12,7 @@ namespace InfoExample
|
||||
.AddRow("[b]Color system[/]", $"{AnsiConsole.Capabilities.ColorSystem}")
|
||||
.AddRow("[b]Supports ansi?[/]", $"{YesNo(AnsiConsole.Capabilities.SupportsAnsi)}")
|
||||
.AddRow("[b]Legacy console?[/]", $"{YesNo(AnsiConsole.Capabilities.LegacyConsole)}")
|
||||
.AddRow("[b]Interactive?[/]", $"{YesNo(Environment.UserInteractive)}")
|
||||
.AddRow("[b]Interactive?[/]", $"{YesNo(AnsiConsole.Capabilities.SupportsInteraction)}")
|
||||
.AddRow("[b]Buffer width[/]", $"{AnsiConsole.Console.Width}")
|
||||
.AddRow("[b]Buffer height[/]", $"{AnsiConsole.Console.Height}");
|
||||
|
||||
|
||||
45
examples/Progress/DescriptionGenerator.cs
Normal file
45
examples/Progress/DescriptionGenerator.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ProgressExample
|
||||
{
|
||||
public static class DescriptionGenerator
|
||||
{
|
||||
private static readonly string[] _verbs = new[] { "Downloading", "Rerouting", "Retriculating", "Collapsing", "Folding", "Solving", "Colliding", "Measuring" };
|
||||
private static readonly string[] _nouns = new[] { "internet", "splines", "space", "capacitators", "quarks", "algorithms", "data structures", "spacetime" };
|
||||
|
||||
private static readonly Random _random;
|
||||
private static readonly HashSet<string> _used;
|
||||
|
||||
static DescriptionGenerator()
|
||||
{
|
||||
_random = new Random(DateTime.Now.Millisecond);
|
||||
_used = new HashSet<string>();
|
||||
}
|
||||
|
||||
public static bool TryGenerate(out string name)
|
||||
{
|
||||
var iterations = 0;
|
||||
while (iterations < 25)
|
||||
{
|
||||
name = Generate();
|
||||
if (!_used.Contains(name))
|
||||
{
|
||||
_used.Add(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
iterations++;
|
||||
}
|
||||
|
||||
name = Generate();
|
||||
return false;
|
||||
}
|
||||
|
||||
public static string Generate()
|
||||
{
|
||||
return _verbs[_random.Next(0, _verbs.Length)]
|
||||
+ " " + _nouns[_random.Next(0, _nouns.Length)];
|
||||
}
|
||||
}
|
||||
}
|
||||
75
examples/Progress/Program.cs
Normal file
75
examples/Progress/Program.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace ProgressExample
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
AnsiConsole.MarkupLine("[yellow]Initializing warp drive[/]...");
|
||||
|
||||
// Show progress
|
||||
AnsiConsole.Progress()
|
||||
.AutoClear(false)
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(), // Task description
|
||||
new ProgressBarColumn(), // Progress bar
|
||||
new PercentageColumn(), // Percentage
|
||||
new RemainingTimeColumn(), // Remaining time
|
||||
new SpinnerColumn(), // Spinner
|
||||
})
|
||||
.Start(ctx =>
|
||||
{
|
||||
var random = new Random(DateTime.Now.Millisecond);
|
||||
var tasks = CreateTasks(ctx, random);
|
||||
|
||||
while (!ctx.IsFinished)
|
||||
{
|
||||
// Increment progress
|
||||
foreach (var (task, increment) in tasks)
|
||||
{
|
||||
task.Increment(random.NextDouble() * increment);
|
||||
}
|
||||
|
||||
// Write some random things to the terminal
|
||||
if (random.NextDouble() < 0.1)
|
||||
{
|
||||
WriteLogMessage();
|
||||
}
|
||||
|
||||
// Simulate some delay
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
});
|
||||
|
||||
// Done
|
||||
AnsiConsole.MarkupLine("[green]Done![/]");
|
||||
}
|
||||
|
||||
private static List<(ProgressTask, int)> CreateTasks(ProgressContext progress, Random random)
|
||||
{
|
||||
var tasks = new List<(ProgressTask, int)>();
|
||||
while (tasks.Count < 5)
|
||||
{
|
||||
if (DescriptionGenerator.TryGenerate(out var name))
|
||||
{
|
||||
tasks.Add((progress.AddTask(name), random.Next(2, 10)));
|
||||
}
|
||||
}
|
||||
|
||||
return tasks;
|
||||
}
|
||||
|
||||
private static void WriteLogMessage()
|
||||
{
|
||||
AnsiConsole.MarkupLine(
|
||||
"[grey]LOG:[/] " +
|
||||
DescriptionGenerator.Generate() +
|
||||
"[grey]...[/]");
|
||||
}
|
||||
}
|
||||
}
|
||||
19
examples/Progress/Progress.csproj
Normal file
19
examples/Progress/Progress.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Title>Progress</Title>
|
||||
<Description>Demonstrates how to show progress bars.</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -6,6 +6,13 @@ namespace Cursor
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
// Check if we can accept key strokes
|
||||
if (!AnsiConsole.Capabilities.SupportsInteraction)
|
||||
{
|
||||
AnsiConsole.MarkupLine("[red]Environment does not support interaction.[/]");
|
||||
return;
|
||||
}
|
||||
|
||||
// Confirmation
|
||||
if (!AnsiConsole.Confirm("Run prompt example?"))
|
||||
{
|
||||
|
||||
70
examples/Status/Program.cs
Normal file
70
examples/Status/Program.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.Threading;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace ProgressExample
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
AnsiConsole.Status()
|
||||
.AutoRefresh(true)
|
||||
.Spinner(Spinner.Known.Default)
|
||||
.Start("[yellow]Initializing warp drive[/]", ctx =>
|
||||
{
|
||||
// Initialize
|
||||
Thread.Sleep(3000);
|
||||
WriteLogMessage("Starting gravimetric field displacement manifold");
|
||||
Thread.Sleep(1000);
|
||||
WriteLogMessage("Warming up deuterium chamber");
|
||||
Thread.Sleep(2000);
|
||||
WriteLogMessage("Generating antideuterium");
|
||||
|
||||
// Warp nacelles
|
||||
Thread.Sleep(3000);
|
||||
ctx.Spinner(Spinner.Known.BouncingBar);
|
||||
ctx.Status("[bold blue]Unfolding warp nacelles[/]");
|
||||
WriteLogMessage("Unfolding left warp nacelle");
|
||||
Thread.Sleep(2000);
|
||||
WriteLogMessage("Left warp nacelle [green]online[/]");
|
||||
WriteLogMessage("Unfolding right warp nacelle");
|
||||
Thread.Sleep(1000);
|
||||
WriteLogMessage("Right warp nacelle [green]online[/]");
|
||||
|
||||
// Warp bubble
|
||||
Thread.Sleep(3000);
|
||||
ctx.Spinner(Spinner.Known.Star2);
|
||||
ctx.Status("[bold blue]Generating warp bubble[/]");
|
||||
Thread.Sleep(3000);
|
||||
ctx.Spinner(Spinner.Known.Star);
|
||||
ctx.Status("[bold blue]Stabilizing warp bubble[/]");
|
||||
|
||||
// Safety
|
||||
ctx.Spinner(Spinner.Known.Monkey);
|
||||
ctx.Status("[bold blue]Performing safety checks[/]");
|
||||
WriteLogMessage("Enabling interior dampening");
|
||||
Thread.Sleep(2000);
|
||||
WriteLogMessage("Interior dampening [green]enabled[/]");
|
||||
|
||||
// Warp!
|
||||
Thread.Sleep(3000);
|
||||
ctx.Spinner(Spinner.Known.Moon);
|
||||
WriteLogMessage("Preparing for warp");
|
||||
Thread.Sleep(1000);
|
||||
for (var warp = 1; warp < 10; warp++)
|
||||
{
|
||||
ctx.Status($"[bold blue]Warp {warp}[/]");
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
});
|
||||
|
||||
// Done
|
||||
AnsiConsole.MarkupLine("[bold green]Crusing at Warp 9.8[/]");
|
||||
}
|
||||
|
||||
private static void WriteLogMessage(string message)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[grey]LOG:[/] {message}[grey]...[/]");
|
||||
}
|
||||
}
|
||||
}
|
||||
19
examples/Status/Status.csproj
Normal file
19
examples/Status/Status.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<Title>Status</Title>
|
||||
<Description>Demonstrates how to show status updates.</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
22
resources/scripts/Generate-Spinners.ps1
Normal file
22
resources/scripts/Generate-Spinners.ps1
Normal file
@@ -0,0 +1,22 @@
|
||||
##########################################################
|
||||
# Script that generates progress spinners.
|
||||
##########################################################
|
||||
|
||||
$Output = Join-Path $PSScriptRoot "Temp"
|
||||
$Source = Join-Path $PSScriptRoot "/../../src/Spectre.Console"
|
||||
|
||||
if(!(Test-Path $Output -PathType Container)) {
|
||||
New-Item -ItemType Directory -Path $Output | Out-Null
|
||||
}
|
||||
|
||||
# Generate the files
|
||||
Push-Location Generator
|
||||
&dotnet run -- spinners "$Output" --input $Output
|
||||
if(!$?) {
|
||||
Pop-Location
|
||||
Throw "An error occured when generating code."
|
||||
}
|
||||
Pop-Location
|
||||
|
||||
# Copy the files to the correct location
|
||||
Copy-Item (Join-Path "$Output" "Spinner.Generated.cs") -Destination "$Source/Progress/Spinner.Generated.cs"
|
||||
@@ -7,7 +7,7 @@ using Spectre.IO;
|
||||
|
||||
namespace Generator.Commands
|
||||
{
|
||||
public sealed class ColorGeneratorCommand : Command<GeneratorCommandSettings>
|
||||
public sealed class ColorGeneratorCommand : Command<ColorGeneratorCommand.Settings>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
@@ -16,7 +16,13 @@ namespace Generator.Commands
|
||||
_fileSystem = new FileSystem();
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context, GeneratorCommandSettings settings)
|
||||
public sealed class Settings : GeneratorSettings
|
||||
{
|
||||
[CommandOption("-i|--input <PATH>")]
|
||||
public string Input { get; set; }
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context, Settings settings)
|
||||
{
|
||||
var templates = new FilePath[]
|
||||
{
|
||||
@@ -50,13 +56,4 @@ namespace Generator.Commands
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class GeneratorCommandSettings : CommandSettings
|
||||
{
|
||||
[CommandArgument(0, "<OUTPUT>")]
|
||||
public string Output { get; set; }
|
||||
|
||||
[CommandOption("-i|--input <PATH>")]
|
||||
public string Input { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ using SpectreEnvironment = Spectre.IO.Environment;
|
||||
|
||||
namespace Generator.Commands
|
||||
{
|
||||
public sealed class EmojiGeneratorCommand : AsyncCommand<GeneratorCommandSettings>
|
||||
public sealed class EmojiGeneratorCommand : AsyncCommand<EmojiGeneratorCommand.Settings>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly IEnvironment _environment;
|
||||
@@ -24,9 +24,15 @@ namespace Generator.Commands
|
||||
private readonly Dictionary<string, string> _templates = new Dictionary<string, string>
|
||||
{
|
||||
{ "Templates/Emoji.Generated.template", "Emoji.Generated.cs" },
|
||||
{ "Templates/Emoji.Json.template", "emojis.json" },
|
||||
{ "Templates/Emoji.Json.template", "emojis.json" }, // For documentation
|
||||
};
|
||||
|
||||
public sealed class Settings : GeneratorSettings
|
||||
{
|
||||
[CommandOption("-i|--input <PATH>")]
|
||||
public string Input { get; set; }
|
||||
}
|
||||
|
||||
public EmojiGeneratorCommand()
|
||||
{
|
||||
_fileSystem = new FileSystem();
|
||||
@@ -34,7 +40,7 @@ namespace Generator.Commands
|
||||
_parser = new HtmlParser();
|
||||
}
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, GeneratorCommandSettings settings)
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
{
|
||||
var output = new DirectoryPath(settings.Output);
|
||||
if (!_fileSystem.Directory.Exists(settings.Output))
|
||||
@@ -60,7 +66,7 @@ namespace Generator.Commands
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async Task<Stream> FetchEmojis(GeneratorCommandSettings settings)
|
||||
private async Task<Stream> FetchEmojis(Settings settings)
|
||||
{
|
||||
var input = string.IsNullOrEmpty(settings.Input)
|
||||
? _environment.WorkingDirectory
|
||||
|
||||
10
resources/scripts/Generator/Commands/GeneratorSettings.cs
Normal file
10
resources/scripts/Generator/Commands/GeneratorSettings.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Spectre.Cli;
|
||||
|
||||
namespace Generator.Commands
|
||||
{
|
||||
public class GeneratorSettings : CommandSettings
|
||||
{
|
||||
[CommandArgument(0, "<OUTPUT>")]
|
||||
public string Output { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Generator.Models;
|
||||
using Scriban;
|
||||
using Spectre.Cli;
|
||||
using Spectre.IO;
|
||||
|
||||
namespace Generator.Commands
|
||||
{
|
||||
public sealed class SpinnerGeneratorCommand : Command<GeneratorSettings>
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
|
||||
public SpinnerGeneratorCommand()
|
||||
{
|
||||
_fileSystem = new FileSystem();
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context, GeneratorSettings settings)
|
||||
{
|
||||
// Read the spinner model.
|
||||
var spinners = new List<Spinner>();
|
||||
spinners.AddRange(Spinner.Parse(File.ReadAllText("Data/spinners_default.json")));
|
||||
spinners.AddRange(Spinner.Parse(File.ReadAllText("Data/spinners_sindresorhus.json")));
|
||||
|
||||
var output = new DirectoryPath(settings.Output);
|
||||
if (!_fileSystem.Directory.Exists(settings.Output))
|
||||
{
|
||||
_fileSystem.Directory.Create(settings.Output);
|
||||
}
|
||||
|
||||
// Parse the Scriban template.
|
||||
var templatePath = new FilePath("Templates/Spinner.Generated.template");
|
||||
var template = Template.Parse(File.ReadAllText(templatePath.FullPath));
|
||||
|
||||
// Render the template with the model.
|
||||
var result = template.Render(new { Spinners = spinners });
|
||||
|
||||
// Write output to file
|
||||
var file = output.CombineWithFilePath(templatePath.GetFilename().ChangeExtension(".cs"));
|
||||
File.WriteAllText(file.FullPath, result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
resources/scripts/Generator/Data/spinners_default.json
Normal file
30
resources/scripts/Generator/Data/spinners_default.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"Default": {
|
||||
"interval": 100,
|
||||
"unicode": true,
|
||||
"frames": [
|
||||
"⣷",
|
||||
"⣯",
|
||||
"⣟",
|
||||
"⡿",
|
||||
"⢿",
|
||||
"⣻",
|
||||
"⣽",
|
||||
"⣾"
|
||||
]
|
||||
},
|
||||
"Ascii": {
|
||||
"interval": 100,
|
||||
"unicode": true,
|
||||
"frames": [
|
||||
"-",
|
||||
"\\",
|
||||
"|",
|
||||
"/",
|
||||
"-",
|
||||
"\\",
|
||||
"|",
|
||||
"/"
|
||||
]
|
||||
}
|
||||
}
|
||||
1368
resources/scripts/Generator/Data/spinners_sindresorhus.json
Normal file
1368
resources/scripts/Generator/Data/spinners_sindresorhus.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,12 @@
|
||||
<None Update="Data\colors.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Data\spinners_default.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Data\spinners_sindresorhus.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Templates\ColorTable.Generated.template">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
@@ -24,6 +30,9 @@
|
||||
<None Update="Templates\ColorPalette.Generated.template">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Templates\Spinner.Generated.template">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Templates\Emoji.Json.template">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
|
||||
31
resources/scripts/Generator/Models/Spinner.cs
Normal file
31
resources/scripts/Generator/Models/Spinner.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Humanizer;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Generator.Models
|
||||
{
|
||||
public sealed class Spinner
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string NormalizedName { get; set; }
|
||||
public int Interval { get; set; }
|
||||
public bool Unicode { get; set; }
|
||||
public List<string> Frames { get; set; }
|
||||
|
||||
public static IEnumerable<Spinner> Parse(string json)
|
||||
{
|
||||
var data = JsonConvert.DeserializeObject<Dictionary<string, Spinner>>(json);
|
||||
foreach (var item in data)
|
||||
{
|
||||
item.Value.Name = item.Key;
|
||||
item.Value.NormalizedName = item.Value.Name.Pascalize();
|
||||
|
||||
var frames = item.Value.Frames;
|
||||
item.Value.Frames = frames.Select(f => f.Replace("\\", "\\\\")).ToList();
|
||||
}
|
||||
|
||||
return data.Values;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ namespace Generator
|
||||
{
|
||||
config.AddCommand<ColorGeneratorCommand>("colors");
|
||||
config.AddCommand<EmojiGeneratorCommand>("emoji");
|
||||
config.AddCommand<SpinnerGeneratorCommand>("spinners");
|
||||
});
|
||||
|
||||
return app.Run(args);
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Generated {{ date.now | date.to_string `%F %R` }}
|
||||
//
|
||||
// Partly generated from
|
||||
// https://github.com/sindresorhus/cli-spinners/blob/master/spinners.json
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
public abstract partial class Spinner
|
||||
{
|
||||
{{~ for spinner in spinners ~}}
|
||||
private sealed class {{ spinner.normalized_name }}Spinner : Spinner
|
||||
{
|
||||
public override TimeSpan Interval => TimeSpan.FromMilliseconds({{ spinner.interval }});
|
||||
public override bool IsUnicode => {{ spinner.unicode }};
|
||||
public override IReadOnlyList<string> Frames => new List<string>
|
||||
{
|
||||
{{~ for frame in spinner.frames ~}}
|
||||
"{{ frame }}",
|
||||
{{~ end ~}}
|
||||
};
|
||||
}
|
||||
{{~ end ~}}
|
||||
|
||||
/// <summary>
|
||||
/// Contains all predefined spinners.
|
||||
/// </summary>
|
||||
public static class Known
|
||||
{
|
||||
{{~ for spinner in spinners ~}}
|
||||
/// <summary>
|
||||
/// Gets the "{{ spinner.name }}" spinner.
|
||||
/// </summary>
|
||||
public static Spinner {{ spinner.normalized_name }} { get; } = new {{ spinner.normalized_name }}Spinner();
|
||||
{{~ end ~}}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,4 +89,7 @@ dotnet_diagnostic.RCS1227.severity = none
|
||||
dotnet_diagnostic.IDE0004.severity = warning
|
||||
|
||||
# CA1810: Initialize reference type static fields inline
|
||||
dotnet_diagnostic.CA1810.severity = none
|
||||
dotnet_diagnostic.CA1810.severity = none
|
||||
|
||||
# IDE0044: Add readonly modifier
|
||||
dotnet_diagnostic.IDE0044.severity = warning
|
||||
@@ -0,0 +1,7 @@
|
||||
┌──────────┐
|
||||
│ ┌──────┐ │
|
||||
│ │ 测试 │ │
|
||||
│ ├──────┤ │
|
||||
│ │ 测试 │ │
|
||||
│ └──────┘ │
|
||||
└──────────┘
|
||||
@@ -0,0 +1 @@
|
||||
━━━━━━━━━━━━━━━━━━━━
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
foo ━━━ 0% -:--:-- ⣷
|
||||
bar ━━━ 0% -:--:-- ⣷
|
||||
baz ━━━ 0% -:--:-- ⣷
|
||||
|
||||
17
src/Spectre.Console.Tests/Tools/DummyCursor.cs
Normal file
17
src/Spectre.Console.Tests/Tools/DummyCursor.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Spectre.Console.Tests
|
||||
{
|
||||
public sealed class DummyCursor : IAnsiConsoleCursor
|
||||
{
|
||||
public void Move(CursorDirection direction, int steps)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetPosition(int column, int line)
|
||||
{
|
||||
}
|
||||
|
||||
public void Show(bool show)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/Spectre.Console.Tests/Tools/DummySpinners.cs
Normal file
25
src/Spectre.Console.Tests/Tools/DummySpinners.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console.Tests
|
||||
{
|
||||
public sealed class DummySpinner1 : Spinner
|
||||
{
|
||||
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
|
||||
public override bool IsUnicode => true;
|
||||
public override IReadOnlyList<string> Frames => new List<string>
|
||||
{
|
||||
"*",
|
||||
};
|
||||
}
|
||||
|
||||
public sealed class DummySpinner2 : Spinner
|
||||
{
|
||||
public override TimeSpan Interval => TimeSpan.FromMilliseconds(100);
|
||||
public override bool IsUnicode => true;
|
||||
public override IReadOnlyList<string> Frames => new List<string>
|
||||
{
|
||||
"-",
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,14 @@ namespace Spectre.Console.Tests
|
||||
{
|
||||
public Capabilities Capabilities { get; }
|
||||
public Encoding Encoding { get; }
|
||||
public IAnsiConsoleCursor Cursor => throw new NotSupportedException();
|
||||
public IAnsiConsoleCursor Cursor => new DummyCursor();
|
||||
public TestableConsoleInput Input { get; }
|
||||
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
|
||||
IAnsiConsoleInput IAnsiConsole.Input => Input;
|
||||
public RenderPipeline Pipeline { get; }
|
||||
|
||||
public Decoration Decoration { get; set; }
|
||||
public Color Foreground { get; set; }
|
||||
@@ -31,14 +32,15 @@ namespace Spectre.Console.Tests
|
||||
public PlainConsole(
|
||||
int width = 80, int height = 9000, Encoding encoding = null,
|
||||
bool supportsAnsi = true, ColorSystem colorSystem = ColorSystem.Standard,
|
||||
bool legacyConsole = false)
|
||||
bool legacyConsole = false, bool interactive = true)
|
||||
{
|
||||
Capabilities = new Capabilities(supportsAnsi, colorSystem, legacyConsole);
|
||||
Capabilities = new Capabilities(supportsAnsi, colorSystem, legacyConsole, interactive);
|
||||
Encoding = encoding ?? Encoding.UTF8;
|
||||
Width = width;
|
||||
Height = height;
|
||||
Writer = new StringWriter();
|
||||
Input = new TestableConsoleInput();
|
||||
Pipeline = new RenderPipeline();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@@ -50,14 +52,17 @@ namespace Spectre.Console.Tests
|
||||
{
|
||||
}
|
||||
|
||||
public void Write(Segment segment)
|
||||
public void Write(IEnumerable<Segment> segments)
|
||||
{
|
||||
if (segment is null)
|
||||
if (segments is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(segment));
|
||||
return;
|
||||
}
|
||||
|
||||
Writer.Write(segment.Text);
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
Writer.Write(segment.Text);
|
||||
}
|
||||
}
|
||||
|
||||
public string WriteNormalizedException(Exception ex, ExceptionFormats formats = ExceptionFormats.Default)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Spectre.Console.Tests.Tools
|
||||
namespace Spectre.Console.Tests
|
||||
{
|
||||
public sealed class TestLinkIdentityGenerator : ILinkIdentityGenerator
|
||||
{
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Spectre.Console.Rendering;
|
||||
using Spectre.Console.Tests.Tools;
|
||||
|
||||
namespace Spectre.Console.Tests
|
||||
{
|
||||
@@ -19,16 +19,21 @@ namespace Spectre.Console.Tests
|
||||
public int Height => _console.Height;
|
||||
public IAnsiConsoleCursor Cursor => _console.Cursor;
|
||||
public TestableConsoleInput Input { get; }
|
||||
public RenderPipeline Pipeline => _console.Pipeline;
|
||||
|
||||
IAnsiConsoleInput IAnsiConsole.Input => Input;
|
||||
|
||||
public TestableAnsiConsole(ColorSystem system, AnsiSupport ansi = AnsiSupport.Yes, int width = 80)
|
||||
public TestableAnsiConsole(
|
||||
ColorSystem system, AnsiSupport ansi = AnsiSupport.Yes,
|
||||
InteractionSupport interaction = InteractionSupport.Yes,
|
||||
int width = 80)
|
||||
{
|
||||
_writer = new StringWriter();
|
||||
_console = AnsiConsole.Create(new AnsiConsoleSettings
|
||||
{
|
||||
Ansi = ansi,
|
||||
ColorSystem = (ColorSystemSupport)system,
|
||||
Interactive = interaction,
|
||||
Out = _writer,
|
||||
LinkIdentityGenerator = new TestLinkIdentityGenerator(),
|
||||
});
|
||||
@@ -47,9 +52,17 @@ namespace Spectre.Console.Tests
|
||||
_console.Clear(home);
|
||||
}
|
||||
|
||||
public void Write(Segment segment)
|
||||
public void Write(IEnumerable<Segment> segments)
|
||||
{
|
||||
_console.Write(segment);
|
||||
if (segments is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
_console.Write(segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Rendering;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
@@ -267,5 +269,23 @@ namespace Spectre.Console.Tests.Unit
|
||||
// Then
|
||||
return Verifier.Verify(console.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Wrap_Table_With_CJK_Tables_In_Panel_Correctly()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole(width: 80);
|
||||
|
||||
var table = new Table();
|
||||
table.AddColumn("测试");
|
||||
table.AddRow("测试");
|
||||
var panel = new Panel(table);
|
||||
|
||||
// When
|
||||
console.Render(panel);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(console.Output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
115
src/Spectre.Console.Tests/Unit/ProgressTests.cs
Normal file
115
src/Spectre.Console.Tests/Unit/ProgressTests.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System.Threading.Tasks;
|
||||
using Shouldly;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit
|
||||
{
|
||||
[UsesVerify]
|
||||
public sealed class ProgressTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Render_Task_Correctly()
|
||||
{
|
||||
// Given
|
||||
var console = new TestableAnsiConsole(ColorSystem.TrueColor, width: 10);
|
||||
|
||||
var progress = new Progress(console)
|
||||
.Columns(new[] { new ProgressBarColumn() })
|
||||
.AutoRefresh(false)
|
||||
.AutoClear(true);
|
||||
|
||||
// When
|
||||
progress.Start(ctx => ctx.AddTask("foo"));
|
||||
|
||||
// Then
|
||||
console.Output
|
||||
.NormalizeLineEndings()
|
||||
.ShouldBe(
|
||||
"[?25l" + // Hide cursor
|
||||
" \n" + // Top padding
|
||||
"[38;5;8m━━━━━━━━━━[0m\n" + // Task
|
||||
" " + // Bottom padding
|
||||
"[2K[1A[2K[1A[2K[?25h"); // Clear + show cursor
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Not_Auto_Clear_If_Specified()
|
||||
{
|
||||
// Given
|
||||
var console = new TestableAnsiConsole(ColorSystem.TrueColor, width: 10);
|
||||
|
||||
var progress = new Progress(console)
|
||||
.Columns(new[] { new ProgressBarColumn() })
|
||||
.AutoRefresh(false)
|
||||
.AutoClear(false);
|
||||
|
||||
// When
|
||||
progress.Start(ctx => ctx.AddTask("foo"));
|
||||
|
||||
// Then
|
||||
console.Output
|
||||
.NormalizeLineEndings()
|
||||
.ShouldBe(
|
||||
"[?25l" + // Hide cursor
|
||||
" \n" + // Top padding
|
||||
"[38;5;8m━━━━━━━━━━[0m\n" + // Task
|
||||
" \n" + // Bottom padding
|
||||
"[?25h"); // show cursor
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task Should_Reduce_Width_If_Needed()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole(width: 20);
|
||||
|
||||
var progress = new Progress(console)
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn(),
|
||||
new RemainingTimeColumn(),
|
||||
new SpinnerColumn(),
|
||||
})
|
||||
.AutoRefresh(false)
|
||||
.AutoClear(false);
|
||||
|
||||
// When
|
||||
progress.Start(ctx =>
|
||||
{
|
||||
ctx.AddTask("foo");
|
||||
ctx.AddTask("bar");
|
||||
ctx.AddTask("baz");
|
||||
});
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(console.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Setting_Max_Value_Should_Set_The_MaxValue_And_Cap_Value()
|
||||
{
|
||||
// Given
|
||||
var task = default(ProgressTask);
|
||||
var console = new PlainConsole();
|
||||
var progress = new Progress(console)
|
||||
.Columns(new[] { new ProgressBarColumn() })
|
||||
.AutoRefresh(false)
|
||||
.AutoClear(false);
|
||||
|
||||
// When
|
||||
progress.Start(ctx =>
|
||||
{
|
||||
task = ctx.AddTask("foo");
|
||||
task.Increment(100);
|
||||
task.MaxValue = 20;
|
||||
});
|
||||
|
||||
// Then
|
||||
task.MaxValue.ShouldBe(20);
|
||||
task.Value.ShouldBe(20);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/Spectre.Console.Tests/Unit/RenderHookTests.cs
Normal file
34
src/Spectre.Console.Tests/Unit/RenderHookTests.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Shouldly;
|
||||
using Spectre.Console.Rendering;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit
|
||||
{
|
||||
public sealed class RenderHookTests
|
||||
{
|
||||
private sealed class HelloRenderHook : IRenderHook
|
||||
{
|
||||
public IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
return new IRenderable[] { new Text("Hello\n") }.Concat(renderables);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Inject_Renderable_Before_Writing_To_Console()
|
||||
{
|
||||
// Given
|
||||
var console = new PlainConsole();
|
||||
console.Pipeline.Attach(new HelloRenderHook());
|
||||
|
||||
// When
|
||||
console.Render(new Text("World"));
|
||||
|
||||
// Then
|
||||
console.Lines[0].ShouldBe("Hello");
|
||||
console.Lines[1].ShouldBe("World");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,18 +22,43 @@ namespace Spectre.Console.Tests.Unit
|
||||
[UsesVerify]
|
||||
public sealed class TheSplitMethod
|
||||
{
|
||||
[Fact]
|
||||
public Task Should_Split_Segment_Correctly()
|
||||
[Theory]
|
||||
[InlineData("Foo Bar", 0, "", "Foo Bar")]
|
||||
[InlineData("Foo Bar", 1, "F", "oo Bar")]
|
||||
[InlineData("Foo Bar", 2, "Fo", "o Bar")]
|
||||
[InlineData("Foo Bar", 3, "Foo", " Bar")]
|
||||
[InlineData("Foo Bar", 4, "Foo ", "Bar")]
|
||||
[InlineData("Foo Bar", 5, "Foo B", "ar")]
|
||||
[InlineData("Foo Bar", 6, "Foo Ba", "r")]
|
||||
[InlineData("Foo Bar", 7, "Foo Bar", null)]
|
||||
[InlineData("Foo 测试 Bar", 0, "", "Foo 测试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 1, "F", "oo 测试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 2, "Fo", "o 测试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 3, "Foo", " 测试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 4, "Foo ", "测试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 5, "Foo 测", "试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 6, "Foo 测", "试 Bar")]
|
||||
[InlineData("Foo 测试 Bar", 7, "Foo 测试", " Bar")]
|
||||
[InlineData("Foo 测试 Bar", 8, "Foo 测试", " Bar")]
|
||||
[InlineData("Foo 测试 Bar", 9, "Foo 测试 ", "Bar")]
|
||||
[InlineData("Foo 测试 Bar", 10, "Foo 测试 B", "ar")]
|
||||
[InlineData("Foo 测试 Bar", 11, "Foo 测试 Ba", "r")]
|
||||
[InlineData("Foo 测试 Bar", 12, "Foo 测试 Bar", null)]
|
||||
public void Should_Split_Segment_Correctly(string text, int offset, string expectedFirst, string expectedSecond)
|
||||
{
|
||||
// Given
|
||||
var style = new Style(Color.Red, Color.Green, Decoration.Bold);
|
||||
var segment = new Segment("Foo Bar", style);
|
||||
var context = new RenderContext(Encoding.UTF8, false);
|
||||
var segment = new Segment(text, style);
|
||||
|
||||
// When
|
||||
var result = segment.Split(3);
|
||||
var (first, second) = segment.Split(context, offset);
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result);
|
||||
first.Text.ShouldBe(expectedFirst);
|
||||
first.Style.ShouldBe(style);
|
||||
second?.Text?.ShouldBe(expectedSecond);
|
||||
second?.Style?.ShouldBe(style);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
42
src/Spectre.Console.Tests/Unit/StatusTests.cs
Normal file
42
src/Spectre.Console.Tests/Unit/StatusTests.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit
|
||||
{
|
||||
public sealed partial class StatusTests
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Render_Status_Correctly()
|
||||
{
|
||||
// Given
|
||||
var console = new TestableAnsiConsole(ColorSystem.TrueColor, width: 10);
|
||||
|
||||
var status = new Status(console);
|
||||
status.AutoRefresh = false;
|
||||
status.Spinner = new DummySpinner1();
|
||||
|
||||
// When
|
||||
status.Start("foo", ctx =>
|
||||
{
|
||||
ctx.Refresh();
|
||||
ctx.Spinner(new DummySpinner2());
|
||||
ctx.Status("bar");
|
||||
ctx.Refresh();
|
||||
ctx.Spinner(new DummySpinner1());
|
||||
ctx.Status("baz");
|
||||
});
|
||||
|
||||
// Then
|
||||
console.Output
|
||||
.NormalizeLineEndings()
|
||||
.ShouldBe(
|
||||
"[?25l \n" +
|
||||
"[38;5;11m*[0m foo\n" +
|
||||
" [1A[1A \n" +
|
||||
"[38;5;11m-[0m bar\n" +
|
||||
" [1A[1A \n" +
|
||||
"[38;5;11m*[0m baz\n" +
|
||||
" [2K[1A[2K[1A[2K[?25h");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Canvas", "..\examples\Canva
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spectre.Console.ImageSharp", "Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj", "{0EFE694D-0770-4E71-BF4E-EC2B41362F79}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Progress", "..\examples\Progress\Progress.csproj", "{2B712A52-40F1-4C1C-833E-7C869ACA91F3}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Status", "..\examples\Status\Status.csproj", "{3716AFDF-0904-4635-8422-86E6B9356840}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -296,6 +300,30 @@ Global
|
||||
{0EFE694D-0770-4E71-BF4E-EC2B41362F79}.Release|x64.Build.0 = Release|Any CPU
|
||||
{0EFE694D-0770-4E71-BF4E-EC2B41362F79}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{0EFE694D-0770-4E71-BF4E-EC2B41362F79}.Release|x86.Build.0 = Release|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Release|x64.Build.0 = Release|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3}.Release|x86.Build.0 = Release|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Release|x64.Build.0 = Release|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -318,6 +346,8 @@ Global
|
||||
{6351C70F-F368-46DB-BAED-9B87CCD69353} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
|
||||
{45BF6302-6553-4E52-BF0F-B10D1AA9A6D1} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
|
||||
{5693761A-754A-40A8-9144-36510D6A4D69} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
|
||||
{2B712A52-40F1-4C1C-833E-7C869ACA91F3} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
|
||||
{3716AFDF-0904-4635-8422-86E6B9356840} = {F0575243-121F-4DEE-9F6B-246E26DC0844}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C}
|
||||
|
||||
26
src/Spectre.Console/AnsiConsole.Progress.cs
Normal file
26
src/Spectre.Console/AnsiConsole.Progress.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A console capable of writing ANSI escape sequences.
|
||||
/// </summary>
|
||||
public static partial class AnsiConsole
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Progress"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="Progress"/> instance.</returns>
|
||||
public static Progress Progress()
|
||||
{
|
||||
return Console.Progress();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Status"/> instance.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="Status"/> instance.</returns>
|
||||
public static Status Status()
|
||||
{
|
||||
return Console.Status();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,12 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
public ColorSystemSupport ColorSystem { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or
|
||||
/// not the console is interactive.
|
||||
/// </summary>
|
||||
public InteractionSupport Interactive { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the link identity generator.
|
||||
/// </summary>
|
||||
|
||||
@@ -36,17 +36,24 @@ namespace Spectre.Console
|
||||
/// </remarks>
|
||||
public bool LegacyConsole { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not the console supports interaction.
|
||||
/// </summary>
|
||||
public bool SupportsInteraction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Capabilities"/> class.
|
||||
/// </summary>
|
||||
/// <param name="supportsAnsi">Whether or not ANSI escape sequences are supported.</param>
|
||||
/// <param name="colorSystem">The color system that is supported.</param>
|
||||
/// <param name="legacyConsole">Whether or not this is a legacy console.</param>
|
||||
public Capabilities(bool supportsAnsi, ColorSystem colorSystem, bool legacyConsole)
|
||||
/// <param name="supportsInteraction">Whether or not the console supports interaction.</param>
|
||||
public Capabilities(bool supportsAnsi, ColorSystem colorSystem, bool legacyConsole, bool supportsInteraction)
|
||||
{
|
||||
SupportsAnsi = supportsAnsi;
|
||||
ColorSystem = colorSystem;
|
||||
LegacyConsole = legacyConsole;
|
||||
SupportsInteraction = supportsInteraction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -8,31 +8,31 @@ namespace Spectre.Console
|
||||
/// <summary>
|
||||
/// Try to detect the color system.
|
||||
/// </summary>
|
||||
Detect = -1,
|
||||
Detect = 0,
|
||||
|
||||
/// <summary>
|
||||
/// No colors.
|
||||
/// </summary>
|
||||
NoColors = 0,
|
||||
NoColors = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Legacy, 3-bit mode.
|
||||
/// </summary>
|
||||
Legacy = 1,
|
||||
Legacy = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Standard, 4-bit mode.
|
||||
/// </summary>
|
||||
Standard = 2,
|
||||
Standard = 3,
|
||||
|
||||
/// <summary>
|
||||
/// 8-bit mode.
|
||||
/// </summary>
|
||||
EightBit = 3,
|
||||
EightBit = 4,
|
||||
|
||||
/// <summary>
|
||||
/// 24-bit mode.
|
||||
/// </summary>
|
||||
TrueColor = 4,
|
||||
TrueColor = 5,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,8 +52,7 @@ namespace Spectre.Console
|
||||
/// <param name="args">An array of objects to write.</param>
|
||||
public static void MarkupLine(this IAnsiConsole console, IFormatProvider provider, string format, params object[] args)
|
||||
{
|
||||
Markup(console, provider, format, args);
|
||||
console.WriteLine();
|
||||
Markup(console, provider, format + Environment.NewLine, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="IAnsiConsole"/>.
|
||||
/// </summary>
|
||||
public static partial class AnsiConsoleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Progress"/> instance for the console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <returns>A <see cref="Progress"/> instance.</returns>
|
||||
public static Progress Progress(this IAnsiConsole console)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
return new Progress(console);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Status"/> instance for the console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
/// <returns>A <see cref="Status"/> instance.</returns>
|
||||
public static Status Status(this IAnsiConsole console)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
return new Status(console);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
@@ -26,19 +26,26 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(renderable));
|
||||
}
|
||||
|
||||
var options = new RenderContext(console.Encoding, console.Capabilities.LegacyConsole);
|
||||
var segments = renderable.Render(options, console.Width).ToArray();
|
||||
segments = Segment.Merge(segments).ToArray();
|
||||
var context = new RenderContext(console.Encoding, console.Capabilities.LegacyConsole);
|
||||
var renderables = console.Pipeline.Process(context, new[] { renderable });
|
||||
|
||||
foreach (var segment in segments)
|
||||
Render(console, context, renderables);
|
||||
}
|
||||
|
||||
private static void Render(IAnsiConsole console, RenderContext options, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
if (renderables is null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(segment.Text))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
console.Write(segment.Text, segment.Style);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = new List<Segment>();
|
||||
foreach (var renderable in renderables)
|
||||
{
|
||||
result.AddRange(renderable.Render(options, console.Width));
|
||||
}
|
||||
|
||||
console.Write(Segment.Merge(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,26 @@ namespace Spectre.Console
|
||||
return new Recorder(console);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified string value to the console.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to write to.</param>
|
||||
/// <param name="segment">The segment to write.</param>
|
||||
public static void Write(this IAnsiConsole console, Segment segment)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
if (segment is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(segment));
|
||||
}
|
||||
|
||||
console.Write(new[] { segment });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the specified string value to the console.
|
||||
/// </summary>
|
||||
@@ -25,7 +45,7 @@ namespace Spectre.Console
|
||||
/// <param name="text">The text to write.</param>
|
||||
public static void Write(this IAnsiConsole console, string text)
|
||||
{
|
||||
Write(console, text, Style.Plain);
|
||||
Render(console, new Text(text, Style.Plain));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -36,17 +56,7 @@ namespace Spectre.Console
|
||||
/// <param name="style">The text style.</param>
|
||||
public static void Write(this IAnsiConsole console, string text, Style style)
|
||||
{
|
||||
if (console is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
if (text is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
console.Write(new Segment(text, style));
|
||||
Render(console, new Text(text, style));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -60,7 +70,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
console.Write(Environment.NewLine, Style.Plain);
|
||||
Render(console, new Text(Environment.NewLine, Style.Plain));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,8 +101,7 @@ namespace Spectre.Console
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
}
|
||||
|
||||
console.Write(new Segment(text, style));
|
||||
console.WriteLine();
|
||||
console.Write(text + Environment.NewLine, style);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
alignment ??= panel.Header?.Alignment;
|
||||
return Header(panel, new PanelHeader(text, alignment));
|
||||
return Header(panel, new PanelHeader(text, alignment));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="PercentageColumn"/>.
|
||||
/// </summary>
|
||||
public static class PercentageColumnExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the style for a non-complete task.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static PercentageColumn Style(this PercentageColumn column, Style style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
column.Style = style;
|
||||
return column;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the style for a completed task.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static PercentageColumn CompletedStyle(this PercentageColumn column, Style style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
column.CompletedStyle = style;
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="ProgressBarColumn"/>.
|
||||
/// </summary>
|
||||
public static class ProgressBarColumnExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the style of completed portions of the progress bar.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static ProgressBarColumn CompletedStyle(this ProgressBarColumn column, Style style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
column.CompletedStyle = style;
|
||||
return column;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the style of a finished progress bar.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static ProgressBarColumn FinishedStyle(this ProgressBarColumn column, Style style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
column.FinishedStyle = style;
|
||||
return column;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the style of remaining portions of the progress bar.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static ProgressBarColumn RemainingStyle(this ProgressBarColumn column, Style style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
column.RemainingStyle = style;
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="Progress"/>.
|
||||
/// </summary>
|
||||
public static class ProgressExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the columns to be used for an <see cref="Progress"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="progress">The <see cref="Progress"/> instance.</param>
|
||||
/// <param name="columns">The columns to use.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Progress Columns(this Progress progress, ProgressColumn[] columns)
|
||||
{
|
||||
if (progress is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(progress));
|
||||
}
|
||||
|
||||
if (columns is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(columns));
|
||||
}
|
||||
|
||||
if (!columns.Any())
|
||||
{
|
||||
throw new InvalidOperationException("At least one column must be specified.");
|
||||
}
|
||||
|
||||
progress.Columns.Clear();
|
||||
progress.Columns.AddRange(columns);
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets whether or not auto refresh is enabled.
|
||||
/// If disabled, you will manually have to refresh the progress.
|
||||
/// </summary>
|
||||
/// <param name="progress">The <see cref="Progress"/> instance.</param>
|
||||
/// <param name="enabled">Whether or not auto refresh is enabled.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Progress AutoRefresh(this Progress progress, bool enabled)
|
||||
{
|
||||
if (progress is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(progress));
|
||||
}
|
||||
|
||||
progress.AutoRefresh = enabled;
|
||||
|
||||
return progress;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets whether or not auto clear is enabled.
|
||||
/// If enabled, the task tabled will be removed once
|
||||
/// all tasks have completed.
|
||||
/// </summary>
|
||||
/// <param name="progress">The <see cref="Progress"/> instance.</param>
|
||||
/// <param name="enabled">Whether or not auto clear is enabled.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Progress AutoClear(this Progress progress, bool enabled)
|
||||
{
|
||||
if (progress is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(progress));
|
||||
}
|
||||
|
||||
progress.AutoClear = enabled;
|
||||
|
||||
return progress;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="ProgressTask"/>.
|
||||
/// </summary>
|
||||
public static class ProgressTaskExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the task description.
|
||||
/// </summary>
|
||||
/// <param name="task">The task.</param>
|
||||
/// <param name="description">The description.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static ProgressTask Description(this ProgressTask task, string description)
|
||||
{
|
||||
if (task is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(task));
|
||||
}
|
||||
|
||||
task.Description = description;
|
||||
return task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the max value of the task.
|
||||
/// </summary>
|
||||
/// <param name="task">The task.</param>
|
||||
/// <param name="value">The max value.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static ProgressTask MaxValue(this ProgressTask task, double value)
|
||||
{
|
||||
if (task is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(task));
|
||||
}
|
||||
|
||||
task.MaxValue = value;
|
||||
return task;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="RemainingTimeColumn"/>.
|
||||
/// </summary>
|
||||
public static class RemainingTimeColumnExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the style of the remaining time text.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static RemainingTimeColumn Style(this RemainingTimeColumn column, Style style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
if (style is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(style));
|
||||
}
|
||||
|
||||
column.Style = style;
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="SpinnerColumn"/>.
|
||||
/// </summary>
|
||||
public static class SpinnerColumnExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the style of the spinner.
|
||||
/// </summary>
|
||||
/// <param name="column">The column.</param>
|
||||
/// <param name="style">The style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static SpinnerColumn Style(this SpinnerColumn column, Style? style)
|
||||
{
|
||||
if (column is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(column));
|
||||
}
|
||||
|
||||
column.Style = style;
|
||||
return column;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="StatusContext"/>.
|
||||
/// </summary>
|
||||
public static class StatusContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the status message.
|
||||
/// </summary>
|
||||
/// <param name="context">The status context.</param>
|
||||
/// <param name="status">The status message.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static StatusContext Status(this StatusContext context, string status)
|
||||
{
|
||||
if (context is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
context.Status = status;
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the spinner.
|
||||
/// </summary>
|
||||
/// <param name="context">The status context.</param>
|
||||
/// <param name="spinner">The spinner.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static StatusContext Spinner(this StatusContext context, Spinner spinner)
|
||||
{
|
||||
if (context is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
context.Spinner = spinner;
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the spinner style.
|
||||
/// </summary>
|
||||
/// <param name="context">The status context.</param>
|
||||
/// <param name="style">The spinner style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static StatusContext SpinnerStyle(this StatusContext context, Style? style)
|
||||
{
|
||||
if (context is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
context.SpinnerStyle = style;
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
62
src/Spectre.Console/Extensions/Progress/StatusExtensions.cs
Normal file
62
src/Spectre.Console/Extensions/Progress/StatusExtensions.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains extension methods for <see cref="Status"/>.
|
||||
/// </summary>
|
||||
public static class StatusExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets whether or not auto refresh is enabled.
|
||||
/// If disabled, you will manually have to refresh the progress.
|
||||
/// </summary>
|
||||
/// <param name="status">The <see cref="Status"/> instance.</param>
|
||||
/// <param name="enabled">Whether or not auto refresh is enabled.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Status AutoRefresh(this Status status, bool enabled)
|
||||
{
|
||||
if (status is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(status));
|
||||
}
|
||||
|
||||
status.AutoRefresh = enabled;
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the spinner.
|
||||
/// </summary>
|
||||
/// <param name="status">The <see cref="Status"/> instance.</param>
|
||||
/// <param name="spinner">The spinner.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Status Spinner(this Status status, Spinner spinner)
|
||||
{
|
||||
if (status is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(status));
|
||||
}
|
||||
|
||||
status.Spinner = spinner;
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the spinner style.
|
||||
/// </summary>
|
||||
/// <param name="status">The <see cref="Status"/> instance.</param>
|
||||
/// <param name="style">The spinner style.</param>
|
||||
/// <returns>The same instance so that multiple calls can be chained.</returns>
|
||||
public static Status SpinnerStyle(this Status status, Style? style)
|
||||
{
|
||||
if (status is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(status));
|
||||
}
|
||||
|
||||
status.SpinnerStyle = style;
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace Spectre.Console
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal static class StringBuilderExtensions
|
||||
{
|
||||
|
||||
@@ -62,10 +62,15 @@ namespace Spectre.Console
|
||||
return text;
|
||||
}
|
||||
|
||||
internal static string NormalizeLineEndings(this string? text, bool native = false)
|
||||
internal static string? RemoveNewLines(this string? text)
|
||||
{
|
||||
return text?.ReplaceExact("\r\n", string.Empty)
|
||||
?.ReplaceExact("\n", string.Empty);
|
||||
}
|
||||
|
||||
internal static string NormalizeNewLines(this string? text, bool native = false)
|
||||
{
|
||||
text = text?.ReplaceExact("\r\n", "\n");
|
||||
text = text?.ReplaceExact("\r", string.Empty);
|
||||
text ??= string.Empty;
|
||||
|
||||
if (native && !_alreadyNormalized)
|
||||
@@ -78,7 +83,7 @@ namespace Spectre.Console
|
||||
|
||||
internal static string[] SplitLines(this string text)
|
||||
{
|
||||
var result = text?.NormalizeLineEndings()?.Split(new[] { '\n' }, StringSplitOptions.None);
|
||||
var result = text?.NormalizeNewLines()?.Split(new[] { '\n' }, StringSplitOptions.None);
|
||||
return result ?? Array.Empty<string>();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
@@ -28,6 +29,11 @@ namespace Spectre.Console
|
||||
/// </summary>
|
||||
IAnsiConsoleInput Input { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the render pipeline.
|
||||
/// </summary>
|
||||
RenderPipeline Pipeline { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the buffer width of the console.
|
||||
/// </summary>
|
||||
@@ -45,9 +51,9 @@ namespace Spectre.Console
|
||||
void Clear(bool home);
|
||||
|
||||
/// <summary>
|
||||
/// Writes a string followed by a line terminator to the console.
|
||||
/// Writes multiple segments to the console.
|
||||
/// </summary>
|
||||
/// <param name="segment">The segment to write.</param>
|
||||
void Write(Segment segment);
|
||||
/// <param name="segments">The segments to write.</param>
|
||||
void Write(IEnumerable<Segment> segments);
|
||||
}
|
||||
}
|
||||
|
||||
24
src/Spectre.Console/InteractionSupport.cs
Normal file
24
src/Spectre.Console/InteractionSupport.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines interactivity support.
|
||||
/// </summary>
|
||||
public enum InteractionSupport
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction support should be
|
||||
/// detected by the system.
|
||||
/// </summary>
|
||||
Detect = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Interactivity is supported.
|
||||
/// </summary>
|
||||
Yes = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Interactivity is not supported.
|
||||
/// </summary>
|
||||
No = 2,
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,8 @@ namespace Spectre.Console.Internal
|
||||
// Enabling failed.
|
||||
return false;
|
||||
}
|
||||
|
||||
isLegacy = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Spectre.Console.Rendering;
|
||||
@@ -11,9 +12,11 @@ namespace Spectre.Console.Internal
|
||||
private readonly AnsiBuilder _ansiBuilder;
|
||||
private readonly AnsiCursor _cursor;
|
||||
private readonly ConsoleInput _input;
|
||||
private readonly object _lock;
|
||||
|
||||
public Capabilities Capabilities { get; }
|
||||
public Encoding Encoding { get; }
|
||||
public RenderPipeline Pipeline { get; }
|
||||
public IAnsiConsoleCursor Cursor => _cursor;
|
||||
public IAnsiConsoleInput Input => _input;
|
||||
|
||||
@@ -49,35 +52,59 @@ namespace Spectre.Console.Internal
|
||||
|
||||
Capabilities = capabilities ?? throw new ArgumentNullException(nameof(capabilities));
|
||||
Encoding = _out.IsStandardOut() ? System.Console.OutputEncoding : Encoding.UTF8;
|
||||
Pipeline = new RenderPipeline();
|
||||
|
||||
_ansiBuilder = new AnsiBuilder(Capabilities, linkHasher);
|
||||
_cursor = new AnsiCursor(this);
|
||||
_input = new ConsoleInput();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
public void Clear(bool home)
|
||||
{
|
||||
Write(Segment.Control("\u001b[2J"));
|
||||
|
||||
if (home)
|
||||
lock (_lock)
|
||||
{
|
||||
Cursor.SetPosition(0, 0);
|
||||
Write(new[] { Segment.Control("\u001b[2J") });
|
||||
|
||||
if (home)
|
||||
{
|
||||
Cursor.SetPosition(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(Segment segment)
|
||||
public void Write(IEnumerable<Segment> segments)
|
||||
{
|
||||
var parts = segment.Text.NormalizeLineEndings().Split(new[] { '\n' });
|
||||
foreach (var (_, _, last, part) in parts.Enumerate())
|
||||
lock (_lock)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(part))
|
||||
var builder = new StringBuilder();
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
_out.Write(_ansiBuilder.GetAnsi(part, segment.Style));
|
||||
if (segment.IsControlCode)
|
||||
{
|
||||
builder.Append(segment.Text);
|
||||
continue;
|
||||
}
|
||||
|
||||
var parts = segment.Text.NormalizeNewLines().Split(new[] { '\n' });
|
||||
foreach (var (_, _, last, part) in parts.Enumerate())
|
||||
{
|
||||
if (!string.IsNullOrEmpty(part))
|
||||
{
|
||||
builder.Append(_ansiBuilder.GetAnsi(part, segment.Style));
|
||||
}
|
||||
|
||||
if (!last)
|
||||
{
|
||||
builder.Append(Environment.NewLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!last)
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
_out.Write(Environment.NewLine);
|
||||
_out.Write(builder.ToString());
|
||||
_out.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,12 +50,18 @@ namespace Spectre.Console.Internal
|
||||
}
|
||||
}
|
||||
|
||||
var supportsInteraction = settings.Interactive == InteractionSupport.Yes;
|
||||
if (settings.Interactive == InteractionSupport.Detect)
|
||||
{
|
||||
supportsInteraction = InteractivityDetector.IsInteractive();
|
||||
}
|
||||
|
||||
var colorSystem = settings.ColorSystem == ColorSystemSupport.Detect
|
||||
? ColorSystemDetector.Detect(supportsAnsi)
|
||||
: (ColorSystem)settings.ColorSystem;
|
||||
|
||||
// Get the capabilities
|
||||
var capabilities = new Capabilities(supportsAnsi, colorSystem, legacyConsole);
|
||||
var capabilities = new Capabilities(supportsAnsi, colorSystem, legacyConsole, supportsInteraction);
|
||||
|
||||
// Create the renderer
|
||||
if (supportsAnsi)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Spectre.Console.Rendering;
|
||||
@@ -14,6 +15,7 @@ namespace Spectre.Console.Internal
|
||||
|
||||
public Capabilities Capabilities { get; }
|
||||
public Encoding Encoding { get; }
|
||||
public RenderPipeline Pipeline { get; }
|
||||
public IAnsiConsoleCursor Cursor => _cursor;
|
||||
public IAnsiConsoleInput Input => _input;
|
||||
|
||||
@@ -43,8 +45,9 @@ namespace Spectre.Console.Internal
|
||||
System.Console.SetOut(@out ?? throw new ArgumentNullException(nameof(@out)));
|
||||
}
|
||||
|
||||
Encoding = System.Console.OutputEncoding;
|
||||
Capabilities = capabilities;
|
||||
Encoding = System.Console.OutputEncoding;
|
||||
Pipeline = new RenderPipeline();
|
||||
}
|
||||
|
||||
public void Clear(bool home)
|
||||
@@ -60,14 +63,22 @@ namespace Spectre.Console.Internal
|
||||
}
|
||||
}
|
||||
|
||||
public void Write(Segment segment)
|
||||
public void Write(IEnumerable<Segment> segments)
|
||||
{
|
||||
if (_lastStyle?.Equals(segment.Style) != true)
|
||||
foreach (var segment in segments)
|
||||
{
|
||||
SetStyle(segment.Style);
|
||||
}
|
||||
if (segment.IsControlCode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
System.Console.Write(segment.Text.NormalizeLineEndings(native: true));
|
||||
if (_lastStyle?.Equals(segment.Style) != true)
|
||||
{
|
||||
SetStyle(segment.Style);
|
||||
}
|
||||
|
||||
System.Console.Write(segment.Text.NormalizeNewLines(native: true));
|
||||
}
|
||||
}
|
||||
|
||||
private void SetStyle(Style style)
|
||||
|
||||
@@ -15,6 +15,11 @@ namespace Spectre.Console.Internal
|
||||
|
||||
foreach (var (_, first, _, segment) in segments.Enumerate())
|
||||
{
|
||||
if (segment.IsControlCode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (segment.Text == "\n" && !first)
|
||||
{
|
||||
builder.Append('\n');
|
||||
|
||||
52
src/Spectre.Console/Internal/InteractivityDetector.cs
Normal file
52
src/Spectre.Console/Internal/InteractivityDetector.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal static class InteractivityDetector
|
||||
{
|
||||
private static readonly Dictionary<string, Func<string, bool>> _environmentVariables;
|
||||
|
||||
static InteractivityDetector()
|
||||
{
|
||||
_environmentVariables = new Dictionary<string, Func<string, bool>>
|
||||
{
|
||||
{ "APPVEYOR", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "bamboo_buildNumber", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "BITBUCKET_REPO_OWNER", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "BITBUCKET_REPO_SLUG", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "BITBUCKET_COMMIT", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "BITRISE_BUILD_URL", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "ContinuaCI.Version", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "CI_SERVER", v => v.Equals("yes", StringComparison.OrdinalIgnoreCase) }, // GitLab
|
||||
{ "GITHUB_ACTIONS", v => v.Equals("true", StringComparison.OrdinalIgnoreCase) },
|
||||
{ "GO_SERVER_URL", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "JENKINS_URL", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "BuildRunner", v => v.Equals("MyGet", StringComparison.OrdinalIgnoreCase) },
|
||||
{ "TEAMCITY_VERSION", v => !string.IsNullOrWhiteSpace(v) },
|
||||
{ "TF_BUILD", v => !string.IsNullOrWhiteSpace(v) }, // TFS and Azure
|
||||
{ "TRAVIS", v => !string.IsNullOrWhiteSpace(v) },
|
||||
};
|
||||
}
|
||||
|
||||
public static bool IsInteractive()
|
||||
{
|
||||
if (!Environment.UserInteractive)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var variable in _environmentVariables)
|
||||
{
|
||||
var func = variable.Value;
|
||||
var value = Environment.GetEnvironmentVariable(variable.Key);
|
||||
if (!string.IsNullOrWhiteSpace(value) && variable.Value(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ namespace Spectre.Console.Internal
|
||||
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
return reader.ReadToEnd().NormalizeLineEndings();
|
||||
return reader.ReadToEnd().NormalizeNewLines();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,32 +8,34 @@ namespace Spectre.Console.Internal
|
||||
{
|
||||
public static int GetCellLength(RenderContext context, string text)
|
||||
{
|
||||
return text.Sum(rune =>
|
||||
{
|
||||
if (context.LegacyConsole)
|
||||
{
|
||||
// Is it represented by a single byte?
|
||||
// In that case we don't have to calculate the
|
||||
// actual cell width.
|
||||
if (context.Encoding.GetByteCount(new[] { rune }) == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return text.Sum(rune => GetCellLength(context, rune));
|
||||
}
|
||||
|
||||
// TODO: We need to figure out why Segment.SplitLines fails
|
||||
// if we let wcwidth (which returns -1 instead of 1)
|
||||
// calculate the size for new line characters.
|
||||
// That is correct from a Unicode perspective, but the
|
||||
// algorithm was written before wcwidth was added and used
|
||||
// to work with string length and not cell length.
|
||||
if (rune == '\n')
|
||||
public static int GetCellLength(RenderContext context, char rune)
|
||||
{
|
||||
if (context.LegacyConsole)
|
||||
{
|
||||
// Is it represented by a single byte?
|
||||
// In that case we don't have to calculate the
|
||||
// actual cell width.
|
||||
if (context.Encoding.GetByteCount(new[] { rune }) == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return UnicodeCalculator.GetWidth(rune);
|
||||
});
|
||||
// TODO: We need to figure out why Segment.SplitLines fails
|
||||
// if we let wcwidth (which returns -1 instead of 1)
|
||||
// calculate the size for new line characters.
|
||||
// That is correct from a Unicode perspective, but the
|
||||
// algorithm was written before wcwidth was added and used
|
||||
// to work with string length and not cell length.
|
||||
if (rune == '\n')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return UnicodeCalculator.GetWidth(rune);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ namespace Spectre.Console.Internal
|
||||
|
||||
foreach (var segment in Segment.Merge(segments))
|
||||
{
|
||||
if (segment.IsControlCode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
builder.Append(segment.Text);
|
||||
}
|
||||
|
||||
|
||||
35
src/Spectre.Console/Progress/Columns/PercentageColumn.cs
Normal file
35
src/Spectre.Console/Progress/Columns/PercentageColumn.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A column showing task progress in percentage.
|
||||
/// </summary>
|
||||
public sealed class PercentageColumn : ProgressColumn
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the style for a non-complete task.
|
||||
/// </summary>
|
||||
public Style Style { get; set; } = Style.Plain;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style for a completed task.
|
||||
/// </summary>
|
||||
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Green);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
|
||||
{
|
||||
var percentage = (int)task.Percentage;
|
||||
var style = percentage == 100 ? CompletedStyle : Style ?? Style.Plain;
|
||||
return new Text($"{percentage}%", style).RightAligned();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int? GetColumnWidth(RenderContext context)
|
||||
{
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
45
src/Spectre.Console/Progress/Columns/ProgressBarColumn.cs
Normal file
45
src/Spectre.Console/Progress/Columns/ProgressBarColumn.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A column showing task progress as a progress bar.
|
||||
/// </summary>
|
||||
public sealed class ProgressBarColumn : ProgressColumn
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the width of the column.
|
||||
/// </summary>
|
||||
public int? Width { get; set; } = 40;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of completed portions of the progress bar.
|
||||
/// </summary>
|
||||
public Style CompletedStyle { get; set; } = new Style(foreground: Color.Yellow);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of a finished progress bar.
|
||||
/// </summary>
|
||||
public Style FinishedStyle { get; set; } = new Style(foreground: Color.Green);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of remaining portions of the progress bar.
|
||||
/// </summary>
|
||||
public Style RemainingStyle { get; set; } = new Style(foreground: Color.Grey);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
|
||||
{
|
||||
return new ProgressBar
|
||||
{
|
||||
MaxValue = task.MaxValue,
|
||||
Value = task.Value,
|
||||
Width = Width,
|
||||
CompletedStyle = CompletedStyle,
|
||||
FinishedStyle = FinishedStyle,
|
||||
RemainingStyle = RemainingStyle,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/Spectre.Console/Progress/Columns/RemainingTimeColumn.cs
Normal file
37
src/Spectre.Console/Progress/Columns/RemainingTimeColumn.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A column showing the remaining time of a task.
|
||||
/// </summary>
|
||||
public sealed class RemainingTimeColumn : ProgressColumn
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected internal override bool NoWrap => true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of the remaining time text.
|
||||
/// </summary>
|
||||
public Style Style { get; set; } = new Style(foreground: Color.Blue);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
|
||||
{
|
||||
var remaining = task.RemainingTime;
|
||||
if (remaining == null)
|
||||
{
|
||||
return new Markup("-:--:--");
|
||||
}
|
||||
|
||||
return new Text($"{remaining.Value:h\\:mm\\:ss}", Style ?? Style.Plain);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int? GetColumnWidth(RenderContext context)
|
||||
{
|
||||
return 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
107
src/Spectre.Console/Progress/Columns/SpinnerColumn.cs
Normal file
107
src/Spectre.Console/Progress/Columns/SpinnerColumn.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A column showing a spinner.
|
||||
/// </summary>
|
||||
public sealed class SpinnerColumn : ProgressColumn
|
||||
{
|
||||
private const string ACCUMULATED = "SPINNER_ACCUMULATED";
|
||||
private const string INDEX = "SPINNER_INDEX";
|
||||
|
||||
private readonly object _lock;
|
||||
private Spinner _spinner;
|
||||
private int? _maxWidth;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected internal override bool NoWrap => true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="Console.Spinner"/>.
|
||||
/// </summary>
|
||||
public Spinner Spinner
|
||||
{
|
||||
get => _spinner;
|
||||
set
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_spinner = value ?? Spinner.Known.Default;
|
||||
_maxWidth = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the style of the spinner.
|
||||
/// </summary>
|
||||
public Style? Style { get; set; } = new Style(foreground: Color.Yellow);
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.
|
||||
/// </summary>
|
||||
public SpinnerColumn()
|
||||
: this(Spinner.Known.Default)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SpinnerColumn"/> class.
|
||||
/// </summary>
|
||||
/// <param name="spinner">The spinner to use.</param>
|
||||
public SpinnerColumn(Spinner spinner)
|
||||
{
|
||||
_spinner = spinner ?? throw new ArgumentNullException(nameof(spinner));
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
|
||||
{
|
||||
var useAscii = (context.LegacyConsole || !context.Unicode) && _spinner.IsUnicode;
|
||||
var spinner = useAscii ? Spinner.Known.Ascii : _spinner ?? Spinner.Known.Default;
|
||||
|
||||
if (!task.IsStarted || task.IsFinished)
|
||||
{
|
||||
return new Markup(new string(' ', GetMaxWidth(context)));
|
||||
}
|
||||
|
||||
var accumulated = task.State.Update<double>(ACCUMULATED, acc => acc + deltaTime.TotalMilliseconds);
|
||||
if (accumulated >= spinner.Interval.TotalMilliseconds)
|
||||
{
|
||||
task.State.Update<double>(ACCUMULATED, _ => 0);
|
||||
task.State.Update<int>(INDEX, index => index + 1);
|
||||
}
|
||||
|
||||
var index = task.State.Get<int>(INDEX);
|
||||
var frame = spinner.Frames[index % spinner.Frames.Count];
|
||||
return new Markup(frame.EscapeMarkup(), Style ?? Style.Plain);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override int? GetColumnWidth(RenderContext context)
|
||||
{
|
||||
return GetMaxWidth(context);
|
||||
}
|
||||
|
||||
private int GetMaxWidth(RenderContext context)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_maxWidth == null)
|
||||
{
|
||||
var useAscii = (context.LegacyConsole || !context.Unicode) && _spinner.IsUnicode;
|
||||
var spinner = useAscii ? Spinner.Known.Ascii : _spinner ?? Spinner.Known.Default;
|
||||
|
||||
_maxWidth = spinner.Frames.Max(frame => Cell.GetCellLength(context, frame));
|
||||
}
|
||||
|
||||
return _maxWidth.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// A column showing the task description.
|
||||
/// </summary>
|
||||
public sealed class TaskDescriptionColumn : ProgressColumn
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected internal override bool NoWrap => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime)
|
||||
{
|
||||
var text = task.Description?.RemoveNewLines()?.Trim();
|
||||
return new Markup(text ?? string.Empty).Overflow(Overflow.Ellipsis).RightAligned();
|
||||
}
|
||||
}
|
||||
}
|
||||
129
src/Spectre.Console/Progress/Progress.cs
Normal file
129
src/Spectre.Console/Progress/Progress.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Internal;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a task list.
|
||||
/// </summary>
|
||||
public sealed class Progress
|
||||
{
|
||||
private readonly IAnsiConsole _console;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not task list should auto refresh.
|
||||
/// Defaults to <c>true</c>.
|
||||
/// </summary>
|
||||
public bool AutoRefresh { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not the task list should
|
||||
/// be cleared once it completes.
|
||||
/// Defaults to <c>false</c>.
|
||||
/// </summary>
|
||||
public bool AutoClear { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the refresh rate if <c>AutoRefresh</c> is enabled.
|
||||
/// Defaults to 10 times/second.
|
||||
/// </summary>
|
||||
public TimeSpan RefreshRate { get; set; } = TimeSpan.FromMilliseconds(100);
|
||||
|
||||
internal List<ProgressColumn> Columns { get; }
|
||||
|
||||
internal ProgressRenderer? FallbackRenderer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Progress"/> class.
|
||||
/// </summary>
|
||||
/// <param name="console">The console to render to.</param>
|
||||
public Progress(IAnsiConsole console)
|
||||
{
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
|
||||
// Initialize with default columns
|
||||
Columns = new List<ProgressColumn>
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the progress task list.
|
||||
/// </summary>
|
||||
/// <param name="action">The action to execute.</param>
|
||||
public void Start(Action<ProgressContext> action)
|
||||
{
|
||||
var task = StartAsync(ctx =>
|
||||
{
|
||||
action(ctx);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the progress task list.
|
||||
/// </summary>
|
||||
/// <param name="action">The action to execute.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
public async Task StartAsync(Func<ProgressContext, Task> action)
|
||||
{
|
||||
if (action is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(action));
|
||||
}
|
||||
|
||||
var renderer = CreateRenderer();
|
||||
renderer.Started();
|
||||
|
||||
try
|
||||
{
|
||||
using (new RenderHookScope(_console, renderer))
|
||||
{
|
||||
var context = new ProgressContext(_console, renderer);
|
||||
|
||||
if (AutoRefresh)
|
||||
{
|
||||
using (var thread = new ProgressRefreshThread(context, renderer.RefreshRate))
|
||||
{
|
||||
await action(context).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await action(context).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
context.Refresh();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
renderer.Completed(AutoClear);
|
||||
}
|
||||
}
|
||||
|
||||
private ProgressRenderer CreateRenderer()
|
||||
{
|
||||
var caps = _console.Capabilities;
|
||||
var interactive = caps.SupportsInteraction && caps.SupportsAnsi;
|
||||
|
||||
if (interactive)
|
||||
{
|
||||
var columns = new List<ProgressColumn>(Columns);
|
||||
return new DefaultProgressRenderer(_console, columns, RefreshRate);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FallbackRenderer ?? new FallbackProgressRenderer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
src/Spectre.Console/Progress/ProgressColumn.cs
Normal file
35
src/Spectre.Console/Progress/ProgressColumn.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a progress column.
|
||||
/// </summary>
|
||||
public abstract class ProgressColumn
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not content should not wrap.
|
||||
/// </summary>
|
||||
protected internal virtual bool NoWrap { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a renderable representing the column.
|
||||
/// </summary>
|
||||
/// <param name="context">The render context.</param>
|
||||
/// <param name="task">The task.</param>
|
||||
/// <param name="deltaTime">The elapsed time since last call.</param>
|
||||
/// <returns>A renderable representing the column.</returns>
|
||||
public abstract IRenderable Render(RenderContext context, ProgressTask task, TimeSpan deltaTime);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of the column.
|
||||
/// </summary>
|
||||
/// <param name="context">The context.</param>
|
||||
/// <returns>The width of the column, or <c>null</c> to calculate.</returns>
|
||||
public virtual int? GetColumnWidth(RenderContext context)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
70
src/Spectre.Console/Progress/ProgressContext.cs
Normal file
70
src/Spectre.Console/Progress/ProgressContext.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a context that can be used to interact with a <see cref="Progress"/>.
|
||||
/// </summary>
|
||||
public sealed class ProgressContext
|
||||
{
|
||||
private readonly List<ProgressTask> _tasks;
|
||||
private readonly object _taskLock;
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly ProgressRenderer _renderer;
|
||||
private int _taskId;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not all tasks have completed.
|
||||
/// </summary>
|
||||
public bool IsFinished => _tasks.All(task => task.IsFinished);
|
||||
|
||||
internal Encoding Encoding => _console.Encoding;
|
||||
|
||||
internal ProgressContext(IAnsiConsole console, ProgressRenderer renderer)
|
||||
{
|
||||
_tasks = new List<ProgressTask>();
|
||||
_taskLock = new object();
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a task.
|
||||
/// </summary>
|
||||
/// <param name="description">The task description.</param>
|
||||
/// <param name="settings">The task settings.</param>
|
||||
/// <returns>The task's ID.</returns>
|
||||
public ProgressTask AddTask(string description, ProgressTaskSettings? settings = null)
|
||||
{
|
||||
lock (_taskLock)
|
||||
{
|
||||
settings ??= new ProgressTaskSettings();
|
||||
var task = new ProgressTask(_taskId++, description, settings.MaxValue, settings.AutoStart);
|
||||
|
||||
_tasks.Add(task);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the current progress.
|
||||
/// </summary>
|
||||
public void Refresh()
|
||||
{
|
||||
_renderer.Update(this);
|
||||
_console.Render(new ControlSequence(string.Empty));
|
||||
}
|
||||
|
||||
internal IReadOnlyList<ProgressTask> GetTasks()
|
||||
{
|
||||
lock (_taskLock)
|
||||
{
|
||||
return new List<ProgressTask>(_tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
58
src/Spectre.Console/Progress/ProgressRefreshThread.cs
Normal file
58
src/Spectre.Console/Progress/ProgressRefreshThread.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal sealed class ProgressRefreshThread : IDisposable
|
||||
{
|
||||
private readonly ProgressContext _context;
|
||||
private readonly TimeSpan _refreshRate;
|
||||
private readonly ManualResetEvent _running;
|
||||
private readonly ManualResetEvent _stopped;
|
||||
private readonly Thread? _thread;
|
||||
|
||||
public ProgressRefreshThread(ProgressContext context, TimeSpan refreshRate)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
_refreshRate = refreshRate;
|
||||
_running = new ManualResetEvent(false);
|
||||
_stopped = new ManualResetEvent(false);
|
||||
|
||||
_thread = new Thread(Run);
|
||||
_thread.IsBackground = true;
|
||||
_thread.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_thread == null || !_running.WaitOne(0))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_stopped.Set();
|
||||
_thread.Join();
|
||||
|
||||
_stopped.Dispose();
|
||||
_running.Dispose();
|
||||
}
|
||||
|
||||
private void Run()
|
||||
{
|
||||
_running.Set();
|
||||
|
||||
try
|
||||
{
|
||||
while (!_stopped.WaitOne(_refreshRate))
|
||||
{
|
||||
_context.Refresh();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stopped.Reset();
|
||||
_running.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/Spectre.Console/Progress/ProgressRenderer.cs
Normal file
22
src/Spectre.Console/Progress/ProgressRenderer.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal abstract class ProgressRenderer : IRenderHook
|
||||
{
|
||||
public abstract TimeSpan RefreshRate { get; }
|
||||
|
||||
public virtual void Started()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Completed(bool clear)
|
||||
{
|
||||
}
|
||||
|
||||
public abstract void Update(ProgressContext context);
|
||||
public abstract IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables);
|
||||
}
|
||||
}
|
||||
16
src/Spectre.Console/Progress/ProgressSample.cs
Normal file
16
src/Spectre.Console/Progress/ProgressSample.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal readonly struct ProgressSample
|
||||
{
|
||||
public double Value { get; }
|
||||
public DateTime Timestamp { get; }
|
||||
|
||||
public ProgressSample(DateTime timestamp, double value)
|
||||
{
|
||||
Timestamp = timestamp;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
281
src/Spectre.Console/Progress/ProgressTask.cs
Normal file
281
src/Spectre.Console/Progress/ProgressTask.cs
Normal file
@@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a progress task.
|
||||
/// </summary>
|
||||
public sealed class ProgressTask
|
||||
{
|
||||
private readonly List<ProgressSample> _samples;
|
||||
private readonly object _lock;
|
||||
|
||||
private double _maxValue;
|
||||
private string _description;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the task ID.
|
||||
/// </summary>
|
||||
public int Id { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the task description.
|
||||
/// </summary>
|
||||
public string Description
|
||||
{
|
||||
get => _description;
|
||||
set => Update(description: value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the max value of the task.
|
||||
/// </summary>
|
||||
public double MaxValue
|
||||
{
|
||||
get => _maxValue;
|
||||
set => Update(maxValue: value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the task.
|
||||
/// </summary>
|
||||
public double Value { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the start time of the task.
|
||||
/// </summary>
|
||||
public DateTime? StartTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the stop time of the task.
|
||||
/// </summary>
|
||||
public DateTime? StopTime { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the task state.
|
||||
/// </summary>
|
||||
public ProgressTaskState State { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the task has started.
|
||||
/// </summary>
|
||||
public bool IsStarted => StartTime != null;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the task has finished.
|
||||
/// </summary>
|
||||
public bool IsFinished => Value >= MaxValue;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the percentage done of the task.
|
||||
/// </summary>
|
||||
public double Percentage => GetPercentage();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the speed measured in steps/second.
|
||||
/// </summary>
|
||||
public double? Speed => GetSpeed();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the elapsed time.
|
||||
/// </summary>
|
||||
public TimeSpan? ElapsedTime => GetElapsedTime();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the remaining time.
|
||||
/// </summary>
|
||||
public TimeSpan? RemainingTime => GetRemainingTime();
|
||||
|
||||
internal ProgressTask(int id, string description, double maxValue, bool autoStart)
|
||||
{
|
||||
_samples = new List<ProgressSample>();
|
||||
_lock = new object();
|
||||
_maxValue = maxValue;
|
||||
|
||||
_description = description?.RemoveNewLines()?.Trim() ?? throw new ArgumentNullException(nameof(description));
|
||||
if (string.IsNullOrWhiteSpace(_description))
|
||||
{
|
||||
throw new ArgumentException("Task name cannot be empty", nameof(description));
|
||||
}
|
||||
|
||||
Id = id;
|
||||
State = new ProgressTaskState();
|
||||
Value = 0;
|
||||
StartTime = autoStart ? DateTime.Now : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the task.
|
||||
/// </summary>
|
||||
public void StartTask()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
StartTime = DateTime.Now;
|
||||
StopTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the task.
|
||||
/// </summary>
|
||||
public void StopTask()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
if (StartTime == null)
|
||||
{
|
||||
StartTime = now;
|
||||
}
|
||||
|
||||
StopTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increments the task's value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to increment with.</param>
|
||||
public void Increment(double value)
|
||||
{
|
||||
Update(increment: value);
|
||||
}
|
||||
|
||||
private void Update(
|
||||
string? description = null,
|
||||
double? maxValue = null,
|
||||
double? increment = null)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var startValue = Value;
|
||||
|
||||
if (description != null)
|
||||
{
|
||||
description = description?.RemoveNewLines()?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(description))
|
||||
{
|
||||
throw new InvalidOperationException("Task name cannot be empty.");
|
||||
}
|
||||
|
||||
_description = description;
|
||||
}
|
||||
|
||||
if (maxValue != null)
|
||||
{
|
||||
_maxValue = maxValue.Value;
|
||||
}
|
||||
|
||||
if (increment != null)
|
||||
{
|
||||
Value += increment.Value;
|
||||
}
|
||||
|
||||
// Need to cap the max value?
|
||||
if (Value > _maxValue)
|
||||
{
|
||||
Value = _maxValue;
|
||||
}
|
||||
|
||||
var timestamp = DateTime.Now;
|
||||
var threshold = timestamp - TimeSpan.FromSeconds(30);
|
||||
|
||||
// Remove samples that's too old
|
||||
while (_samples.Count > 0 && _samples[0].Timestamp < threshold)
|
||||
{
|
||||
_samples.RemoveAt(0);
|
||||
}
|
||||
|
||||
// Keep maximum of 1000 samples
|
||||
while (_samples.Count > 1000)
|
||||
{
|
||||
_samples.RemoveAt(0);
|
||||
}
|
||||
|
||||
_samples.Add(new ProgressSample(timestamp, Value - startValue));
|
||||
}
|
||||
}
|
||||
|
||||
private double GetPercentage()
|
||||
{
|
||||
var percentage = (Value / MaxValue) * 100;
|
||||
percentage = Math.Min(100, Math.Max(0, percentage));
|
||||
return percentage;
|
||||
}
|
||||
|
||||
private double? GetSpeed()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (StartTime == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_samples.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var totalTime = _samples.Last().Timestamp - _samples[0].Timestamp;
|
||||
if (totalTime == TimeSpan.Zero)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var totalCompleted = _samples.Sum(x => x.Value);
|
||||
return totalCompleted / totalTime.TotalSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
private TimeSpan? GetElapsedTime()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (StartTime == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (StopTime != null)
|
||||
{
|
||||
return StopTime - StartTime;
|
||||
}
|
||||
|
||||
return DateTime.Now - StartTime;
|
||||
}
|
||||
}
|
||||
|
||||
private TimeSpan? GetRemainingTime()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (IsFinished)
|
||||
{
|
||||
return TimeSpan.Zero;
|
||||
}
|
||||
|
||||
var speed = GetSpeed();
|
||||
if (speed == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// If the speed is zero, the estimate below
|
||||
// will return infinity (since it's a double),
|
||||
// so let's set the speed to 1 in that case.
|
||||
if (speed == 0)
|
||||
{
|
||||
speed = 1;
|
||||
}
|
||||
|
||||
var estimate = (MaxValue - Value) / speed.Value;
|
||||
return TimeSpan.FromSeconds(estimate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/Spectre.Console/Progress/ProgressTaskSettings.cs
Normal file
20
src/Spectre.Console/Progress/ProgressTaskSettings.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents settings for a progress task.
|
||||
/// </summary>
|
||||
public sealed class ProgressTaskSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the task's max value.
|
||||
/// Defaults to <c>100</c>.
|
||||
/// </summary>
|
||||
public double MaxValue { get; set; } = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not the task
|
||||
/// will be auto started. Defaults to <c>true</c>.
|
||||
/// </summary>
|
||||
public bool AutoStart { get; set; } = true;
|
||||
}
|
||||
}
|
||||
81
src/Spectre.Console/Progress/ProgressTaskState.cs
Normal file
81
src/Spectre.Console/Progress/ProgressTaskState.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents progress task state.
|
||||
/// </summary>
|
||||
public sealed class ProgressTaskState
|
||||
{
|
||||
private readonly Dictionary<string, object> _state;
|
||||
private readonly object _lock;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ProgressTaskState"/> class.
|
||||
/// </summary>
|
||||
public ProgressTaskState()
|
||||
{
|
||||
_state = new Dictionary<string, object>();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the state value for the specified key.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The state value type.</typeparam>
|
||||
/// <param name="key">The state key.</param>
|
||||
/// <returns>The value for the specified key.</returns>
|
||||
public T Get<T>(string key)
|
||||
where T : struct
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_state.TryGetValue(key, out var value))
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
if (!(value is T))
|
||||
{
|
||||
throw new InvalidOperationException("State value is of the wrong type.");
|
||||
}
|
||||
|
||||
return (T)value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a task state value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The state value type.</typeparam>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="func">The transformation function.</param>
|
||||
/// <returns>The updated value.</returns>
|
||||
public T Update<T>(string key, Func<T, T> func)
|
||||
where T : struct
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (func is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(func));
|
||||
}
|
||||
|
||||
var old = default(T);
|
||||
if (_state.TryGetValue(key, out var value))
|
||||
{
|
||||
if (!(value is T))
|
||||
{
|
||||
throw new InvalidOperationException("State value is of the wrong type.");
|
||||
}
|
||||
|
||||
old = (T)value;
|
||||
}
|
||||
|
||||
_state[key] = func(old);
|
||||
return (T)_state[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal sealed class DefaultProgressRenderer : ProgressRenderer
|
||||
{
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly List<ProgressColumn> _columns;
|
||||
private readonly LiveRenderable _live;
|
||||
private readonly object _lock;
|
||||
private readonly Stopwatch _stopwatch;
|
||||
private TimeSpan _lastUpdate;
|
||||
|
||||
public override TimeSpan RefreshRate { get; }
|
||||
|
||||
public DefaultProgressRenderer(IAnsiConsole console, List<ProgressColumn> columns, TimeSpan refreshRate)
|
||||
{
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
_columns = columns ?? throw new ArgumentNullException(nameof(columns));
|
||||
_live = new LiveRenderable();
|
||||
_lock = new object();
|
||||
_stopwatch = new Stopwatch();
|
||||
_lastUpdate = TimeSpan.Zero;
|
||||
|
||||
RefreshRate = refreshRate;
|
||||
}
|
||||
|
||||
public override void Started()
|
||||
{
|
||||
_console.Cursor.Hide();
|
||||
}
|
||||
|
||||
public override void Completed(bool clear)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (clear)
|
||||
{
|
||||
_console.Render(_live.RestoreCursor());
|
||||
}
|
||||
else
|
||||
{
|
||||
_console.WriteLine();
|
||||
}
|
||||
|
||||
_console.Cursor.Show();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(ProgressContext context)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_stopwatch.IsRunning)
|
||||
{
|
||||
_stopwatch.Start();
|
||||
}
|
||||
|
||||
var renderContext = new RenderContext(_console.Encoding, _console.Capabilities.LegacyConsole);
|
||||
|
||||
var delta = _stopwatch.Elapsed - _lastUpdate;
|
||||
_lastUpdate = _stopwatch.Elapsed;
|
||||
|
||||
var grid = new Grid();
|
||||
for (var columnIndex = 0; columnIndex < _columns.Count; columnIndex++)
|
||||
{
|
||||
var column = new GridColumn().PadRight(1);
|
||||
|
||||
var columnWidth = _columns[columnIndex].GetColumnWidth(renderContext);
|
||||
if (columnWidth != null)
|
||||
{
|
||||
column.Width = columnWidth;
|
||||
}
|
||||
|
||||
if (_columns[columnIndex].NoWrap)
|
||||
{
|
||||
column.NoWrap();
|
||||
}
|
||||
|
||||
// Last column?
|
||||
if (columnIndex == _columns.Count - 1)
|
||||
{
|
||||
column.PadRight(0);
|
||||
}
|
||||
|
||||
grid.AddColumn(column);
|
||||
}
|
||||
|
||||
// Add rows
|
||||
foreach (var task in context.GetTasks())
|
||||
{
|
||||
var columns = _columns.Select(column => column.Render(renderContext, task, delta));
|
||||
grid.AddRow(columns.ToArray());
|
||||
}
|
||||
|
||||
_live.SetRenderable(new Padder(grid, new Padding(0, 1)));
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
yield return _live.PositionCursor();
|
||||
|
||||
foreach (var renderable in renderables)
|
||||
{
|
||||
yield return renderable;
|
||||
}
|
||||
|
||||
yield return _live;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal sealed class FallbackProgressRenderer : ProgressRenderer
|
||||
{
|
||||
private const double FirstMilestone = 25;
|
||||
private static readonly double?[] _milestones = new double?[] { FirstMilestone, 50, 75, 95, 96, 97, 98, 99, 100 };
|
||||
|
||||
private readonly Dictionary<int, double> _taskMilestones;
|
||||
private readonly object _lock;
|
||||
private IRenderable? _renderable;
|
||||
private DateTime _lastUpdate;
|
||||
|
||||
public override TimeSpan RefreshRate => TimeSpan.FromSeconds(1);
|
||||
|
||||
public FallbackProgressRenderer()
|
||||
{
|
||||
_taskMilestones = new Dictionary<int, double>();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
public override void Update(ProgressContext context)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var hasStartedTasks = false;
|
||||
var updates = new List<(string, double)>();
|
||||
|
||||
foreach (var task in context.GetTasks())
|
||||
{
|
||||
if (!task.IsStarted || task.IsFinished)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
hasStartedTasks = true;
|
||||
|
||||
if (TryAdvance(task.Id, task.Percentage))
|
||||
{
|
||||
updates.Add((task.Description, task.Percentage));
|
||||
}
|
||||
}
|
||||
|
||||
// Got started tasks but no updates for 30 seconds?
|
||||
if (hasStartedTasks && updates.Count == 0 && (DateTime.Now - _lastUpdate) > TimeSpan.FromSeconds(30))
|
||||
{
|
||||
foreach (var task in context.GetTasks())
|
||||
{
|
||||
updates.Add((task.Description, task.Percentage));
|
||||
}
|
||||
}
|
||||
|
||||
if (updates.Count > 0)
|
||||
{
|
||||
_lastUpdate = DateTime.Now;
|
||||
}
|
||||
|
||||
_renderable = BuildTaskGrid(updates);
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var result = new List<IRenderable>();
|
||||
result.AddRange(renderables);
|
||||
|
||||
if (_renderable != null)
|
||||
{
|
||||
result.Add(_renderable);
|
||||
}
|
||||
|
||||
_renderable = null;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryAdvance(int task, double percentage)
|
||||
{
|
||||
if (!_taskMilestones.TryGetValue(task, out var milestone))
|
||||
{
|
||||
_taskMilestones.Add(task, FirstMilestone);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (percentage > milestone)
|
||||
{
|
||||
var nextMilestone = GetNextMilestone(percentage);
|
||||
if (nextMilestone != null && _taskMilestones[task] != nextMilestone)
|
||||
{
|
||||
_taskMilestones[task] = nextMilestone.Value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static double? GetNextMilestone(double percentage)
|
||||
{
|
||||
return Array.Find(_milestones, p => p > percentage);
|
||||
}
|
||||
|
||||
private static IRenderable? BuildTaskGrid(List<(string Name, double Percentage)> updates)
|
||||
{
|
||||
if (updates.Count > 0)
|
||||
{
|
||||
var renderables = new List<IRenderable>();
|
||||
foreach (var (name, percentage) in updates)
|
||||
{
|
||||
renderables.Add(new Markup($"[blue]{name}[/]: {(int)percentage}%"));
|
||||
}
|
||||
|
||||
return new Rows(renderables);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
namespace Spectre.Console.Internal
|
||||
{
|
||||
internal sealed class StatusFallbackRenderer : ProgressRenderer
|
||||
{
|
||||
private readonly object _lock;
|
||||
private IRenderable? _renderable;
|
||||
private string? _lastStatus;
|
||||
|
||||
public override TimeSpan RefreshRate => TimeSpan.FromMilliseconds(100);
|
||||
|
||||
public StatusFallbackRenderer()
|
||||
{
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
public override void Update(ProgressContext context)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var task = context.GetTasks().SingleOrDefault();
|
||||
if (task != null)
|
||||
{
|
||||
// Not same description?
|
||||
if (_lastStatus != task.Description)
|
||||
{
|
||||
_lastStatus = task.Description;
|
||||
_renderable = new Markup(task.Description + Environment.NewLine);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_renderable = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<IRenderable> Process(RenderContext context, IEnumerable<IRenderable> renderables)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var result = new List<IRenderable>();
|
||||
result.AddRange(renderables);
|
||||
|
||||
if (_renderable != null)
|
||||
{
|
||||
result.Add(_renderable);
|
||||
}
|
||||
|
||||
_renderable = null;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1873
src/Spectre.Console/Progress/Spinner.Generated.cs
Normal file
1873
src/Spectre.Console/Progress/Spinner.Generated.cs
Normal file
File diff suppressed because it is too large
Load Diff
27
src/Spectre.Console/Progress/Spinner.cs
Normal file
27
src/Spectre.Console/Progress/Spinner.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a spinner used in a <see cref="SpinnerColumn"/>.
|
||||
/// </summary>
|
||||
public abstract partial class Spinner
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the update interval for the spinner.
|
||||
/// </summary>
|
||||
public abstract TimeSpan Interval { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether or not the spinner
|
||||
/// uses Unicode characters.
|
||||
/// </summary>
|
||||
public abstract bool IsUnicode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the spinner frames.
|
||||
/// </summary>
|
||||
public abstract IReadOnlyList<string> Frames { get; }
|
||||
}
|
||||
}
|
||||
89
src/Spectre.Console/Progress/Status.cs
Normal file
89
src/Spectre.Console/Progress/Status.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Spectre.Console.Internal;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a status display.
|
||||
/// </summary>
|
||||
public sealed class Status
|
||||
{
|
||||
private readonly IAnsiConsole _console;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the spinner.
|
||||
/// </summary>
|
||||
public Spinner? Spinner { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the spinner style.
|
||||
/// </summary>
|
||||
public Style? SpinnerStyle { get; set; } = new Style(foreground: Color.Yellow);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not status
|
||||
/// should auto refresh. Defaults to <c>true</c>.
|
||||
/// </summary>
|
||||
public bool AutoRefresh { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Status"/> class.
|
||||
/// </summary>
|
||||
/// <param name="console">The console.</param>
|
||||
public Status(IAnsiConsole console)
|
||||
{
|
||||
_console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a new status display.
|
||||
/// </summary>
|
||||
/// <param name="status">The status to display.</param>
|
||||
/// <param name="action">he action to execute.</param>
|
||||
public void Start(string status, Action<StatusContext> action)
|
||||
{
|
||||
var task = StartAsync(status, ctx =>
|
||||
{
|
||||
action(ctx);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts a new status display.
|
||||
/// </summary>
|
||||
/// <param name="status">The status to display.</param>
|
||||
/// <param name="action">he action to execute.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
public async Task StartAsync(string status, Func<StatusContext, Task> action)
|
||||
{
|
||||
// Set the progress columns
|
||||
var spinnerColumn = new SpinnerColumn(Spinner ?? Spinner.Known.Default)
|
||||
{
|
||||
Style = SpinnerStyle ?? Style.Plain,
|
||||
};
|
||||
|
||||
var progress = new Progress(_console)
|
||||
{
|
||||
FallbackRenderer = new StatusFallbackRenderer(),
|
||||
AutoClear = true,
|
||||
AutoRefresh = AutoRefresh,
|
||||
};
|
||||
|
||||
progress.Columns(new ProgressColumn[]
|
||||
{
|
||||
spinnerColumn,
|
||||
new TaskDescriptionColumn(),
|
||||
});
|
||||
|
||||
await progress.StartAsync(async ctx =>
|
||||
{
|
||||
var statusContext = new StatusContext(ctx, ctx.AddTask(status), spinnerColumn);
|
||||
await action(statusContext).ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
76
src/Spectre.Console/Progress/StatusContext.cs
Normal file
76
src/Spectre.Console/Progress/StatusContext.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
|
||||
namespace Spectre.Console
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a context that can be used to interact with a <see cref="Status"/>.
|
||||
/// </summary>
|
||||
public sealed class StatusContext
|
||||
{
|
||||
private readonly ProgressContext _context;
|
||||
private readonly ProgressTask _task;
|
||||
private readonly SpinnerColumn _spinnerColumn;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the current status.
|
||||
/// </summary>
|
||||
public string Status
|
||||
{
|
||||
get => _task.Description;
|
||||
set => SetStatus(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the current spinner.
|
||||
/// </summary>
|
||||
public Spinner Spinner
|
||||
{
|
||||
get => _spinnerColumn.Spinner;
|
||||
set => SetSpinner(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the current spinner style.
|
||||
/// </summary>
|
||||
public Style? SpinnerStyle
|
||||
{
|
||||
get => _spinnerColumn.Style;
|
||||
set => _spinnerColumn.Style = value;
|
||||
}
|
||||
|
||||
internal StatusContext(ProgressContext context, ProgressTask task, SpinnerColumn spinnerColumn)
|
||||
{
|
||||
_context = context ?? throw new ArgumentNullException(nameof(context));
|
||||
_task = task ?? throw new ArgumentNullException(nameof(task));
|
||||
_spinnerColumn = spinnerColumn ?? throw new ArgumentNullException(nameof(spinnerColumn));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refreshes the status.
|
||||
/// </summary>
|
||||
public void Refresh()
|
||||
{
|
||||
_context.Refresh();
|
||||
}
|
||||
|
||||
private void SetStatus(string status)
|
||||
{
|
||||
if (status is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(status));
|
||||
}
|
||||
|
||||
_task.Description = status;
|
||||
}
|
||||
|
||||
private void SetSpinner(Spinner spinner)
|
||||
{
|
||||
if (spinner is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(spinner));
|
||||
}
|
||||
|
||||
_spinnerColumn.Spinner = spinner;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Spectre.Console.Rendering;
|
||||
|
||||
@@ -8,7 +10,8 @@ namespace Spectre.Console
|
||||
/// <summary>
|
||||
/// A console recorder used to record output from a console.
|
||||
/// </summary>
|
||||
public sealed class Recorder : IAnsiConsole, IDisposable
|
||||
[SuppressMessage("Design", "CA1063:Implement IDisposable Correctly")]
|
||||
public class Recorder : IAnsiConsole, IDisposable
|
||||
{
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly List<Segment> _recorded;
|
||||
@@ -31,6 +34,14 @@ namespace Spectre.Console
|
||||
/// <inheritdoc/>
|
||||
public int Height => _console.Height;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public RenderPipeline Pipeline => _console.Pipeline;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list containing all recorded segments.
|
||||
/// </summary>
|
||||
protected List<Segment> Recorded => _recorded;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Recorder"/> class.
|
||||
/// </summary>
|
||||
@@ -42,6 +53,7 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
[SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize")]
|
||||
public void Dispose()
|
||||
{
|
||||
// Only used for scoping.
|
||||
@@ -54,20 +66,25 @@ namespace Spectre.Console
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Write(Segment segment)
|
||||
public void Write(IEnumerable<Segment> segments)
|
||||
{
|
||||
if (segment is null)
|
||||
if (segments is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(segment));
|
||||
throw new ArgumentNullException(nameof(segments));
|
||||
}
|
||||
|
||||
// Don't record control codes.
|
||||
if (!segment.IsControlCode)
|
||||
{
|
||||
_recorded.Add(segment);
|
||||
}
|
||||
Record(segments);
|
||||
|
||||
_console.Write(segment);
|
||||
_console.Write(segments);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records the specified segments.
|
||||
/// </summary>
|
||||
/// <param name="segments">The segments to be recorded.</param>
|
||||
protected virtual void Record(IEnumerable<Segment> segments)
|
||||
{
|
||||
Recorded.AddRange(segments.Where(s => !s.IsControlCode));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user