-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonTools.cs
159 lines (139 loc) · 4.67 KB
/
CommonTools.cs
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
using System;
using System.IO;
using System.Net;
namespace UralicNLP
{
/**
* Shared methods
*/
public class CommonTools
{
/**
* Deletes a directory and its contents
* @param dirPath Directory to be deleted
*/
public static void DeleteDir(string dirPath)
{
DirectoryInfo dir = new DirectoryInfo(dirPath);
if (dir.Exists)
{
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo subDir in dirs)
{
DeleteDir(subDir.FullName);
}
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
file.Delete();
}
}
dir.Delete(false);
}
/**
* Downloads a text file and returns it as a String
* @param targetURL URL of the text file
* @return The contents of the file as a string
* @throws Exception Fails if the file cannot be downloaded
*/
public static string ReadToString(string targetURL)
{
using (WebClient client = new WebClient())
{
string content = client.DownloadString(targetURL);
return content.Trim();
}
}
/**
* Downloads a file from the internet and saves it to the disk
* @param url URL of the file
* @param filePath Path where to save the file
* @param showProgress true to print out a progress bar
* @throws Exception May fail if the URL cannot be reached or the file cannot be written
*/
public static void DownloadToFile(string url, string filePath, bool showProgress)
{
using (WebClient client = new WebClient())
{
if (showProgress)
{
// Assuming ShellProgressBar is used for progress indication in C#
IProgressBar progressBar = new ConsoleProgressBar(100);
client.DownloadProgressChanged += (sender, e) =>
{
progressBar.ShowProgress(e.ProgressPercentage);
};
client.DownloadFile(url, filePath);
}
else
{
Console.WriteLine("Not showing download progress");
client.DownloadFile(url, filePath);
}
}
}
}
public class ConsoleProgressBar : IProgressBar
{
private const ConsoleColor ForeColor = ConsoleColor.Green;
private const ConsoleColor BkColor = ConsoleColor.Gray;
private const int DefaultWidthOfBar = 32;
private const int TextMarginLeft = 3;
private readonly int _total;
private readonly int _widthOfBar;
public ConsoleProgressBar(int total, int widthOfBar = DefaultWidthOfBar)
{
_total = total;
_widthOfBar = widthOfBar;
}
private bool _intited;
public void Init()
{
_lastPosition = 0;
//Draw empty progress bar
Console.CursorVisible = false;
Console.CursorLeft = 0;
Console.Write("["); //start
Console.CursorLeft = _widthOfBar;
Console.Write("]"); //end
Console.CursorLeft = 1;
//Draw background bar
for (var position = 1; position < _widthOfBar; position++) //Skip the first position which is "[".
{
Console.BackgroundColor = BkColor;
Console.CursorLeft = position;
Console.Write(" ");
}
}
public void ShowProgress(int currentCount)
{
if (!_intited)
{
Init();
_intited = true;
}
DrawTextProgressBar(currentCount);
}
private int _lastPosition;
public void DrawTextProgressBar(int currentCount)
{
//Draw current chunk.
var position = currentCount * _widthOfBar / _total;
if (position != _lastPosition)
{
_lastPosition = position;
Console.BackgroundColor = ForeColor;
Console.CursorLeft = position >= _widthOfBar ? _widthOfBar - 1 : position;
Console.Write(" ");
}
//Draw totals
Console.CursorLeft = _widthOfBar + TextMarginLeft;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(currentCount + " of " + _total + " "); //blanks at the end remove any excess
}
}
public interface IProgressBar
{
public void ShowProgress(int currentCount);
}
}