summaryrefslogtreecommitdiff
path: root/source/app.d
blob: 25642c2e148f983f6bd7b6eaea05c684073afd09 (plain)
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
import std.stdio;
import std.getopt;

import prim.opt;

import prompt.ps1;
import prompt.rps1;
import prompt.preexec;

import style;
import style.color;

Opts defaultOpts() {
  Opts opts;

  opts.pathlen = 3;
  opts.pchar = "*";

  return opts;
}

void printHelp(GetoptResult args) {
  defaultGetoptPrinter("prim prompt:\n", args.options);
  writeln("\nEnvironment:\n",
    "\tNO_COLOR\tsee https://no-color.org");
}

void main(string[] argv) {
  Opts opts = defaultOpts();

  try {

    GetoptResult args = getopt(
      argv,
      std.getopt.config.bundling,
      "ps1|p", "print PS1", &opts.ps1,
      "rps1|r", "print RPS1", &opts.rps1,
      "preexec|x", "print preexec", &opts.preexec,

      std.getopt.config.required,
      "col", "terminal width", &opts.col,

      std.getopt.config.required,
      "row", "terminal height", &opts.row,

      std.getopt.config.required,
      "status", "previous command exit code", &opts.status,

      "pathlen", "set length of displayed path", &opts.pathlen,
      "pchar", "override default prompt character", &opts.pchar,
    );

    if (args.helpWanted) {
      printHelp(args);
    }
  } catch (Exception e) {
    writeln(e.msg);
    writeln("try '--help' for more information");
  }

  dorun(opts);
}

void dorun(Opts opts) {
  if (opts.ps1) {
    ps1(opts).write();
  }

  if (opts.preexec) {
    preexec(opts).write();
  }

  if (opts.rps1) {
    rps1(opts).write();
  }
}