Compare commits

..

No commits in common. "1b38e781ad67f5805f2be26c13ec4683360327af" and "d491b047d5f1a7557139d25b033ce9c0f7b6074a" have entirely different histories.

2 changed files with 9 additions and 34 deletions

View file

@ -14,38 +14,14 @@ if(Utilities.HasFlags(args, "-v", "--version")) {
return 0;
}
// collect nonflag args for merge or file selection
var arguments = Utilities.CollectArgs(args);
// handle merge flag
if(Utilities.HasFlags(args, "-m", "--merge")) {
Utilities.Merge(arguments);
Utilities.Merge(args);
return 0;
}
// get resource path from var or default
string resourcePath = Environment.GetEnvironmentVariable("FORTUNE_CS_DIR");
if(resourcePath == "" || !Directory.Exists(resourcePath))
resourcePath = Globals.DEFAULT_PATH;
// pull file arg if provided
string file = null;
if(arguments.Count == 1) {
file = arguments[0];
// if the file doens't exist, see if it's in `resourcePath`
if(!File.Exists(file)) {
if(!file.EndsWith(".txt"))
file = file + ".txt";
file = resourcePath + file;
if(!File.Exists(file)) {
// don't try to read a file that doesn't exist
Console.WriteLine($"fortune-cs: no file '{file}' found.");
return 2;
}
}
}
// make sure fortune directory exists
var resourcePath = Globals.DEFAULT_PATH;
if(!Directory.Exists(resourcePath)) {
Console.WriteLine($"fortune-cs: directory '${resourcePath}' does not exist");
return 1;
@ -60,17 +36,15 @@ if(Utilities.HasFlags(args, "-l", "--list")) {
return 0;
}
// choose file if not provided
if(file == null)
file = files[RandomNumberGenerator.GetInt32(files.Length)];
// read the file and choose a line
// choose a file and line
var file = files[RandomNumberGenerator.GetInt32(files.Length)];
var lines = File.ReadAllLines(file);
var line = lines[RandomNumberGenerator.GetInt32(lines.Length)];
// process line breaks
// process escape codes
line = line.Replace("\\n", "\n");
// write the fortune
// write
Console.WriteLine(line);
return 0;

View file

@ -27,9 +27,10 @@ public static class Utilities {
}
/// <summary>
/// outputs all unique lines from all files in `files` to stdout
/// outputs all unique lines from all files in `args` to stdout
/// </summary>
public static void Merge(List<string> files) {
public static void Merge(string[] args) {
var files = CollectArgs(args);
// hashset to prevent duplicates
var members = new HashSet<string>();
// iterate over all paths given