script-launcher/src/PromptConstructor.cs

68 lines
2.1 KiB
C#
Raw Normal View History

2023-07-20 14:29:47 +02:00
using System.Globalization;
2022-06-10 16:30:57 +02:00
using System.Text;
using Spectre.Console;
namespace ScriptLauncher;
internal static class PromptConstructor
2022-06-10 16:30:57 +02:00
{
const int ScriptListSize = 15;
private static Style SelectionHighlight =>
new(decoration: Decoration.Bold | Decoration.Underline);
2022-06-10 16:30:57 +02:00
private static string FileStyle(FileInfo info, bool brief)
{
var builder = new StringBuilder();
if (!brief)
{
2023-07-20 14:29:47 +02:00
builder.Append(
CultureInfo.InvariantCulture,
$"[blue]{info.DirectoryName}{Path.DirectorySeparatorChar}[/]"
);
2022-06-10 16:30:57 +02:00
}
builder
2023-07-20 14:29:47 +02:00
.Append(
CultureInfo.InvariantCulture,
$"[orangered1]{Path.GetFileNameWithoutExtension(info.Name)}[/]"
)
.Append(CultureInfo.InvariantCulture, $"[greenyellow]{info.Extension}[/]");
2022-06-10 16:30:57 +02:00
return builder.ToString();
}
private static string DirectoryStyle(DirectoryInfo info) => $"[blue]{info}[/]";
public static MultiSelectionPrompt<FileInfo> GetScriptPrompt(FileInfo[] files, bool brief)
{
var prompt = new MultiSelectionPrompt<FileInfo>()
.Title("Select the scripts to execute:")
.NotRequired()
.PageSize(ScriptListSize)
.InstructionsText(
"[grey](Press [blue]<space>[/] to toggle a script, [green]<enter>[/] to accept)[/]"
)
.MoreChoicesText("[grey]Move up and down to reveal more options[/]")
.UseConverter(x => FileStyle(x, brief))
.HighlightStyle(SelectionHighlight)
.AddChoices(files);
2022-06-10 16:30:57 +02:00
return prompt;
}
public static SelectionPrompt<DirectoryInfo> GetDirectoryPrompt(DirectoryInfo[] directories)
{
var prompt = new SelectionPrompt<DirectoryInfo>()
.Title("Select a directory:")
.PageSize(ScriptListSize)
.MoreChoicesText("[grey]Move up and down to reveal more options[/]")
.UseConverter(DirectoryStyle)
.HighlightStyle(SelectionHighlight)
.AddChoices(directories);
2022-06-10 16:30:57 +02:00
return prompt;
}
}