merged in specify-file branch
This commit is contained in:
commit
1b38e781ad
2 changed files with 34 additions and 9 deletions
|
@ -14,14 +14,38 @@ if(Utilities.HasFlags(args, "-v", "--version")) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// collect nonflag args for merge or file selection
|
||||||
|
var arguments = Utilities.CollectArgs(args);
|
||||||
|
|
||||||
// handle merge flag
|
// handle merge flag
|
||||||
if(Utilities.HasFlags(args, "-m", "--merge")) {
|
if(Utilities.HasFlags(args, "-m", "--merge")) {
|
||||||
Utilities.Merge(args);
|
Utilities.Merge(arguments);
|
||||||
return 0;
|
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
|
// make sure fortune directory exists
|
||||||
var resourcePath = Globals.DEFAULT_PATH;
|
|
||||||
if(!Directory.Exists(resourcePath)) {
|
if(!Directory.Exists(resourcePath)) {
|
||||||
Console.WriteLine($"fortune-cs: directory '${resourcePath}' does not exist");
|
Console.WriteLine($"fortune-cs: directory '${resourcePath}' does not exist");
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -36,15 +60,17 @@ if(Utilities.HasFlags(args, "-l", "--list")) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// choose a file and line
|
// choose file if not provided
|
||||||
var file = files[RandomNumberGenerator.GetInt32(files.Length)];
|
if(file == null)
|
||||||
|
file = files[RandomNumberGenerator.GetInt32(files.Length)];
|
||||||
|
// read the file and choose a line
|
||||||
var lines = File.ReadAllLines(file);
|
var lines = File.ReadAllLines(file);
|
||||||
var line = lines[RandomNumberGenerator.GetInt32(lines.Length)];
|
var line = lines[RandomNumberGenerator.GetInt32(lines.Length)];
|
||||||
|
|
||||||
// process escape codes
|
// process line breaks
|
||||||
line = line.Replace("\\n", "\n");
|
line = line.Replace("\\n", "\n");
|
||||||
|
|
||||||
// write
|
// write the fortune
|
||||||
Console.WriteLine(line);
|
Console.WriteLine(line);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -27,10 +27,9 @@ public static class Utilities {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// outputs all unique lines from all files in `args` to stdout
|
/// outputs all unique lines from all files in `files` to stdout
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Merge(string[] args) {
|
public static void Merge(List<string> files) {
|
||||||
var files = CollectArgs(args);
|
|
||||||
// hashset to prevent duplicates
|
// hashset to prevent duplicates
|
||||||
var members = new HashSet<string>();
|
var members = new HashSet<string>();
|
||||||
// iterate over all paths given
|
// iterate over all paths given
|
||||||
|
|
Loading…
Reference in a new issue