CLI interface to medialist (fossil mirror)
Revision | 5053fb1067fb33547ce6830bb455489485fe1eb1 (tree) |
---|---|
Time | 2021-09-20 18:27:06 |
Author | mio <stigma@disr...> |
Commiter | mio |
Reformat util.d and add expandEnvironmentVariables function.
FossilOrigin-Name: 86bdf587332c456bf313f00ee843aa300ac2ebecb7bbc53e8348d77fe7c6ad93
@@ -37,23 +37,78 @@ enum NUMBER_OF_ML_HEADERS = 6; | ||
37 | 37 | */ |
38 | 38 | enum MLHeaders |
39 | 39 | { |
40 | - title = "title", | |
41 | - status = "status", | |
42 | - progress = "progress", | |
43 | - startDate = "start_date", | |
44 | - endDate = "end_date", | |
45 | - lastUpdated = "last_updated" | |
40 | + title = "title", | |
41 | + status = "status", | |
42 | + progress = "progress", | |
43 | + startDate = "start_date", | |
44 | + endDate = "end_date", | |
45 | + lastUpdated = "last_updated" | |
46 | 46 | } |
47 | 47 | |
48 | 48 | @trusted @nogc public void |
49 | 49 | _log(string msg, bool err = false) |
50 | 50 | { |
51 | - import core.stdc.stdio : fprintf, stderr; | |
51 | + import core.stdc.stdio : fprintf, stderr; | |
52 | 52 | |
53 | - if (true == err) | |
54 | - fprintf(stderr, "*** error: %s ***\n", msg.ptr); | |
55 | - else | |
56 | - fprintf(stderr, "** warning: %s **\n", msg.ptr); | |
53 | + if (true == err) | |
54 | + fprintf(stderr, "*** error: %s ***\n", msg.ptr); | |
55 | + else | |
56 | + fprintf(stderr, "** warning: %s **\n", msg.ptr); | |
57 | +} | |
58 | + | |
59 | +/** | |
60 | + * Expand a string which can contain POSIX-style environment variables. | |
61 | + * | |
62 | + * This function does $(B NOT) expand tildes. | |
63 | + */ | |
64 | +@safe public string | |
65 | +expandEnvironmentVariables(string str) | |
66 | +{ | |
67 | + import std.array : appender; | |
68 | + import std.process : environment; | |
69 | + import std.string : indexOf, indexOfAny; | |
70 | + | |
71 | + ptrdiff_t startPos = 0; | |
72 | + ptrdiff_t endPos = 0; | |
73 | + | |
74 | + auto result = appender!string; | |
75 | + | |
76 | + startPos = indexOf(str, '$', endPos); | |
77 | + if (-1 == startPos) | |
78 | + { | |
79 | + return str; | |
80 | + } | |
81 | + | |
82 | + do | |
83 | + { | |
84 | + result ~= str[endPos..startPos]; | |
85 | + | |
86 | + endPos = indexOfAny(str, "/ ", startPos); | |
87 | + | |
88 | + /* Env. var ends the string */ | |
89 | + /* startPos+1 to remove the $ */ | |
90 | + if (endPos == -1) | |
91 | + { | |
92 | + result ~= environment.get(str[startPos+1..$], ""); | |
93 | + break; | |
94 | + } | |
95 | + else | |
96 | + { | |
97 | + result ~= environment.get(str[startPos+1..endPos], ""); | |
98 | + } | |
99 | + | |
100 | + startPos = indexOf(str, '$', endPos); | |
101 | + | |
102 | + if (startPos == -1) | |
103 | + { | |
104 | + result ~= str[endPos..$]; | |
105 | + break; | |
106 | + } | |
107 | + | |
108 | + | |
109 | + } while (true); | |
110 | + | |
111 | + return result.data; | |
57 | 112 | } |
58 | 113 | |
59 | 114 | /** |
@@ -68,30 +123,30 @@ _log(string msg, bool err = false) | ||
68 | 123 | @trusted public Tuple!(size_t, string)[NUMBER_OF_ML_HEADERS] |
69 | 124 | parseMLHeader(string[] headerSections) |
70 | 125 | { |
71 | - import std.string : toLower; | |
126 | + import std.string : toLower; | |
72 | 127 | |
73 | - Tuple!(size_t, string)[NUMBER_OF_ML_HEADERS] sections; | |
74 | - size_t validHeaderCounter = 0; | |
128 | + Tuple!(size_t, string)[NUMBER_OF_ML_HEADERS] sections; | |
129 | + size_t validHeaderCounter = 0; | |
75 | 130 | |
76 | - foreach (tabIndent, headerSection; headerSections) | |
77 | - { | |
78 | - string lowercaseHeader = headerSection.toLower; | |
79 | - switch (lowercaseHeader) | |
80 | - { | |
81 | - case MLHeaders.title: | |
82 | - case MLHeaders.status: | |
83 | - case MLHeaders.progress: | |
84 | - case MLHeaders.startDate: | |
85 | - case MLHeaders.endDate: | |
86 | - case MLHeaders.lastUpdated: | |
87 | - sections[validHeaderCounter++] = tuple(tabIndent, lowercaseHeader); | |
88 | - break; | |
89 | - default: | |
90 | - break; | |
91 | - } | |
92 | - if (validHeaderCounter >= NUMBER_OF_ML_HEADERS) | |
93 | - break; | |
94 | - } | |
131 | + foreach (tabIndent, headerSection; headerSections) | |
132 | + { | |
133 | + string lowercaseHeader = headerSection.toLower; | |
134 | + switch (lowercaseHeader) | |
135 | + { | |
136 | + case MLHeaders.title: | |
137 | + case MLHeaders.status: | |
138 | + case MLHeaders.progress: | |
139 | + case MLHeaders.startDate: | |
140 | + case MLHeaders.endDate: | |
141 | + case MLHeaders.lastUpdated: | |
142 | + sections[validHeaderCounter++] = tuple(tabIndent, lowercaseHeader); | |
143 | + break; | |
144 | + default: | |
145 | + break; | |
146 | + } | |
147 | + if (validHeaderCounter >= NUMBER_OF_ML_HEADERS) | |
148 | + break; | |
149 | + } | |
95 | 150 | |
96 | - return sections; | |
151 | + return sections; | |
97 | 152 | } |