The purpose of this repository is to provide a way for people to generate random "placeholder text" -- with a Markov Chain. https://www.craigoates.net/Software/project/12
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
7.2 KiB

using Console.Waterworks;
using Console.Waterworks.Attributes;
using System;
using static WetPancake.Pancake;
using static System.Environment;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.FSharp.Control;
using Microsoft.FSharp.Core;
using System.Threading;
using System.IO;
using static System.Console;
namespace WetPancakeCLI
{
public static class ConsoleCommands
{
// Pass these in when running an FSharpAsync task.
// Examples are within the command-methods.
static FSharpOption<TaskCreationOptions> _taskCreationOptions = FSharpOption<TaskCreationOptions>.None;
static FSharpOption<CancellationToken> _cancellationToken = FSharpOption<CancellationToken>.None;
[ListCommand]
[Description("Prints a test message to the console.")]
[Parameters("None")]
[Usage("> Test")]
public static string Test() => "SUCCESS: Console.Waterworks is wired into Wet Pancake CLI.";
[ListCommand]
[Description("Lists out the commands this program offers.")]
[Parameters("None")]
[Usage("> Help")]
public static string Help() => new CW_Liaison().RequestHelpDocumentation("WetPancakeCLI");
[ListCommand]
[Description("Exits out of the program.")]
[Parameters("None")]
[Usage("> Exit")]
public static void Exit() => Environment.Exit(ExitCode);
[ListCommand]
[Description("Generates random text, the number of sentences generated is randomly determined.")]
[Parameters("None")]
[Usage("> GenerateRandomText")]
public static string GenerateRandomText()
{
try
{
return FSharpAsync.StartAsTask(RequestRandomTextAsync(), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
[ListCommand]
[Description("Generates text using the gibberish level and number of sentences specified by the user. Gibberish level must be between 2 and 20.")]
[Parameters("gibberish level: int, sentences: int")]
[Usage("> GenerateText 5 10")]
public static string GenerateText(int gibberishLevel, int sentences)
{
try
{
if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException
("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1)
throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences");
return FSharpAsync.StartAsTask
(RequestTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
[ListCommand]
[Description("Loads the specified .txt file and generates text based on it using the gibberish level and number of sentences specified by the user. Gibberish level must be between 2 and 20.")]
[Parameters("gibberish level: int, sentences: int, file path: string")]
[Usage("> GenerateTextFromFile 3 6 C:/yourfile.txt")]
public static string GenerateTextFromFile(int gibberishLevel, int sentences, string filePath)
{
try
{
if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException
("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1)
throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences");
if (Path.GetExtension(filePath) != ".txt")
throw new FileLoadException
("The file entered is not a plain text (.txt) file.", filePath);
return FSharpAsync.StartAsTask
(RequestTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
[ListCommand]
[Description("Returns a list of all the available .txt files built-in to Wet Pancake.")]
[Parameters("None")]
[Usage("> RequestAllTemplateFiles")]
public static string RequestAllTemplateFiles()
{
try
{
WriteLine("Attempting to list out the available template files...");
var files = FSharpAsync.StartAsTask(RequestAllTemplateFilesAsync(), _taskCreationOptions, _cancellationToken).Result;
foreach (var item in files)
{
WriteLine($"File path: {item}");
}
return "Listing complete.";
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
// TODO: Add "Help" attribute to GenerateCleanText command-method.
public static string GenerateCleanText(int gibberishLevel, int sentences)
{
try
{
if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException
("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1)
throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences");
return FSharpAsync.StartAsTask
(RequestCleanTextAsync(gibberishLevel, sentences), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
//TODO: Add "HElp" attribute to GenerateCleanTextFromFile command-method.
public static string GenerateCleanTextFromFile(int gibberishLevel, int sentences, string filePath)
{
try
{
if (gibberishLevel < 2 || gibberishLevel > 20)
throw new ArgumentException
("Invalid argument. Must be between 2 and 20 (inclusive).", "gibberishLevel");
if (sentences < 1)
throw new ArgumentException
("Invalid argument. Must be greater than 0.", "sentences");
if (Path.GetExtension(filePath) != ".txt")
throw new FileLoadException
("The file entered is not a plain text (.txt) file.", filePath);
return FSharpAsync.StartAsTask
(RequestCleanTextFromFileAsync(gibberishLevel, sentences, filePath), _taskCreationOptions, _cancellationToken).Result;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
}
}