Skip to content

Commit

Permalink
Migrated Spectre.Console to Spectre.Console.Rx
Browse files Browse the repository at this point in the history
Converted elements to use reactive code where required.
Unfortunately the original source doesn't have any DI setup for swap out of components.
  • Loading branch information
ChrisPulman committed Aug 26, 2023
1 parent 3881e9b commit e37be76
Show file tree
Hide file tree
Showing 365 changed files with 40,582 additions and 286 deletions.
1 change: 0 additions & 1 deletion build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ partial class Build : NukeBuild
}
PackagesDirectory.CreateOrCleanDirectory();
////await this.UpdateVisualStudio();
await this.InstallDotNetSdk("3.1.x", "5.x.x", "6.x.x", "7.x.x");
});

Expand Down
2 changes: 1 addition & 1 deletion src/LiveExample/LiveExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\Spectre.Console.Rx\Spectre.Console.Rx.csproj" />
</ItemGroup>

</Project>
8 changes: 2 additions & 6 deletions src/LiveExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Reactive.Linq;
using Spectre.Console;
using Spectre.Console.Rx;

namespace Live;
Expand All @@ -24,7 +23,7 @@ public static void Main()
AnsiConsoleRx.Live(table, ld => ld.AutoClear(false).Overflow(VerticalOverflow.Ellipsis).Cropping(VerticalOverflowCropping.Top))
.ObserveOn(AnsiConsoleRx.Scheduler)
.Subscribe(ctx =>
{
// Columns
ctx.Update(230, () => table.AddColumn("Release date"))
.Update(230, () => table.AddColumn("Title"))
Expand Down Expand Up @@ -76,9 +75,6 @@ public static void Main()
.Update(230, () => table.SimpleHeavyBorder())
// Caption
.Update(400, () => table.Caption("[[ [blue]THE END[/] ]]"));
});

Console.ReadLine();
.Update(400, () => table.Caption("[[ [blue]THE END[/] ]]")));
}
}
2 changes: 1 addition & 1 deletion src/LiveTableExample/LiveTableExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\Spectre.Console.Rx\Spectre.Console.Rx.csproj" />
</ItemGroup>

</Project>
33 changes: 16 additions & 17 deletions src/LiveTableExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

using System;
using System.Linq;
using System.Threading.Tasks;
using Spectre.Console;
using System.Reactive.Linq;
using Spectre.Console.Rx;

namespace LiveTable;
Expand Down Expand Up @@ -45,31 +44,31 @@ public static void Main(string[] args)
p.AutoClear(false)
.Overflow(VerticalOverflow.Ellipsis)
.Cropping(VerticalOverflowCropping.Bottom))
.Subscribe(async ctx =>
.ObserveOn(AnsiConsoleRx.Scheduler)
.Do(_ =>
{
// Add some initial rows
foreach (var a in Enumerable.Range(0, NumberOfRows))
{
AddExchangeRateRow(table);
}
})
.CombineLatest(Observable.Interval(TimeSpan.FromMilliseconds(400)), (ctx, _) => ctx)
.Subscribe(ctx =>
{
// Continously update the table
while (true)
// More rows than we want?
if (table.Rows.Count > NumberOfRows)
{
// More rows than we want?
if (table.Rows.Count > NumberOfRows)
{
// Remove the first one
table.Rows.RemoveAt(0);
}
// Remove the first one
table.Rows.RemoveAt(0);
}
// Add a new row
AddExchangeRateRow(table);
// Add a new row
AddExchangeRateRow(table);
// Refresh and wait for a while
ctx.Refresh();
await Task.Delay(400);
}
// Refresh and wait for a while
ctx.Refresh();
});

Console.ReadLine();
Expand Down
73 changes: 32 additions & 41 deletions src/ProgressExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading;
using Spectre.Console;
using Spectre.Console.Rx;

namespace Progress;
Expand All @@ -23,7 +21,8 @@ public static void Main()
AnsiConsole.MarkupLine("[yellow]Initializing warp drive[/]...");

// Show progress
AnsiConsoleRx.Progress(p => p.AutoClear(false)
AnsiConsoleRx.Progress(p =>
p.AutoClear(false)
.Columns(new ProgressColumn[]
{
new TaskDescriptionColumn(), // Task description
Expand All @@ -33,72 +32,64 @@ public static void Main()
new SpinnerColumn(), // Spinner
}))
.ObserveOn(AnsiConsoleRx.Scheduler)
.Subscribe(ctx =>
.Subscribe(async ctx =>
{
var random = new Random(DateTime.Now.Millisecond);
// Create some tasks
var tasks = CreateTasks(ctx, random);
var warpTask = ctx.AddTask("Going to warp", autoStart: false).IsIndeterminate();
// Wait for all tasks (except the indeterminate one) to complete
while (!ctx.IsFinished)
{
// Increment progress
foreach (var (task, increment) in tasks)
await ctx.Schedule(
TimeSpan.FromMilliseconds(100),
() => ctx.IsFinished,
() =>
{
task.Increment(random.NextDouble() * increment);
}
// Write some random things to the terminal
if (random.NextDouble() < 0.1)
{
WriteLogMessage();
}
ctx.Refresh();
// Simulate some delay
Thread.Sleep(100);
}
// 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();
}
});
// Now start the "warp" task
warpTask.StartTask();
warpTask.IsIndeterminate(false);
while (!ctx.IsFinished)
{
warpTask.Increment(12 * random.NextDouble());
// Simulate some delay
Thread.Sleep(100);
}
warpTask.StartTask().IsIndeterminate(false);
await ctx.Schedule(
TimeSpan.FromMilliseconds(100),
() => warpTask.Increment(12 * random.NextDouble()));
// Done
AnsiConsole.MarkupLine("[green]Done![/]");
await ctx.Schedule(_ => WriteLogMessage("[green]Done![/]"));
});

