1 /**
2 Copyright: Copyright (c) 2021, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module dextool.plugin.mutate.backend.report.html.utility;
11 
12 import std.datetime : SysTime, Duration, dur;
13 
14 import dextool.plugin.mutate.backend.report.html.constants;
15 import dextool.type : Path;
16 
17 @safe:
18 
19 /// Convert a file path to a html represented path.
20 string pathToHtml(string p) {
21     import std.algorithm : joiner;
22     import std.path : pathSplitter, buildPath;
23     import std.utf : toUTF8;
24 
25     return p.pathSplitter.joiner("__").toUTF8;
26 }
27 
28 Path pathToHtmlLink(string p) {
29     return Path(pathToHtml(p) ~ Html.ext);
30 }
31 
32 string toShortDate(SysTime ts) {
33     import std.format : format;
34 
35     return format("%04s-%02s-%02s", ts.year, cast(ushort) ts.month, ts.day);
36 }
37 
38 string toShortTime(Duration d) {
39     import std.conv : to;
40     import std.format : format;
41 
42     immutable Units = ["days", "hours", "minutes", "seconds"];
43 
44     static foreach (UnitIdx; 0 .. Units.length - 1) {
45         {
46             if (d.total!(Units[UnitIdx]) > 0) {
47                 enum unit0 = Units[UnitIdx];
48                 enum unit1 = Units[UnitIdx + 1];
49                 return format("%s%s %s%s", d.total!unit0, unit0[0], (d - d.total!unit0
50                         .dur!unit0).total!unit1, unit1[0]);
51             }
52         }
53     }
54 
55     return format!"%ss"(d.total!"seconds");
56 }