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


<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


using ReadData;
Console.WriteLine("Hello, World!");

IReadPersonSpectreFactory factory = new ReadPersonSpectreFactory();
var person = factory.Get();
//Console.WriteLine($"Hello, {person.FirstName} {person.LastName}!");
person.SpectreDump();




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

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