Console.ReadLine();
}

private static List<(ProgressTask Task, int Delay)> CreateTasks(ProgressContext progress, Random random)
private static List<(ProgressTask Task, int Delay)> CreateTasks(ProgressContext context, 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)));
tasks.Add((context.AddTask(name), random.Next(2, 10)));
}
}

return tasks;
}

private static void WriteLogMessage()
{
AnsiConsole.MarkupLine(
private static void WriteLogMessage() => AnsiConsole.MarkupLine(
"[grey]LOG:[/] " +
DescriptionGenerator.Generate() +
"[grey]...[/]");
}

private static void WriteLogMessage(string message) => AnsiConsole.MarkupLine(
"[grey]LOG:[/] " +
message +
"[grey]...[/]");
}
2 changes: 1 addition & 1 deletion src/ProgressExample/ProgressExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\Spectre.Console.Rx\Spectre.Console.Rx.csproj" />
</ItemGroup>

</Project>
21 changes: 5 additions & 16 deletions src/ProgressThreadExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Spectre.Console;
using Spectre.Console.Rx;

namespace Progress;
Expand Down Expand Up @@ -33,28 +32,18 @@ public static void Main()
{
ctx.AddTask("Deleting Custom Formats").MaxValue(cfs.Count),
})
.ObserveOn(AnsiConsoleRx.Scheduler)
.Subscribe(
ctx =>
{
var rng = new Random();
cfs.ToObservable()
.Select(x => Observable.FromAsync(async ct =>
{
await Task.Delay(TimeSpan.FromSeconds(rng.Next(1, 5)), ct);
}))
.Select(_ => Observable.FromAsync(async ct => await Task.Delay(TimeSpan.FromSeconds(rng.Next(1, 5)), ct), AnsiConsoleRx.Scheduler))
.Merge(8)
.Subscribe(_ =>
{
ctx.tasks[0].Increment(1); // Not thread safe; needs to invoke on the thread that subscribed; not sure if this is ObserveOn or SubscribeOn
});
.ObserveOn(SpectreConsoleScheduler.Instance)
.Subscribe(_ => ctx.tasks[0].Increment(1));
},
() =>
{
// Render a message when the sequence completes
AnsiConsole.MarkupLine("[blue]Done![/]");
});

Console.ReadLine();
() => AnsiConsole.MarkupLine("[blue]Done![/]")); // Render a message when the sequence completes
}
}
2 changes: 1 addition & 1 deletion src/ProgressThreadExample/ProgressThreadExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\Spectre.Console.Rx\Spectre.Console.Rx.csproj" />
</ItemGroup>

</Project>
17 changes: 0 additions & 17 deletions src/Shared/Shared.csproj

This file was deleted.

11 changes: 2 additions & 9 deletions src/Spectre.Console.Rx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spectre.Console.Rx", "Spect
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LiveExample", "LiveExample\LiveExample.csproj", "{3EBF3829-4483-48ED-AD1E-E057CC7DDFF0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{725C299C-42CE-405F-85CC-6DEFEA8487B9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProgressExample", "ProgressExample\ProgressExample.csproj", "{D3195DF3-668F-4BF0-9020-22C6649924EF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{E17E245C-D501-46B4-A804-A68241982088}"
Expand All @@ -27,8 +25,6 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{15A30A6D-9A94-4A7C-A74B-D7A9CB36C216}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15A30A6D-9A94-4A7C-A74B-D7A9CB36C216}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43DC5A6B-B066-4CCF-90A8-680432C139D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{43DC5A6B-B066-4CCF-90A8-680432C139D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43DC5A6B-B066-4CCF-90A8-680432C139D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -37,10 +33,6 @@ Global
{3EBF3829-4483-48ED-AD1E-E057CC7DDFF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EBF3829-4483-48ED-AD1E-E057CC7DDFF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EBF3829-4483-48ED-AD1E-E057CC7DDFF0}.Release|Any CPU.Build.0 = Release|Any CPU
{725C299C-42CE-405F-85CC-6DEFEA8487B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{725C299C-42CE-405F-85CC-6DEFEA8487B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{725C299C-42CE-405F-85CC-6DEFEA8487B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{725C299C-42CE-405F-85CC-6DEFEA8487B9}.Release|Any CPU.Build.0 = Release|Any CPU
{D3195DF3-668F-4BF0-9020-22C6649924EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3195DF3-668F-4BF0-9020-22C6649924EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3195DF3-668F-4BF0-9020-22C6649924EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -57,13 +49,14 @@ Global
{CB209C31-2CD2-4188-A088-18E68DF23788}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB209C31-2CD2-4188-A088-18E68DF23788}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB209C31-2CD2-4188-A088-18E68DF23788}.Release|Any CPU.Build.0 = Release|Any CPU
{15A30A6D-9A94-4A7C-A74B-D7A9CB36C216}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15A30A6D-9A94-4A7C-A74B-D7A9CB36C216}.Release|Any CPU.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3EBF3829-4483-48ED-AD1E-E057CC7DDFF0} = {E17E245C-D501-46B4-A804-A68241982088}
{725C299C-42CE-405F-85CC-6DEFEA8487B9} = {E17E245C-D501-46B4-A804-A68241982088}
{D3195DF3-668F-4BF0-9020-22C6649924EF} = {E17E245C-D501-46B4-A804-A68241982088}
{9B8D02EB-C0CB-42B1-B44C-74471D83A8C2} = {E17E245C-D501-46B4-A804-A68241982088}
{51ACCE14-F44D-455B-A5E9-AEC71F7C6F61} = {E17E245C-D501-46B4-A804-A68241982088}
Expand Down
Loading

0 comments on commit e37be76

Please sign in to comment.