ParadoxSaveParser/ParadoxSaveParser.CLI/Modes/Interactive.cs

143 lines
5.8 KiB
C#

using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.Json;
using ParadoxSaveParser.Lib;
using Path = DTLib.Filesystem.Path;
namespace ParadoxSaveParser.CLI;
internal enum Mode
{
Unset, Search, Interactive
}
internal static partial class Modes
{
internal static void Interactive()
{
ColoredConsole.Clear();
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);
string input = ColoredConsole.ReadLine(ConsoleColor.Gray);
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, ConsoleColor.White);
break;
case "i":
case "input":
ColoredConsole.Write("Input file path: ", ConsoleColor.Blue);
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
if (string.IsNullOrEmpty(input))
throw new NullReferenceException();
inputPath = input;
break;
case "o":
case "output":
ColoredConsole.Write("Output file path [default=stdout]: ", ConsoleColor.Blue);
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
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");
ColoredConsole.Write("search expression: ", ConsoleColor.Blue);
var searchQuery = ColoredConsole.ReadLine(ConsoleColor.Gray);
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":
ColoredConsole.Write("Change working directory to: ", ConsoleColor.Blue);
input = ColoredConsole.ReadLine(ConsoleColor.Gray);
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);
}
}
}
}