1 /++
2  + main unit of dime.
3  +/
4 module dime;
5 
6 public import dime.packageversion;
7 
8 /**
9  * measure the time for calling args
10  * Params:
11  * args = shell command
12  */
13 int dime_(string[] args)
14 {
15     import unit;
16     import colored;
17     import std.array;
18     import std.datetime.stopwatch;
19     import std.datetime;
20     import std.process;
21     import std.algorithm.iteration;
22     import std..string;
23     import std.conv;
24     import std.stdio;
25     import std.getopt;
26 
27     auto help = getopt(args);
28     if (help.helpWanted)
29     {
30         defaultGetoptPrinter("D(t)ime your programs.", help.options);
31         import packageversion;
32         import std.algorithm : sort;
33         import asciitable;
34         // dfmt off
35         auto table = packageversion
36             .getPackages.sort!("a.name < b. name")
37             .fold!((table, p) => table.add(p.name, p.semVer, p.license))(AsciiTable(0, 0, 0));
38         // dfmt on
39         writeln("Packages:\n", table.toString("   ", " "));
40         return 0;
41     }
42 
43     auto childCommand = args[1 .. $];
44     auto cmd = escapeShellCommand(childCommand);
45     auto sw = std.datetime.stopwatch.StopWatch(AutoStart.yes);
46     auto pid = spawnShell(cmd);
47     auto exitCode = pid.wait();
48     auto duration = sw.peek();
49     auto d = duration.total!("msecs");
50     auto s = childCommand.to!string;
51     // dfmt off
52     stderr.writeln("%s took %s".format(
53                      exitCode == 0 ? s.green.to!string : s.black.onRed.to!string,
54                      TIME
55                          .transform(d)
56                          .onlyRelevant.map!((part) => ("%0" ~ part.digits.to!string ~ "d %s")
57                          .format(part.value, part.name))
58                          .join(" ")));
59     // dfmt on
60     return exitCode;
61 }
62 
63 @("formatWithWidth") unittest
64 {
65     import unit_threaded;
66     import std..string;
67 
68     "%03d".format(1).shouldEqual("001");
69     "%01d".format(1).shouldEqual("1");
70 }