Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spectre console refactor #10

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CommandLine/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.42.0" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions CommandLine/Commands/AssemblerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Spectre.Console.Cli;
using System.ComponentModel;

namespace CommandLine.Commands
{
public class AssemblerSettings : BasicSettings
{
[CommandOption("--base <BASE>")]
[Description("What base to use for font glyph data.")]
[TypeConverter(typeof(NumberBaseConverter))]
[DefaultValue("hex")]
public NumberBase Base { get; set; }
}
}
13 changes: 13 additions & 0 deletions CommandLine/Commands/BasicSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Spectre.Console.Cli;

namespace CommandLine.Commands
{
public class BasicSettings : CommandSettings
{
[CommandArgument(0, "[FileGlob]")]
public string Glob { get; set; }

[CommandArgument(1, "[OutputFolder]")]
public string OutputFolder { get; set; }
}
}
20 changes: 20 additions & 0 deletions CommandLine/Commands/ConvertToA8Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using PixelWorld;
using PixelWorld.Machines;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Convert to Atari 8-bit binary font file")]
public class ConvertToAtari8BitCommand : Command<BasicSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] BasicSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
Converter.ConvertToAtari8(files, Spectrum.UK, settings.OutputFolder);
return 0;
}
}
}
20 changes: 20 additions & 0 deletions CommandLine/Commands/ConvertToC64Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using PixelWorld;
using PixelWorld.Machines;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Convert to Commodore 64 binary font formats")]
public class ConvertToC64Command : Command<BasicSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] BasicSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
Converter.ConvertToC64(files, Spectrum.UK, settings.OutputFolder);
return 0;
}
}
}
20 changes: 20 additions & 0 deletions CommandLine/Commands/ConvertToCPCCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using PixelWorld;
using PixelWorld.Machines;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Convert to Amstrad CPC BASIC file")]
public class ConvertToCPCCommand : Command<BasicSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] BasicSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
Converter.ConvertToAmstradCPC(files, Spectrum.UK, settings.OutputFolder);
return 0;
}
}
}
27 changes: 27 additions & 0 deletions CommandLine/Commands/ConvertToFZXCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using PixelWorld;
using PixelWorld.Machines;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
public class FZXSettings : BasicSettings
{
[CommandOption("--proportional")]
[Description("Make the font proportional by left aligning and detecting width")]
public bool Proportional { get; set; }
}

[Description("Convert to FZX-format font")]
public class ConvertToFZXCommand : Command<FZXSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] FZXSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
Converter.ConvertToFZX(files, Spectrum.UK, settings.Proportional, settings.OutputFolder);
return 0;
}
}
}
19 changes: 19 additions & 0 deletions CommandLine/Commands/DumpCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PixelWorld;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Produce raw memory dumps from snapshots")]
public class DumpCommand : Command<BasicSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] BasicSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
Dumper.Dump(files, settings.OutputFolder);
return 0;
}
}
}
19 changes: 19 additions & 0 deletions CommandLine/Commands/HuntCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PixelWorld;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Hunt dumps for possible fonts")]
public class HuntCommand : Command<BasicSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] BasicSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
FontHunter.Hunt(files, settings.OutputFolder);
return 0;
}
}
}
27 changes: 27 additions & 0 deletions CommandLine/Commands/M6502AsmCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using PixelWorld;
using PixelWorld.Formatters;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Generate 6502 assembly def files")]
public class M6502AsmCommand : Command<AssemblerSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] AssemblerSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
switch (settings.Base)
{
case NumberBase.Decimal:
AssemblyFontFormatter.GenAsmHex("6502", ".byte ", "${0:x2}", files, settings.OutputFolder);
break;
default:
AssemblyFontFormatter.GenAsmHex("6502", ".byte ", "{0}", files, settings.OutputFolder);
break;
}
return 0;
}
}
}
27 changes: 27 additions & 0 deletions CommandLine/Commands/M68000AsmCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using PixelWorld;
using PixelWorld.Formatters;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Generate Intel 8086 assembly def files")]
public class X86AsmCommand : Command<AssemblerSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] AssemblerSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
switch (settings.Base)
{
case NumberBase.Decimal:
AssemblyFontFormatter.GenAsmHex("x86", "db\t", "0x{0:x2}", files, settings.OutputFolder);
break;
default:
AssemblyFontFormatter.GenAsmHex("x86", "db\t", "{0}", files, settings.OutputFolder);
break;
}
return 0;
}
}
}
48 changes: 48 additions & 0 deletions CommandLine/Commands/NumberBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;

namespace CommandLine.Commands
{
public enum NumberBase
{
Binary = 2,
Decimal = 10,
Hex = 16,
}

public sealed class NumberBaseConverter : TypeConverter
{
private readonly Dictionary<string, NumberBase> lookup;

public NumberBaseConverter()
{
lookup = new Dictionary<string, NumberBase>(StringComparer.OrdinalIgnoreCase)
{
{ "hex", NumberBase.Hex },
{ "16", NumberBase.Hex },
{ "decimal", NumberBase.Decimal },
{ "10", NumberBase.Decimal },
{ "binary", NumberBase.Binary },
{ "2", NumberBase.Binary },
};
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string stringValue)
{
var result = lookup.TryGetValue(stringValue, out var numberBase);
if (!result)
{
const string format = "The value '{0}' is not a number base.";
var message = string.Format(CultureInfo.InvariantCulture, format, value);
throw new InvalidOperationException(message);
}
return numberBase;
}
throw new NotSupportedException("Can't convert value to number base.");
}
}
}
19 changes: 19 additions & 0 deletions CommandLine/Commands/PreviewCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PixelWorld;
using PixelWorld.Tools;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Generate PNG previews of fonts")]
public class PreviewCommand : Command<BasicSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] BasicSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
PreviewCreator.Preview(files, settings.OutputFolder);
return 0;
}
}
}
27 changes: 27 additions & 0 deletions CommandLine/Commands/X86AsmCommand.cs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using PixelWorld;
using PixelWorld.Formatters;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Generate Motorola 68000 assembly def files")]
public class M68000AsmCommand : Command<AssemblerSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] AssemblerSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
switch (settings.Base)
{
case NumberBase.Decimal:
AssemblyFontFormatter.GenAsmHex("68000", "DC.B ", "${0:x2}", files, settings.OutputFolder);
break;
default:
AssemblyFontFormatter.GenAsmHex("68000", "DC.B ", "{0}", files, settings.OutputFolder);
break;
}
return 0;
}
}
}
30 changes: 30 additions & 0 deletions CommandLine/Commands/Z80AsmCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using PixelWorld;
using PixelWorld.Formatters;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace CommandLine.Commands
{
[Description("Generate Z80 assembly def files")]
public class Z80AsmCommand : Command<AssemblerSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] AssemblerSettings settings)
{
var files = Utils.MatchGlobWithFiles(settings.Glob);
switch (settings.Base)
{
case NumberBase.Binary:
AssemblyFontFormatter.GenZ80AsmBinary(files, settings.OutputFolder);
break;
case NumberBase.Decimal:
AssemblyFontFormatter.GenAsmHex("z80", "defb ", "{0}", files, settings.OutputFolder);
break;
default:
AssemblyFontFormatter.GenAsmHex("z80", "defb ", "&{0:x2}", files, settings.OutputFolder);
break;
}
return 0;
}
}
}
Loading