Compare commits
2 commits
eb328210a5
...
3902f5e3b7
Author | SHA1 | Date | |
---|---|---|---|
3902f5e3b7 | |||
c8a0606ae5 |
3 changed files with 46 additions and 4 deletions
11
README.md
11
README.md
|
@ -11,6 +11,17 @@ A no-nonsense, user-extensible `fortune-mod` replacement.
|
|||
This project was originally built in Rust, and I migrated it to C# primarily to
|
||||
test out [bflat](https://github.com/bflattened/bflat).
|
||||
|
||||
<details>
|
||||
<summary><h2>Methodology</h2></summary>
|
||||
|
||||
`fortune-cs` starts by selecting a category file, then a line, so the user will
|
||||
see a normal representation of each file. Then, it selects a line from the file.
|
||||
|
||||
The fortune files themselves can be extended just by appending;
|
||||
`cat b.txt >> a.txt` is a perfectly valid way of merging two files.
|
||||
|
||||
</details>
|
||||
|
||||
## Installation
|
||||
|
||||
### Application
|
||||
|
|
|
@ -2,6 +2,12 @@ using System;
|
|||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
// use merge function if given multiple arguments
|
||||
if(args.Length > 0) {
|
||||
Utilities.Merge(args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// make sure fortune directory exists
|
||||
var resourcePath = "/usr/share/fortune-cs/";
|
||||
if(!Directory.Exists(resourcePath)) {
|
||||
|
@ -12,15 +18,11 @@ if(!Directory.Exists(resourcePath)) {
|
|||
// pull file list
|
||||
var files = Directory.GetFiles(resourcePath, "*.txt");
|
||||
|
||||
var prng = RandomNumberGenerator.Create();
|
||||
|
||||
// choose a file and line
|
||||
var file = files[RandomNumberGenerator.GetInt32(files.Length)];
|
||||
var lines = File.ReadAllLines(file);
|
||||
var line = lines[RandomNumberGenerator.GetInt32(lines.Length)];
|
||||
|
||||
prng.Dispose();
|
||||
|
||||
// process escape codes
|
||||
line = line.Replace("\\n", "\n");
|
||||
|
||||
|
|
29
src/Util.cs
Normal file
29
src/Util.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
public static class Utilities {
|
||||
|
||||
public static void Merge(params string[] files) {
|
||||
// hashset to prevent duplicates
|
||||
var members = new HashSet<string>();
|
||||
// iterate over all paths given
|
||||
foreach(var file in files) {
|
||||
// skip nonexistent files gracefully
|
||||
if(!File.Exists(file))
|
||||
continue;
|
||||
// iterate over lines
|
||||
var lines = File.ReadAllLines(file);
|
||||
foreach(var line in lines) {
|
||||
// prevent duplicates
|
||||
if(members.Contains(line))
|
||||
continue;
|
||||
members.Add(line);
|
||||
// emit to stdout
|
||||
Console.WriteLine(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in a new issue