Skip to content

Commit

Permalink
keyboard ui navigation in menu mode (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpannella authored Sep 9, 2023
1 parent 2bd93e0 commit 744ccf4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pocket_updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.33.1</Version>
<Version>2.34.0</Version>
<Description>Keep your Analogue Pocket up to date</Description>
<Copyright>2023 Matt Pannella</Copyright>
<Authors>Matt Pannella</Authors>
Expand All @@ -15,6 +15,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="ConsoleMenu-simple" Version="2.6.1" />
<PackageReference Include="Crc32.NET" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
Expand Down
89 changes: 81 additions & 8 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Runtime.InteropServices;
using CommandLine;
using pannella.analoguepocket;
using ConsoleTools;


internal class Program
{
Expand Down Expand Up @@ -145,7 +147,6 @@ private static async Task Main(string[] args)
{
if(o.InstallPath != null && o.InstallPath != "") {
path = o.InstallPath;
cliMode = true;
}
}
)
Expand Down Expand Up @@ -246,7 +247,7 @@ private static async Task Main(string[] args)
} else {
bool flag = true;
while(flag) {
int choice = DisplayMenu();
int choice = DisplayMenuNew();

switch(choice) {
case 1:
Expand Down Expand Up @@ -653,6 +654,32 @@ private static void ShowSponsorLinks()
}
}

private static string? GetSponsorLinks()
{
if (GlobalHelper.Instance.InstalledCores.Count == 0) return null;
var random = new Random();
var index = random.Next(GlobalHelper.Instance.InstalledCores.Count);
var randomItem = GlobalHelper.Instance.InstalledCores[index];
string output = "";
if(randomItem.sponsor != null) {
var links = "";
if (randomItem.sponsor.custom != null) {
links += "\r\n" + String.Join("\r\n", randomItem.sponsor.custom);
}
if (randomItem.sponsor.github != null) {
links += "\r\n" + String.Join("\r\n", randomItem.sponsor.github);
}
if (randomItem.sponsor.patreon != null) {
links += "\r\n" + randomItem.sponsor.patreon;
}
output += "\r\n";
output += $"Please consider supporting {randomItem.getConfig().core.metadata.author} for their work on the {randomItem} core:";
output += $"\r\n{links.Trim()}";
}

return output;
}

private static async Task Funding(string? identifier)
{
await updater.Initialize();
Expand Down Expand Up @@ -847,6 +874,37 @@ private static string GetPlatform()
return "";
}

private static int DisplayMenuNew()
{
Console.Clear();
Random random = new Random();
int i = random.Next(0, welcomeMessages.Length);
string welcome = welcomeMessages[i];

int choice = 0;

var menu = new ConsoleMenu()
.Configure(config =>
{
config.Selector = "=>";
//config.EnableFilter = true;
config.Title = $"{welcome}\r\n{GetSponsorLinks()}\r\n";
config.EnableWriteTitle = true;
//config.EnableBreadcrumb = true;
config.WriteHeaderAction = () => Console.WriteLine("Choose your destiny:");
config.SelectedItemBackgroundColor = Console.ForegroundColor;
config.SelectedItemForegroundColor = Console.BackgroundColor;
});

foreach(var (item, index) in menuItems.WithIndex()) {
menu.Add(item, (thisMenu) => { choice = thisMenu.CurrentItem.Index; thisMenu.CloseMenu(); });
}

menu.Show();

return choice;
}

private static int DisplayMenu()
{
Console.Clear();
Expand Down Expand Up @@ -875,14 +933,29 @@ private static async Task ImagePackSelector(string path)
Console.WriteLine("Checking for image packs...\n");
ImagePack[] packs = await ImagePacksService.GetImagePacks();
if(packs.Length > 0) {


int choice = 0;

var menu = new ConsoleMenu()
.Configure(config =>
{
config.Selector = "=>";
config.EnableWriteTitle = false;
//config.EnableBreadcrumb = true;
config.WriteHeaderAction = () => Console.WriteLine("So, what'll it be?:");
config.SelectedItemBackgroundColor = Console.ForegroundColor;
config.SelectedItemForegroundColor = Console.BackgroundColor;
});

foreach(var (pack, index) in packs.WithIndex()) {
Console.WriteLine($"{index}) {pack.owner}: {pack.repository} {pack.variant}");
menu.Add($"{pack.owner}: {pack.repository} {pack.variant}", (thisMenu) => { choice = thisMenu.CurrentItem.Index; thisMenu.CloseMenu(); });
}
Console.WriteLine($"{packs.Length}) Go back");
Console.Write("\nSo, what'll it be?: ");
int choice;
bool result = int.TryParse(Console.ReadLine(), out choice);
if(result && choice < packs.Length && choice >= 0) {
menu.Add("Go Back", (thisMenu) => {choice = packs.Length; thisMenu.CloseMenu();});

menu.Show();

if(choice < packs.Length && choice >= 0) {
await InstallImagePack(path, packs[choice]);
Pause();
} else if(choice == packs.Length) {
Expand Down

0 comments on commit 744ccf4

Please sign in to comment.