-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTab.cs
360 lines (351 loc) · 16.3 KB
/
Tab.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
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
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using Coal.Draw;
using Coal.Search;
using Coal.CopyDir;
using System.Diagnostics;
namespace Coal {
class Tab {
private bool _isSecond = false;
private CoalCore _core;
private ArrayList _currentTabContains;
private DirectoryInfo _currentDirectory;
public DirectoryInfo CurrentDirectory { get => _currentDirectory; }
private object _selectedItem;
private int _scrollMaxClearValue;
private int _selectedIndex;
private int _scrollOffset;
public int TabHeight { get; set; }
public int TabWidth { get; set; }
public Tab() {
_currentDirectory = null;
_currentTabContains = null;
_selectedItem = null;
_core = null;
_selectedIndex = _scrollOffset = TabHeight = TabWidth = 0;
}
public Tab(CoalCore core, DirectoryInfo currentDirectory, bool iS = false) {
_isSecond = iS;
_core = core;
_currentDirectory = currentDirectory;
_currentTabContains = new ArrayList();
_currentTabContains.AddRange(currentDirectory.GetDirectories());
_currentTabContains.AddRange(currentDirectory.GetFiles());
_selectedItem = _currentTabContains[0];
_selectedIndex = _scrollOffset = 0;
TabHeight = Console.BufferHeight - 5;
TabWidth = Console.BufferWidth/2;
CalculateMax();
}
public void CalculateMax() {
int max = 0;
for (int i = 0; i < _currentTabContains.Count; i++) {
if (_currentTabContains[i] is FileInfo) {
if ((_currentTabContains[i] as FileInfo).Name.Length > max) {
max = (_currentTabContains[i] as FileInfo).Name.Length;
}
}
else if(_currentTabContains[i] is DirectoryInfo) {
if ((_currentTabContains[i] as DirectoryInfo).Name.Length > max) {
max = (_currentTabContains[i] as DirectoryInfo).Name.Length;
}
}
else if(_currentTabContains[i] is DriveInfo) {
if ((_currentTabContains[i] as DriveInfo).Name.Length > max) {
max = (_currentTabContains[i] as DriveInfo).Name.Length;
}
}
}
_scrollMaxClearValue = max + 1;
}
public void DirectoryChangedEventHandler() {
_currentTabContains.Clear();
_currentTabContains.AddRange(_currentDirectory.GetDirectories());
_currentTabContains.AddRange(_currentDirectory.GetFiles());
}
public void Draw() {
Borders.DrawBorder(TabHeight, TabWidth, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, 0, _isSecond);
Directories.DrawCurrentDirectory(TabWidth, _currentDirectory?.FullName, _isSecond, _core.CurrentTab==this?true:false);
if (!_isSecond)
BottomMenu.DrawMenu(TabHeight);
}
public void EscapeKeyPressedHandler() {
if (_currentDirectory?.Parent != null) {
_scrollOffset = 0;
_selectedIndex = 0;
_currentDirectory = _currentDirectory.Parent;
Events.CallDirectoryChanged();
Directories.DrawCurrentDirectory(TabWidth, _currentDirectory?.FullName, _isSecond, true);
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
CalculateMax();
}
else if(_currentDirectory == null) {
_scrollOffset = 0;
_selectedIndex = 0;
_currentDirectory = DriveInfo.GetDrives()[0].RootDirectory;
Events.CallDirectoryChanged();
Directories.DrawCurrentDirectory(TabWidth, _currentDirectory?.FullName, _isSecond, true);
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
CalculateMax();
}
else if(_currentDirectory.Parent == null) {
_scrollOffset = 0;
_selectedIndex = 0;
_currentDirectory = null;
_currentTabContains.Clear();
_currentTabContains.AddRange(DriveInfo.GetDrives());
Directories.DrawCurrentDirectory(TabWidth, _currentDirectory?.FullName, _isSecond, true);
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
CalculateMax();
}
_core.CallEventDrawClock();
}
public void ArrowDownKeyPressedHandler() {
_selectedIndex++;
if (_selectedIndex > _currentTabContains.Count - 1) {
_selectedIndex = _currentTabContains.Count - 1;
}
else if (_selectedIndex > TabHeight - 3 + _scrollOffset && _selectedIndex < _currentTabContains.Count) {
_scrollOffset++;
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
return;
}
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
}
public void ArrowUpKeyPressedHandler() {
_selectedIndex--;
if (_selectedIndex < 0) {
_selectedIndex = 0;
return;
}
if (_selectedIndex < _scrollOffset) {
_scrollOffset--;
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue,_isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
return;
}
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
}
public void F2KeyPressedHandler() {
bool isPressed = false;
Info.Execute(TabHeight, TabWidth*2, _selectedItem);
while(!isPressed) {
ConsoleKeyInfo key = Console.ReadKey(true);
if(key.Key == ConsoleKey.Escape) {
isPressed = true;
_core.DrawBothTabs();
}
}
}
private void DeleteFile() {
if(Confirmation.Execute(TabHeight, TabWidth*2, "Are you sure that you want to delete this file?")) {
(_selectedItem as FileInfo).Delete();
Events.CallDirectoryChanged();
if(_core.NonActiveTab._currentDirectory != null)
_core.NonActiveTab.DirectoryChangedEventHandler();
_core.DrawBothTabs();
}
else {
_core.DrawBothTabs();
}
}
private void DeleteDirectory() {
bool res = Confirmation.Execute(TabHeight, TabWidth * 2, "Warning! All files inside directory will be also deleted!");
if(res) {
(_selectedItem as DirectoryInfo).Delete(true);
Events.CallDirectoryChanged();
_core.NonActiveTab.DirectoryChangedEventHandler();
}
_core.DrawBothTabs();
}
public void EnterKeyPressedHandler() {
if (_selectedItem is DirectoryInfo) {
try {
_selectedIndex = 0;
_scrollOffset = 0;
_currentDirectory = _selectedItem as DirectoryInfo;
Events.CallDirectoryChanged();
Directories.DrawCurrentDirectory(TabWidth, _currentDirectory.FullName, _isSecond, true);
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
CalculateMax();
}
catch (Exception) {
Error.Execute(TabHeight, TabWidth*2, "You have no permissions to enter this folder");
if (_currentDirectory.Parent != null) {
_currentDirectory = _currentDirectory.Parent;
}
else {
_currentDirectory = DriveInfo.GetDrives()[0].RootDirectory;
}
Events.CallDirectoryChanged();
_core.DrawBothTabs();
}
}
else if(_selectedItem is FileInfo) {
try {
Process.Start((_selectedItem as FileInfo).FullName);
} catch(Exception e) {
Error.Execute(TabHeight, TabWidth*2, e.Message);
_core.DrawBothTabs();
}
}
else if(_selectedItem is DriveInfo) {
try {
_selectedIndex = 0;
_scrollOffset = 0;
_currentDirectory = (_selectedItem as DriveInfo).RootDirectory;
Events.CallDirectoryChanged();
Directories.DrawCurrentDirectory(TabWidth, _currentDirectory.FullName, _isSecond, true);
Clear.ClearTab(TabHeight, TabWidth, _scrollMaxClearValue, _isSecond);
Directories.DrawDirectoriesAndFiles(_currentTabContains, ref _selectedItem, _selectedIndex, TabHeight, TabWidth, _scrollOffset, _isSecond);
CalculateMax();
} catch (Exception) {
Error.Execute(TabHeight, TabWidth * 2, "You have no permissions to enter this drive");
_core.DrawBothTabs();
}
}
_core.CallEventDrawClock();
}
public void F3KeyPressedHandler() {
string movePath = Move.Execute(TabHeight, TabWidth*2);
if (movePath != null && movePath != String.Empty) {
if (_selectedItem is FileInfo) {
try {
(_selectedItem as FileInfo).MoveTo(movePath + (_selectedItem as FileInfo).Name);
}
catch (Exception e) {
Error.Execute(TabHeight, TabWidth*2, e.Message);
}
}
else if (_selectedItem is DirectoryInfo) {
try {
(_selectedItem as DirectoryInfo).MoveTo(movePath + (_selectedItem as DirectoryInfo).Name);
}
catch (Exception e) {
Error.Execute(TabHeight, TabWidth*2, e.Message);
}
}
Events.CallDirectoryChanged();
_core.NonActiveTab.DirectoryChangedEventHandler();
}
_core.DrawBothTabs();
}
public void F4KeyPressHandler() {
string newName = Edit.Execute(TabWidth*2, TabHeight);
if(newName != null && newName != String.Empty) {
if(_selectedItem is FileInfo) {
try {
(_selectedItem as FileInfo).CopyTo(_currentDirectory.FullName + "\\" + newName + (_selectedItem as FileInfo).Extension);
(_selectedItem as FileInfo).Delete();
} catch(Exception e) {
Error.Execute(TabHeight, TabWidth, e.Message);
}
}
else if (_selectedItem is DirectoryInfo) {
try {
(_selectedItem as DirectoryInfo).MoveTo(CurrentDirectory.FullName + newName);
}
catch (Exception e) {
Error.Execute(TabHeight, TabWidth, e.Message);
}
}
Events.CallDirectoryChanged();
_core.NonActiveTab.DirectoryChangedEventHandler();
}
_core.DrawBothTabs();
}
public void F5KeyPressedHandler() {
string copyPath = Copy.Execute(TabHeight, TabWidth*2);
if(copyPath != null && copyPath != String.Empty) {
if (_selectedItem is FileInfo) {
try {
(_selectedItem as FileInfo).CopyTo(copyPath);
}
catch (Exception e) {
Error.Execute(TabHeight, TabWidth * 2, e.Message);
}
}
else if (_selectedItem is DirectoryInfo) {
try {
CopyDirectory cp = new CopyDirectory();
cp.CopyDir(_selectedItem as DirectoryInfo, new DirectoryInfo(copyPath));
} catch(Exception e) {
Error.Execute(TabHeight, TabWidth * 2, e.Message);
}
}
Events.CallDirectoryChanged();
_core.NonActiveTab.DirectoryChangedEventHandler();
}
_core.DrawBothTabs();
}
public void F6KeyPressedHandler() {
string path = MakeDir.Execute(TabHeight, TabWidth * 2);
if(path != null && path != String.Empty) {
Directory.CreateDirectory(path);
Events.CallDirectoryChanged();
_core.NonActiveTab.DirectoryChangedEventHandler();
_core.DrawBothTabs();
}
else {
_core.DrawBothTabs();
}
}
public void F7KeyPressedHandler() {
if (_selectedItem is FileInfo) {
DeleteFile();
}
else if (_selectedItem is DirectoryInfo) {
DeleteDirectory();
}
}
public void F8KeyPressedHandler() {
string filename = MkFile.Execute(TabWidth*2, TabHeight);
if(filename != null && filename != string.Empty) {
try {
FileStream fs = File.Create(_currentDirectory.FullName + "/" + filename);
Events.CallDirectoryChanged();
if(_core.NonActiveTab._currentDirectory != null)
_core.NonActiveTab.DirectoryChangedEventHandler();
fs.Dispose();
} catch(Exception e) {
_core.DrawBothTabs();
Error.Execute(TabHeight, TabWidth*2, e.Message);
}
}
else {
_core.DrawBothTabs();
Error.Execute(TabHeight, TabWidth*2, "Cannot create file.");
}
_core.DrawBothTabs();
}
//TODO: Переход в директорию и обратно в поиске
public void F9KeyPressedHandler() {
try {
SearchEngine.ClearSearchResult();
string[] searchParams = Coal.Draw.Search.Execute(TabHeight, TabWidth * 2);
if (searchParams != null && searchParams[0] != string.Empty && searchParams[1] != string.Empty) {
SearchEngine.SearchByExpression(searchParams[0], new DirectoryInfo(searchParams[1]));
_currentTabContains = SearchEngine.GetSearchResult();
CalculateMax();
_currentDirectory = null;
_core.DrawBothTabs();
}
else {
_core.DrawBothTabs();
}
} catch(Exception e) {
Error.Execute(TabHeight, TabWidth*2, e.Message);
_core.DrawBothTabs();
}
}
}
}