aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-06-25 10:37:08 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-06-25 10:37:08 -0400
commita956211b2cd0ba7d970f08afe3b7470728691206 (patch)
tree601ebafedf17edb83169bdfcf1372683fe7fcfc8 /source
downloadjdtalk-a956211b2cd0ba7d970f08afe3b7470728691206.tar.gz
Initial commit
Diffstat (limited to 'source')
-rw-r--r--source/app.d79
1 files changed, 79 insertions, 0 deletions
diff --git a/source/app.d b/source/app.d
new file mode 100644
index 0000000..d5b89ad
--- /dev/null
+++ b/source/app.d
@@ -0,0 +1,79 @@
+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)
+{
+ import std.getopt;
+ long i = 0,
+ limit = 0;
+ bool exactMatch = false;
+ string pattern = null;
+ string dataRoot = absolutePath(getcwd());
+
+ if (environment.get("JDTALK_DATA", null)) {
+ dataRoot = environment["JDTALK_DATA"].absolutePath;
+ }
+
+ auto opt = getopt(
+ args,
+ "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,
+ "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;
+ }
+
+ while(true) {
+ string output = talk(dict);
+
+ if (pattern !is null) {
+ if (exactMatch && !hasWord(pattern, output)) {
+ continue;
+ }
+ else if (!exactMatch && !output.canFind(pattern)) {
+ continue;
+ }
+ }
+
+ writeln(output);
+
+ if (limit > 0) {
+ i++;
+ if (i == limit)
+ break;
+ }
+ }
+
+ return 0;
+}