-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
532 lines (491 loc) · 14 KB
/
Program.cs
File metadata and controls
532 lines (491 loc) · 14 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
using System.Diagnostics;
public class Bar
{
public int Width { get; set; }
public int Ind { get; set; }
public int Pct { get; set; }
public int CatFrame { get; set; }
public long ElapsedMs { get; set; }
public string Beg { get; set; }
public string BegFilled { get; set; }
public string Filler { get; set; }
public string Space { get; set; }
public string Head { get; set; }
public string Fin { get; set; }
public string FinFilled { get; set; }
}
public class UnicodeTheme
{
public const string Beg = "\uee00";
public const string BegFilled = "\uee03";
public const string Filler = "\uee04";
public const string Space = "\uee01";
public const string Head = "\uee01";
public const string Fin = "\uee02";
public const string FinFilled = "\uee05";
}
public class AsciiTheme
{
public const string Beg = "[";
public const string BegFilled = "[";
public const string Filler = "=";
public const string Space = " ";
public const string Head = ">";
public const string Fin = "]";
public const string FinFilled = "]";
}
public class RmrfArgs
{
public bool NoAnimation { get; set; }
public bool DryRun { get; set; }
public bool Ascii { get; set; }
public bool Cat { get; set; } = true;
public string Path { get; set; }
public string[] Include { get; set; }
public string[] Exclude { get; set; }
}
public class Program
{
private static readonly string NL = Environment.NewLine;
private static readonly string[] _cats = [
"( ^ー^)ニャン",
"( ^ー^)ニャン",
"ニャン(^ー^ ) ",
"ニャン(^ー^*) ",
"み (>ω<*) ",
"み (>ω<*) ",
"ニャン(^ー^ ) ",
"ニャン(^ー^ ) ",
];
public static void Main(string[] args)
{
if(!TryParse(args, out var rmrfArgs))
{
return;
}
Console.OutputEncoding = rmrfArgs.Ascii
? System.Text.Encoding.ASCII
: System.Text.Encoding.UTF8;
Console.Write("Analyzing...");
var flatDirs = FlattenTargets(GetTargets(rmrfArgs));
var success = true;
Exception e = null;
using var sw = CreateStream();
var bar = CreateBar(rmrfArgs);
Console.CancelKeyPress += delegate {
if (rmrfArgs.NoAnimation)
return;
Recover(sw);
};
var stopwatch = new Stopwatch();
DrawInitial(sw, bar, rmrfArgs);
var deleted = 0;
var total = flatDirs.Count;
stopwatch.Start();
foreach (var directory in flatDirs)
{
success = DeleteDirectory(directory, rmrfArgs, out e);
if (!success)
break;
deleted++;
var (nextPct, nextInd) = CountProgress(bar, deleted, total);
MoveDelta(sw, bar, rmrfArgs, nextInd, nextPct, stopwatch.ElapsedMilliseconds);
}
if (success)
MoveDelta(sw, bar, rmrfArgs, bar.Width - 1, 100, stopwatch.ElapsedMilliseconds);
DrawComplete(sw, rmrfArgs, success, e);
}
private static bool DeleteDirectory(string path, RmrfArgs args, out Exception e)
{
e = null;
try
{
if (args.DryRun)
{
var time = Directory.GetDirectories(path).Length +
Directory.GetFiles(path).Length;
Thread.Sleep(time / 10);
return true;
}
foreach (var dir in Directory.GetDirectories(path))
{
Directory.Delete(dir);
}
foreach (var file in Directory.GetFiles(path))
{
FileDelete(file);
}
if (path != Directory.GetCurrentDirectory())
{
Directory.Delete(path);
}
}
catch (Exception err)
{
e = err;
return false;
}
return true;
}
private static void FileDelete(string path)
{
try
{
File.Delete(path);
}
catch (UnauthorizedAccessException)
{
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(path, attributes);
File.Delete(path);
}
else
{
throw;
}
}
}
private static (int, int) CountProgress(Bar bar, int current, int total)
{
var pct = (double)current / total;
return ((int)(pct * 100), (int)(pct * (bar.Width - 1)));
}
private static StreamWriter CreateStream()
{
var sw = new StreamWriter(Console.OpenStandardOutput());
sw.AutoFlush = true;
Console.SetOut(sw);
return sw;
}
private static Bar CreateBar(RmrfArgs args)
{
var bar = new Bar
{
Width = 30
};
SetupTheme(bar, args);
return bar;
}
private static void SetupTheme(Bar bar, RmrfArgs args)
{
if (args.Ascii)
{
bar.Beg = AsciiTheme.Beg;
bar.BegFilled = AsciiTheme.BegFilled;
bar.Filler = AsciiTheme.Filler;
bar.Space = AsciiTheme.Space;
bar.Head = AsciiTheme.Head;
bar.Fin = AsciiTheme.Fin;
bar.FinFilled = AsciiTheme.FinFilled;
}
else
{
bar.Beg = UnicodeTheme.Beg;
bar.BegFilled = UnicodeTheme.BegFilled;
bar.Filler = UnicodeTheme.Filler;
bar.Space = UnicodeTheme.Space;
bar.Head = UnicodeTheme.Head;
bar.Fin = UnicodeTheme.Fin;
bar.FinFilled = UnicodeTheme.FinFilled;
}
}
private static List<string> FlattenTargets(List<string> targets)
{
var flat = new List<string>(targets.Count);
foreach (var target in targets)
{
CollectInner(target, flat);
}
return flat;
}
private static void CollectInner(string path, List<string> flat)
{
var innerDirs = Directory.GetDirectories(path);
foreach (var innerDir in innerDirs)
{
CollectInner(innerDir, flat);
}
flat.Add(path);
}
private static List<string> GetTargets(RmrfArgs args)
{
var res = new List<string>();
var path = args.Path;
if (args.Include == null)
{
res.Add(path);
return res;
}
CollectIncludes(path, args, res);
return res;
}
private static void CollectIncludes(string path, RmrfArgs args, List<string> targets)
{
var dirs = Directory.GetDirectories(path);
var toExplore = new List<string>();
foreach (var dir in dirs)
{
var name = Path.GetFileName(dir);
if (args.Include.Any(f => f == name))
{
targets.Add(dir);
}
else if (args.Exclude != null && args.Exclude.Any(f => f == name))
{
continue;
}
else
{
toExplore.Add(dir);
}
}
foreach (var dir in toExplore)
{
CollectIncludes(dir, args, targets);
}
}
private static bool TryParse(string[] args, out RmrfArgs res)
{
int ind = 0;
res = new RmrfArgs();
res.Path = Directory.GetCurrentDirectory();
while (ind < args.Length)
{
if (args[ind] == "-p" && ind < args.Length - 1)
{
ind++;
res.Path = args[ind];
ind++;
}
else if (args[ind] == "-i" && ind < args.Length - 1)
{
ind++;
res.Include = args[ind].Split(',');
ind++;
}
else if (args[ind] == "-e" && ind < args.Length - 1)
{
ind++;
res.Exclude = args[ind].Split(',');
ind++;
}
else if (args[ind] == "-ascii")
{
ind++;
res.Ascii = true;
}
else if (args[ind] == "-dry")
{
ind++;
res.DryRun = true;
}
else if (args[ind] == "-na")
{
ind++;
res.NoAnimation = true;
}
else if (args[ind] == "-na")
{
ind++;
res.NoAnimation = true;
}
else if (args[ind] == "-?")
{
PrintUsage(Console.Out);
return false;
}
else
{
var tw = Console.Error;
tw.WriteLine("Invalid argument list");
tw.WriteLine();
PrintUsage(tw);
return false;
}
}
if (res.Ascii)
{
res.Cat = false;
}
if (Console.IsOutputRedirected)
{
res.NoAnimation = true;
}
return true;
}
private static void PrintUsage(TextWriter tw)
{
tw.WriteLine("rmrf [args]");
tw.WriteLine("args list:");
tw.WriteLine(" -p <folder1>: path");
tw.WriteLine(" -i <folder1,folder2,...>: directories to delete");
tw.WriteLine(" -e <folder1,folder2,...>: exclude paths");
tw.WriteLine(" -na: no animation");
tw.WriteLine(" -dry: dry run");
tw.WriteLine(" -ascii: ascii theme");
tw.WriteLine(" -?: show this message");
}
private static void DrawInitial(StreamWriter sw, Bar bar, RmrfArgs args)
{
if (args.NoAnimation)
{
OutText(sw, NL);
OutText(sw, "Start deleting");
OutText(sw, NL);
return;
}
OutEsc(sw, "?25l");
OutEsc(sw, "1G");
OutText(sw, bar.Beg);
for (int i = 0; i < bar.Width; i++)
{
OutText(sw, bar.Space);
}
OutText(sw, bar.Fin);
OutText(sw, " 0%");
if (args.Cat)
{
OutText(sw, " ");
OutText(sw, _cats[0]);
}
}
private static void DrawComplete(StreamWriter sw, RmrfArgs args, bool success, Exception e = null)
{
if (args.NoAnimation)
{
DrawCompleteNoAnimation(sw, success, e);
return;
}
OutEsc(sw, "0m");
OutText(sw, NL);
if (success)
{
OutEsc(sw, "32m");
OutText(sw, "Over");
} else {
OutEsc(sw, "31m");
OutText(sw, "Failed");
}
OutText(sw, NL);
OutEsc(sw, "0m");
if (e != null)
{
OutText(sw, NL);
OutText(sw, e.ToString());
}
OutEsc(sw, "?25h");
}
private static void DrawCompleteNoAnimation(StreamWriter sw, bool success, Exception e = null)
{
if (success)
{
OutText(sw, "Over");
return;
}
OutText(sw, "Failed");
if (e != null)
{
OutText(sw, NL);
OutText(sw, e.ToString());
}
}
private static void Recover(StreamWriter sw)
{
OutEsc(sw, "0m");
OutEsc(sw, "?25h");
}
private static void MoveDelta(
StreamWriter sw,
Bar bar,
RmrfArgs args,
int nextInd,
int nextPct,
long elapsed)
{
if (args.NoAnimation)
{
MoveDeltaNoAnimation(sw, bar, args, nextInd, nextPct, elapsed);
return;
}
var catFrame = GetCatFrame(bar, args, elapsed);
if (nextInd == bar.Ind &&
nextPct == bar.Pct &&
catFrame == bar.CatFrame)
return;
if (nextInd != bar.Ind)
{
if (bar.Ind == 0)
{
OutEsc(sw, 1 + "G");
OutText(sw, bar.BegFilled);
}
OutEsc(sw, 2 + bar.Ind + "G");
for (int i = bar.Ind; i < nextInd; i++)
{
OutText(sw, bar.Filler);
}
if (nextInd == bar.Width - 1)
{
OutText(sw, bar.Filler);
OutText(sw, bar.FinFilled);
}
else
{
OutText(sw, bar.Head);
}
}
if (nextPct != bar.Pct)
{
OutEsc(sw, 4 + bar.Width + "G");
var pctStr = nextPct.ToString().PadLeft(3, ' ');
OutText(sw, pctStr);
}
if (catFrame != bar.CatFrame)
{
OutEsc(sw, 4 + 4 + bar.Width + "G");
OutText(sw, " ");
OutText(sw, _cats[catFrame]);
bar.ElapsedMs = elapsed;
}
bar.Ind = nextInd;
bar.Pct = nextPct;
bar.CatFrame = catFrame;
}
private static void MoveDeltaNoAnimation(
StreamWriter sw,
Bar bar,
RmrfArgs args,
int nextInd,
int nextPct,
long elapsed)
{
const int delay = 10;
if (nextPct - bar.Pct >= delay || (nextPct == 100 && bar.Pct != 100))
{
OutText(sw, $"Deleted {nextPct}%");
OutText(sw, NL);
bar.Pct = nextPct;
}
}
private static int GetCatFrame(Bar bar, RmrfArgs args, long elapsed)
{
if (!args.Cat)
return 0;
const int delay = 700;
if (elapsed - bar.ElapsedMs > delay)
return (bar.CatFrame + 1) % _cats.Length;
return bar.CatFrame;
}
private static void OutEsc(StreamWriter sw, string str)
{
sw.Write("\x1b[");
sw.Write(str);
}
private static void OutText(StreamWriter sw, string str)
{
sw.Write(str);
}
}