-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.d
97 lines (91 loc) · 3.07 KB
/
manage.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
/+dub.sdl:
name "manage"
dependency "trashcan" path="../"
+/
import std.path;
import std.stdio;
import std.array;
import std.exception;
import std.algorithm.sorting : sort;
import std.getopt;
import trashcan;
void printHelp()
{
writeln("Possible commands: restore <index>; erase <index>; exit; help; name;");
}
void main(string[] args)
{
bool onlyList;
getopt(args, "list", "List items in trash can and exit", &onlyList);
auto trashCan = new Trashcan();
TrashcanItem[] items;
void printItems()
{
items = trashCan.byItem.array.sort!((a,b) => a.restorePath.baseName < b.restorePath.baseName).array;
foreach(i, item; items) {
writefln("%s: %s (%s, deletion time: %s)", i, item.restorePath, item.isDir ? "directory" : "file", item.deletionTime.toISOExtString);
}
}
printItems();
if (onlyList)
return;
string line;
printHelp();
write("$ ");
while((line = readln()) !is null) {
import std.string : stripRight;
import std.conv : to;
import std.algorithm.iteration : splitter;
line = line.stripRight;
auto splitted = line.splitter(' ');
if (!splitted.empty) {
try {
string command = splitted.front;
splitted.popFront();
switch(command) {
case "erase":
case "delete":
{
foreach(arg; splitted) {
const index = arg.to!size_t;
enforce(index < items.length, "Wrong index " ~ arg);
trashCan.erase(items[index]);
writefln("Item %s %s deleted from trashcan", index, items[index].restorePath.baseName);
}
}
break;
case "restore":
case "undelete":
{
foreach(arg; splitted) {
const index = arg.to!size_t;
enforce(index < items.length, "Wrong index " ~ arg);
trashCan.restore(items[index]);
writefln("Item %s %s restored to its original location", index, items[index].restorePath.baseName);
}
}
break;
case "exit":
case "quit":
return;
case "name":
writeln(trashCan.displayName);
break;
case "help":
case "?":
printHelp();
break;
case "list":
printItems();
break;
default:
stderr.writefln("Unknown command %s", command);
break;
}
} catch(Exception e) {
stderr.writeln(e.msg);
}
}
write("$ ");
}
}