-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.d
112 lines (99 loc) · 3.3 KB
/
app.d
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import std.stdio;
import std.getopt;
import std.array;
import std.typecons;
import mime.database;
import mime.paths;
void detectTypes(MimeDatabase database, string[] filePaths)
{
foreach(filePath; filePaths) {
writefln("MIME type of '%s' according to", filePath);
auto mimeType = database.mimeTypeForFile(filePath, MimeDatabase.Match.globPatterns);
writefln("\tglob patterns:\t%s", mimeType ? mimeType.name : "unknown");
mimeType = database.mimeTypeForFile(filePath, MimeDatabase.Match.magicRules);
writefln("\tmagic rules:\t%s", mimeType ? mimeType.name : "unknown");
mimeType = database.mimeTypeForFile(filePath, MimeDatabase.Match.textFallback | MimeDatabase.Match.octetStreamFallback | MimeDatabase.Match.emptyFileFallback);
writefln("\ttext or binary:\t%s", mimeType ? mimeType.name : "unknown");
mimeType = database.mimeTypeForFile(filePath, MimeDatabase.Match.inodeType);
writefln("\tinode type:\t%s", mimeType ? mimeType.name : "unknown");
}
}
void printInfo(MimeDatabase database, string[] mimeTypes)
{
foreach(type; mimeTypes) {
auto mimeType = database.mimeType(type);
if (mimeType is null) {
writefln("Could not find mime type named %s\n", type);
} else {
writefln("Information for %s:", type);
writefln("MIME type: %s", mimeType.name);
writefln("Icon: %s", mimeType.getIcon());
writefln("Generic-Icon: %s", mimeType.getGenericIcon());
writefln("Aliases: %s", mimeType.aliases);
writefln("Parents: %s", mimeType.parents);
writefln("Patterns: %s", mimeType.patterns);
writeln();
}
}
}
void resolveAliases(MimeDatabase database, string[] aliases)
{
foreach(nameOrAlias; aliases) {
auto mimeType = database.mimeType(nameOrAlias, Yes.resolveAlias);
if (!mimeType) {
writefln("Could not resolve alias %s", nameOrAlias);
} else {
if (mimeType.name == nameOrAlias) {
writefln("%20s is real name, not alias", nameOrAlias);
} else {
writefln("%-20s is alias for %s", nameOrAlias, mimeType.name);
}
}
}
}
void main(string[] args)
{
string[] mimePaths;
getopt(args,
"mimepath", "Set mime path to search files in.", &mimePaths
);
version(OSX) {} else version(Posix) {
if (!mimePaths.length) {
mimePaths = mime.paths.mimePaths().array;
}
}
if (!mimePaths.length) {
stderr.writeln("No mime paths set");
return;
}
auto database = new MimeDatabase(mimePaths);
string[] posArgs;
string command;
if (args.length < 2) {
stderr.writeln("expected command");
return;
} else {
posArgs = args[2..$];
command = args[1];
}
switch(command) {
case "detect":
{
detectTypes(database, posArgs);
}
break;
case "info":
{
printInfo(database, posArgs);
}
break;
case "resolve":
{
resolveAliases(database, posArgs);
}
break;
default: {
stderr.writefln("Unknown command: %s", command);
}
}
}