aboutsummaryrefslogtreecommitdiff
path: root/source/app.d
blob: d5b89ad903a9549da95a52cd17f1641a609a3a4b (plain) (blame)
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
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;
}