RSCG – AutoSpectre

RSCG – AutoSpectre
 
 

name AutoSpectre
nuget https://www.nuget.org/packages/AutoSpectre.SourceGeneration
https://www.nuget.org/packages/AutoSpectre
link https://github.com/jeppevammenkristensen/auto-spectre
author Jeppe Roi Kristensen

Generating prompt to input values for a console application.

 

This is how you can use AutoSpectre .

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
 
  <ItemGroup>
    <PackageReference Include="AutoSpectre" Version="0.8.1" />
    <PackageReference Include="AutoSpectre.SourceGeneration" Version="0.8.1" />
  </ItemGroup>
    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>
</Project>

The code that you will use is

1
2
3
4
5
6
7
using ReadData;
Console.WriteLine("Hello, World!");
 
IReadPersonSpectreFactory factory = new ReadPersonSpectreFactory();
var person = factory.Get();
//Console.WriteLine($"Hello, {person.FirstName} {person.LastName}!");
person.SpectreDump();
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
using AutoSpectre;
 
namespace ReadData;
[AutoSpectreForm()]
public class ReadPerson
{
 
    [TextPrompt(Title = "Enter first name", DefaultValueStyle = "bold",
        DefaultValueSource = nameof(FirstNameDefaultValue))]
    public string? FirstName { get; set; }
    public readonly string? FirstNameDefaultValue = "Andrei";
 
    [TextPrompt(PromptStyle = "green bold")]
    public string? LastName { get; set; }
    public readonly string? LastNameDefaultValue = "Ignat";
 
 
}

 

The code that is generated is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Immutable;
using System.Globalization;
using AutoSpectre.Extensions;
using Spectre.Console.Rendering;
 
namespace ReadData
{
    /// <summary>
    /// Helps create and fill <see cref = "ReadPerson"/> with values
    /// </summary>
    public interface IReadPersonSpectreFactory
    {
        ReadPerson Get(ReadPerson destination = null);
    }
 
    /// <summary>
    /// Helps create and fill <see cref = "ReadPerson"/> with values
    /// </summary>
    public class ReadPersonSpectreFactory : IReadPersonSpectreFactory
    {
        public ReadPerson Get(ReadPerson destination = null)
        {
            destination ??= new ReadData.ReadPerson();
            var culture = CultureInfo.CurrentUICulture;
            destination.FirstName = AnsiConsole.Prompt(new TextPrompt<string?>("Enter first name").AllowEmpty().WithCulture(culture).DefaultValue(destination.FirstNameDefaultValue).DefaultValueStyle("bold"));
            destination.LastName = AnsiConsole.Prompt(new TextPrompt<string?>("Enter [green]LastName[/]").AllowEmpty().WithCulture(culture).DefaultValue(destination.LastNameDefaultValue).PromptStyle("green bold"));
            return destination;
        }
    }
 
    public static class ReadPersonSpectreFactoryExtensions
    {
        public static ReadPerson SpectrePrompt(this ReadPerson source)
        {
            ReadPersonSpectreFactory factory = new();
            return factory.Get(source);
        }
 
        /// <summary>
        /// Returns data as a IRenderable
        /// Experimental. Might break
        /// </summary>
        /// <returns></returns>
        public static IRenderable GenerateTable(this ReadPerson source)
        {
            var table = new Table();
            table.AddColumn(new TableColumn("Name"));
            table.AddColumn(new TableColumn("Value"));
            table.AddRow(new Markup("FirstName"), new Markup(source.FirstName?.ToString()));
            table.AddRow(new Markup("LastName"), new Markup(source.LastName?.ToString()));
            return table;
        }
 
        /// <summary>
        /// Renders the table
        /// Experimental. Might break
        /// </summary>
        /// <returns></returns>
        public static void SpectreDump(this ReadPerson source)
        {
            AnsiConsole.Write(source.GenerateTable());
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoSpectre