Compare commits

..

2 commits

Author SHA1 Message Date
3902f5e3b7 added file merge functionality 2024-03-06 19:51:24 -05:00
c8a0606ae5 updated README 2024-03-06 19:15:50 -05:00
3 changed files with 46 additions and 4 deletions

View file

@ -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

View file

@ -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
View 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);
}
}
}
}