1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
module app;
import std.stdio;
import std.file;
import std.array;
import std.string;
import std.range;
import std.conv;
import std.random;
import std.datetime;
import std.algorithm;
import std.path;
import std.process : environment;
import jdtalk.core;
int main(string[] args)
{
setvbuf(stdout.getFP(), null, _IONBF, 0);
setvbuf(stderr.getFP(), null, _IONBF, 0);
import std.getopt;
long i = 0;
long limit = 0;
int salad = 0;
bool exactMatch = false;
bool rCase = false;
bool hCase = false;
bool haxor = false;
string acronym;
string custom_format = null;
string pattern = null;
string dataRoot = absolutePath(getcwd());
if (environment.get("JDTALK_DATA", null)) {
dataRoot = environment["JDTALK_DATA"].absolutePath;
}
auto opt = getopt(
args,
"format|f", "Produce words with formatted string", &custom_format,
"acronym|a", "Expand characters to acronym", &acronym,
"limit|c", format("(default: %d)", limit), &limit,
"pattern|p", "Limit output to a root word", &pattern,
"exact|e", format("Exact matches only (default: %s)", exactMatch ? "true" : "false"), &exactMatch,
"salad|s", "Produce N random words", &salad,
"rcase", format("Randomize case (default: %s)", rCase ? "true" : "false"), &rCase,
"hcase", format("Change every other case (default: %s)", hCase ? "true" : "false"), &hCase,
"leet", format("1337ify output (default: %s)", haxor ? "true" : "false"), &haxor,
"dataroot|d", "Path to dictionaries", &dataRoot,
);
if (opt.helpWanted) {
defaultGetoptPrinter("jdtalk", opt.options);
return 0;
}
if (exactMatch && pattern is null) {
stderr.writeln("Argument -e requires -p");
return 1;
}
dict_t dict = getData(dataRoot);
if (pattern !is null && !searchDict(dict, pattern)) {
stderr.writefln("Word not available in dictionary: %s", pattern);
return 1;
}
if (acronym !is null) {
char result = 0;
if ((result = acronymSafe(dict, acronym, pattern)) > 0) {
stderr.writefln("No words will have: '%c'", result);
return 1;
}
}
while(true) {
string output;
if (salad) {
output = talkSalad(dict, salad);
}
else if (acronym) {
output = talkAcronym(dict, acronym);
}
else if (custom_format) {
output = talkf(dict, custom_format);
}
else {
output = talkf(dict, "%a %n %d %v");
}
if (pattern !is null) {
if (exactMatch && !hasWord(pattern, output)) {
continue;
}
else if (!exactMatch && !output.canFind(pattern)) {
continue;
}
}
if (rCase) {
output = randomCase(output);
}
else if (hCase) {
output = hillCase(output);
}
else if (haxor) {
output = leetSpeak(output);
}
if (output == null) {
writeln("An error has occurred.");
return 1;
}
if (acronym) {
// Capitalize each word in the acronym
string[] parts;
foreach (word; output.split(" ")) {
parts ~= word.capitalize;
}
output = parts.join(" ");
}
writeln(output);
if (limit > 0) {
i++;
if (i == limit)
break;
}
}
return 0;
}
|