ParadoxSaveParser/ParadoxSaveParser.CLI/Modes.cs
2025-04-06 01:43:27 +05:00

156 lines
6.2 KiB
C#

using System.Text.Json;
using ParadoxSaveParser.Lib;
namespace ParadoxSaveParser.CLI;
internal enum Mode
{
Unset, Search, Interactive
}
internal static class Modes
{
internal static void Search(string searchQuery, IOPath inputPath, IOPath? outputPath)
{
using var inputStream = File.OpenRead(inputPath);
using var outputStream = outputPath is null
? Console.OpenStandardOutput()
: File.OpenWrite(outputPath.Value);
var searchExpression = SearchExpressionCompiler.Compile(searchQuery);
var parser = new SaveParserEU4(inputStream, searchExpression);
var parsedValue = parser.Parse();
JsonSerializer.Serialize(outputStream, parsedValue, ParsedValueJsonContext.Default.DictionaryStringListObject);
}
internal static void Interactive()
{
Console.Clear();
Console.ResetColor();
ColoredConsole.WriteTitle("interactive mode", fg: ConsoleColor.Cyan);
ColoredConsole.WriteLine($"working directory: '{Environment.CurrentDirectory}'", ConsoleColor.Gray);
IOPath? inputPath = null;
IOPath? outputPath = null;
while (true)
{
try
{
ColoredConsole.Write("> ", ConsoleColor.Blue);
Console.ForegroundColor = ConsoleColor.Gray;
string? input = Console.ReadLine();
if (string.IsNullOrEmpty(input))
continue;
const string helpMessage =
"""
Commands dont have arguments. Just write command name ant it will ask you for more information.
Avaliable commands:
h, help - show this message
q, quit, exit - close the program
pwd - show working directory
cd - change working directory
ls - show list of files in working directory
i, input - set input file path
o, output - set output file path
s, search - perform search in input file using expression
""";
if (input.Contains(' '))
{
ColoredConsole.WriteLine("Commands dont have arguments."
+ " Just write command name ant it will ask you for more information.",
ConsoleColor.Red);
continue;
}
switch (input)
{
default:
ColoredConsole.WriteLine("Unknown command, use 'help' for a list of commands.",
ConsoleColor.Red);
continue;
case "q":
case "quit":
case "exit":
return;
case "h":
case "help":
ColoredConsole.WriteLine(helpMessage, fg: ConsoleColor.White);
break;
case "i":
case "input":
input = ColoredConsole.ReadLine("Input file path",
ConsoleColor.Blue);
if (string.IsNullOrEmpty(input))
throw new NullReferenceException();
inputPath = input;
break;
case "o":
case "output":
input = ColoredConsole.ReadLine("Output file path [default=stdout]",
ConsoleColor.Blue);
if (string.IsNullOrEmpty(input))
throw new ArgumentException("Input file path is required");
inputPath = input;
break;
case "s":
case "search":
if (inputPath is null)
throw new ArgumentException("Input file path is required");
var searchQuery = ColoredConsole.ReadLine("search expression",
ConsoleColor.Blue);
if (string.IsNullOrEmpty(searchQuery))
throw new ArgumentException("Search expression is required");
ColoredConsole.WriteHLine('-', ConsoleColor.Cyan);
Console.ResetColor();
Search(searchQuery, inputPath.Value, outputPath);
ColoredConsole.WriteHLine('-', ConsoleColor.Green);
break;
case "pwd":
ColoredConsole.WriteLine(Environment.CurrentDirectory, ConsoleColor.White);
break;
case "cd":
input = ColoredConsole.ReadLine("Change working directory to",
ConsoleColor.Blue);
if (!string.IsNullOrEmpty(input))
{
Environment.CurrentDirectory = new IOPath(input).Str;
ColoredConsole.WriteLine(Environment.CurrentDirectory, ConsoleColor.Green);
}
break;
case "ls":
IOPath curdir = Directory.GetCurrent();
foreach (var dir in Directory.GetDirectories(curdir))
{
ColoredConsole.WriteLine(dir.RemoveBase(curdir).Str + Path.Sep, ConsoleColor.Cyan);
}
foreach (var file in Directory.GetFiles(curdir))
{
ColoredConsole.WriteLine(file.RemoveBase(curdir).Str, ConsoleColor.White);
}
break;
}
}
catch (Exception ex)
{
ColoredConsole.WriteLine(ex.ToStringDemystified(), ConsoleColor.Red);
}
}
}
}