Skip to content

Commit 64ac551

Browse files
committed
Update 'open' example to handle arbitrary url types
1 parent d6ef525 commit 64ac551

File tree

1 file changed

+7
-5
lines changed
  • examples/open/source

1 file changed

+7
-5
lines changed

examples/open/source/app.d

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import std.string : stripRight, startsWith;
55
import std.conv : to;
66
import std.typecons : rebindable, Rebindable;
77

8+
import std.regex;
9+
810
import desktopfile.paths;
911
import mimeapps;
1012
import mime.database;
@@ -25,20 +27,20 @@ void main(string[] args)
2527
auto mimeAppsLists = mimeAppsListFiles();
2628
auto mimeInfoCaches = mimeInfoCacheFiles();
2729

30+
auto urlRegex = regex(`([a-z]+)://.*`);
31+
2832
foreach(filePath; files) {
2933
Rebindable!(const(MimeType)) mimeType;
3034
string mimeTypeName;
31-
if (filePath.startsWith("http://")) {
32-
mimeTypeName = "x-scheme-handler/http";
33-
} else if (filePath.startsWith("https://")) {
34-
mimeTypeName = "x-scheme-handler/https";
35+
auto matchResult = matchFirst(filePath, urlRegex);
36+
if (!matchResult.empty) {
37+
mimeTypeName = "x-scheme-handler/" ~ matchResult[1];
3538
} else {
3639
mimeType = mimeDatabase.mimeTypeForFile(filePath, match);
3740
if (mimeType) {
3841
mimeTypeName = mimeType.name;
3942
}
4043
}
41-
4244
if (mimeTypeName.empty) {
4345
stderr.writefln("Could not detect MIME type for %s", filePath);
4446
continue;

0 commit comments

Comments
 (0)