mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-01 01:25:27 +08:00 
			
		
		
		
	Rename folder
This commit is contained in:
		
							
								
								
									
										2
									
								
								scripts/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								scripts/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| Generated | ||||
| Temp | ||||
							
								
								
									
										24
									
								
								scripts/Generate-Colors.ps1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								scripts/Generate-Colors.ps1
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| ########################################################## | ||||
| # Script that generates known colors and lookup tables. | ||||
| ########################################################## | ||||
|  | ||||
| $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 -- colors "$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" "Color.Generated.cs") -Destination "$Source/Color.Generated.cs" | ||||
| Copy-Item  (Join-Path "$Output" "ColorPalette.Generated.cs") -Destination "$Source/Internal/Colors/ColorPalette.Generated.cs" | ||||
| Copy-Item  (Join-Path "$Output" "ColorTable.Generated.cs") -Destination "$Source/Internal/Colors/ColorTable.Generated.cs" | ||||
							
								
								
									
										59
									
								
								scripts/Generator/Commands/ColorGeneratorCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								scripts/Generator/Commands/ColorGeneratorCommand.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| using System; | ||||
| using System.IO; | ||||
| using Generator.Models; | ||||
| using Scriban; | ||||
| using Spectre.Cli; | ||||
| using Spectre.IO; | ||||
|  | ||||
| namespace Generator.Commands | ||||
| { | ||||
|     public sealed class ColorGeneratorCommand : Command<GeneratorCommandSettings> | ||||
|     { | ||||
|         private readonly IFileSystem _fileSystem; | ||||
|  | ||||
|         public ColorGeneratorCommand() | ||||
|         { | ||||
|             _fileSystem = new FileSystem(); | ||||
|         } | ||||
|  | ||||
|         public override int Execute(CommandContext context, GeneratorCommandSettings settings) | ||||
|         { | ||||
|             var templates = new FilePath[] | ||||
|             { | ||||
|                 "Templates/ColorPalette.Generated.template", | ||||
|                 "Templates/Color.Generated.template", | ||||
|                 "Templates/ColorTable.Generated.template" | ||||
|             }; | ||||
|  | ||||
|             // Read the color model. | ||||
|             var model = Color.Parse(File.ReadAllText("Data/colors.json")); | ||||
|  | ||||
|             var output = new DirectoryPath(settings.Output); | ||||
|             if (!_fileSystem.Directory.Exists(settings.Output)) | ||||
|             { | ||||
|                 _fileSystem.Directory.Create(settings.Output); | ||||
|             } | ||||
|  | ||||
|             foreach (var templatePath in templates) | ||||
|             { | ||||
|                 // Parse the Scriban template. | ||||
|                 var template = Template.Parse(File.ReadAllText(templatePath.FullPath)); | ||||
|  | ||||
|                 // Render the template with the model. | ||||
|                 var result = template.Render(new { Colors = model }); | ||||
|  | ||||
|                 // Write output to file | ||||
|                 var file = output.CombineWithFilePath(templatePath.GetFilename().ChangeExtension(".cs")); | ||||
|                 File.WriteAllText(file.FullPath, result); | ||||
|             } | ||||
|  | ||||
|             return 0; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public sealed class GeneratorCommandSettings : CommandSettings | ||||
|     { | ||||
|         [CommandArgument(0, "<OUTPUT>")] | ||||
|         public string Output { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										3842
									
								
								scripts/Generator/Data/colors.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3842
									
								
								scripts/Generator/Data/colors.json
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										36
									
								
								scripts/Generator/Generator.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								scripts/Generator/Generator.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <OutputType>Exe</OutputType> | ||||
|     <TargetFramework>netcoreapp3.1</TargetFramework> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <Compile Remove="out\**" /> | ||||
|     <EmbeddedResource Remove="out\**" /> | ||||
|     <None Remove="out\**" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <None Update="Data\colors.json"> | ||||
|       <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
|     </None> | ||||
|     <None Update="Templates\ColorTable.Generated.template"> | ||||
|       <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
|     </None> | ||||
|     <None Update="Templates\Color.Generated.template"> | ||||
|       <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
|     </None> | ||||
|     <None Update="Templates\ColorPalette.Generated.template"> | ||||
|       <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||||
|     </None> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||||
|     <PackageReference Include="Scriban" Version="2.1.3" /> | ||||
|     <PackageReference Include="Spectre.Cli" Version="0.36.1-preview.0.6" /> | ||||
|     <PackageReference Include="Spectre.IO" Version="0.1.0" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
							
								
								
									
										37
									
								
								scripts/Generator/Generator.sln
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								scripts/Generator/Generator.sln
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
|  | ||||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||||
| # Visual Studio Version 16 | ||||
| VisualStudioVersion = 16.0.30320.27 | ||||
| MinimumVisualStudioVersion = 15.0.26124.0 | ||||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "Generator.csproj", "{5668D267-53E3-4B99-97AE-59AA597D22ED}" | ||||
| EndProject | ||||
| Global | ||||
| 	GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
| 		Debug|Any CPU = Debug|Any CPU | ||||
| 		Debug|x64 = Debug|x64 | ||||
| 		Debug|x86 = Debug|x86 | ||||
| 		Release|Any CPU = Release|Any CPU | ||||
| 		Release|x64 = Release|x64 | ||||
| 		Release|x86 = Release|x86 | ||||
| 	EndGlobalSection | ||||
| 	GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x64.ActiveCfg = Debug|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x64.Build.0 = Debug|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x86.ActiveCfg = Debug|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Debug|x86.Build.0 = Debug|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|Any CPU.Build.0 = Release|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x64.ActiveCfg = Release|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x64.Build.0 = Release|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x86.ActiveCfg = Release|Any CPU | ||||
| 		{5668D267-53E3-4B99-97AE-59AA597D22ED}.Release|x86.Build.0 = Release|Any CPU | ||||
| 	EndGlobalSection | ||||
| 	GlobalSection(SolutionProperties) = preSolution | ||||
| 		HideSolutionNode = FALSE | ||||
| 	EndGlobalSection | ||||
| 	GlobalSection(ExtensibilityGlobals) = postSolution | ||||
| 		SolutionGuid = {5F37FDE3-D591-4D43-8DDE-2ED6BAB0A7B4} | ||||
| 	EndGlobalSection | ||||
| EndGlobal | ||||
							
								
								
									
										62
									
								
								scripts/Generator/Models/Color.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								scripts/Generator/Models/Color.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace Generator.Models | ||||
| { | ||||
|     public sealed class Color | ||||
|     { | ||||
|         public int Number { get; set; } | ||||
|         public string Hex { get; set; } | ||||
|         public string Name { get; set; } | ||||
|         public Rgb Rgb { get; set; } | ||||
|  | ||||
|         public int R => Rgb.R; | ||||
|         public int G => Rgb.G; | ||||
|         public int B => Rgb.B; | ||||
|  | ||||
|         public static IEnumerable<Color> Parse(string json) | ||||
|         { | ||||
|             var source = JsonConvert.DeserializeObject<List<Color>>(json); | ||||
|  | ||||
|             var check = new Dictionary<string, Color>(StringComparer.OrdinalIgnoreCase); | ||||
|             foreach (var color in source.OrderBy(c => c.Number)) | ||||
|             { | ||||
|                 if (!check.ContainsKey(color.Name)) | ||||
|                 { | ||||
|                     check.Add(color.Name, color); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     var newName = (string)null; | ||||
|                     for (int i = 1; i < 100; i++) | ||||
|                     { | ||||
|                         if (!check.ContainsKey($"{color.Name}_{i}")) | ||||
|                         { | ||||
|                             newName = $"{color.Name}_{i}"; | ||||
|                             break; | ||||
|                         } | ||||
|                     } | ||||
|  | ||||
|                     if (newName == null) | ||||
|                     { | ||||
|                         throw new InvalidOperationException("Impossible!"); | ||||
|                     } | ||||
|  | ||||
|                     check.Add(newName, color); | ||||
|                     color.Name = newName; | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             return source; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public sealed class Rgb | ||||
|     { | ||||
|         public int R { get; set; } | ||||
|         public int G { get; set; } | ||||
|         public int B { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										14
									
								
								scripts/Generator/Models/ColorModel.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								scripts/Generator/Models/ColorModel.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| using System.Collections.Generic; | ||||
|  | ||||
| namespace Generator.Models | ||||
| { | ||||
|     public sealed class ColorModel | ||||
|     { | ||||
|         public List<Color> Colors { get; set; } | ||||
|  | ||||
|         public ColorModel(IEnumerable<Color> colors) | ||||
|         { | ||||
|             Colors = new List<Color>(colors); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										6
									
								
								scripts/Generator/Models/Palette.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								scripts/Generator/Models/Palette.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| namespace Generator.Models | ||||
| { | ||||
|     public sealed class Palette | ||||
|     { | ||||
|     } | ||||
| } | ||||
							
								
								
									
										19
									
								
								scripts/Generator/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								scripts/Generator/Program.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| using Generator.Commands; | ||||
| using Spectre.Cli; | ||||
|  | ||||
| namespace Generator | ||||
| { | ||||
|     public static class Program | ||||
|     { | ||||
|         public static int Main(string[] args) | ||||
|         { | ||||
|             var app = new CommandApp(); | ||||
|             app.Configure(config => | ||||
|             { | ||||
|                 config.AddCommand<ColorGeneratorCommand>("colors"); | ||||
|             }); | ||||
|  | ||||
|             return app.Run(args); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										36
									
								
								scripts/Generator/Templates/Color.Generated.template
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								scripts/Generator/Templates/Color.Generated.template
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| //------------------------------------------------------------------------------ | ||||
| // <auto-generated> | ||||
| //     This code was generated by a tool. | ||||
| //     Generated {{ date.now | date.to_string `%Y-%m-%d %k:%M` }} | ||||
| // | ||||
| //     Changes to this file may cause incorrect behavior and will be lost if | ||||
| //     the code is regenerated. | ||||
| // </auto-generated> | ||||
| //------------------------------------------------------------------------------ | ||||
|  | ||||
| using System.Diagnostics.CodeAnalysis; | ||||
|  | ||||
| namespace Spectre.Console | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Represents a color. | ||||
|     /// </summary> | ||||
|     public partial struct Color | ||||
|     { | ||||
|         internal Color(byte number, byte red, byte green, byte blue, bool isDefault = false) | ||||
|             : this(red, green, blue) | ||||
|         { | ||||
|             Number = number; | ||||
|             IsDefault = isDefault; | ||||
|         } | ||||
|         {{~ for color in colors }} | ||||
|         /// <summary> | ||||
|         /// Gets the color "{{ color.name }}" (RGB {{ color.r }},{{ color.g }},{{ color.b }}). | ||||
|         /// </summary> | ||||
|         {{- if string.contains color.name "_" }} | ||||
|         [SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores")] | ||||
|         {{- end}} | ||||
|         public static Color {{ color.name }} { get; } = new Color({{ color.number }}, {{ color.r }}, {{ color.g }}, {{ color.b }}); | ||||
|         {{~ end ~}} | ||||
|     } | ||||
| } | ||||
							
								
								
									
										47
									
								
								scripts/Generator/Templates/ColorPalette.Generated.template
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								scripts/Generator/Templates/ColorPalette.Generated.template
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| //------------------------------------------------------------------------------ | ||||
| // <auto-generated> | ||||
| //     This code was generated by a tool. | ||||
| //     Generated {{ date.now | date.to_string `%Y-%m-%d %k:%M` }} | ||||
| // | ||||
| //     Changes to this file may cause incorrect behavior and will be lost if | ||||
| //     the code is regenerated. | ||||
| // </auto-generated> | ||||
| //------------------------------------------------------------------------------ | ||||
|  | ||||
| using System.Collections.Generic; | ||||
|  | ||||
| namespace Spectre.Console.Internal | ||||
| { | ||||
|     internal static partial class ColorPalette | ||||
|     { | ||||
|         private static List<Color> GenerateLegacyPalette() | ||||
|         { | ||||
|             return new List<Color> | ||||
|             { | ||||
|                 {{~ for number in 0..7 ~}} | ||||
|                 Color.{{ colors[number].name }}, | ||||
|                 {{~ end ~}} | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|         private static List<Color> GenerateStandardPalette(IReadOnlyList<Color> legacy) | ||||
|         { | ||||
|             return new List<Color>(legacy) | ||||
|             { | ||||
|                 {{~ for number in 8..15 ~}} | ||||
|                 Color.{{ colors[number].name }}, | ||||
|                 {{~ end ~}} | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|         private static List<Color> GenerateEightBitPalette(IReadOnlyList<Color> standard) | ||||
|         { | ||||
|             return new List<Color>(standard) | ||||
|             { | ||||
|                 {{~ for number in 16..255 ~}} | ||||
|                 Color.{{ colors[number].name }}, | ||||
|                 {{~ end ~}} | ||||
|             }; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										28
									
								
								scripts/Generator/Templates/ColorTable.Generated.template
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								scripts/Generator/Templates/ColorTable.Generated.template
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| //------------------------------------------------------------------------------ | ||||
| // <auto-generated> | ||||
| //     This code was generated by a tool. | ||||
| //     Generated {{ date.now | date.to_string `%Y-%m-%d %k:%M` }} | ||||
| // | ||||
| //     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.Internal | ||||
| { | ||||
|     internal static partial class ColorTable | ||||
|     { | ||||
|         private static Dictionary<string, int> GenerateTable() | ||||
|         { | ||||
|             return new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) | ||||
|             { | ||||
|                 {{~ for color in colors ~}} | ||||
|                 { "{{ string.downcase color.name }}", {{ color.number }} }, | ||||
|                 {{~ end ~}} | ||||
|             }; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Patrik Svensson
					Patrik Svensson