-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathside.cpp
319 lines (275 loc) · 7.03 KB
/
side.cpp
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
#include "side.h"
HANDLE side::hand = GetStdHandle(STD_OUTPUT_HANDLE);
const size_t side::countFiles = 20;
side::side(string p, size_t x, size_t y)
: indexFile(0), upIndexFile(0), indexRow(0), path(std::move(p)), posX(x), posY(y), isActive(true)
{
if (!fillFiles())
throw string("Can't read the directory: " + path);
loadFilesPath();
}
void side::loadFilesPath()
{
size_t lastPos = 0;
size_t findedPos = 0;
filesPath.clear();
do
{
findedPos = path.find('\\', lastPos);
filesPath.emplace_back(path, lastPos, findedPos - lastPos);
if (findedPos == path.size()-1)
break;
lastPos = findedPos + 1;
} while (findedPos != string::npos);
}
bool side::fillFiles()
{
files.clear();
_finddata_t finfo;
intptr_t done = _findfirst((path + "*.*").c_str(), &finfo);
if (done == -1)
{
_findclose(done);
return false;
}
if (_stricmp(finfo.name, ".")) files.push_back(finfo);
while (_findnext(done, &finfo) != -1)
if (_stricmp(finfo.name, ".")) files.push_back(finfo);
_findclose(done);
return true;
}
void side::show() const
{
if (!files.size())
throw string("Nothing to show in func show()");
string bufLine;
for (size_t a = 0; a < countFiles; ++a)
{
SetConsoleCursorPosition(hand, { posX, posY + a });
if (a+upIndexFile < files.size())
{
SetConsoleTextAttribute(hand, a + upIndexFile == indexFile ? 7 | 2 << 4 : 7);
makeLine(files[a+upIndexFile], bufLine);
cout << bufLine;
}
else
{
SetConsoleTextAttribute(hand, 7);
printf_s("%c %c %c %c", 179, 179, 179, 179);
}
}
}
void side::showTwoLines(size_t row) const
{
if (row + 1 + upIndexFile >= files.size())
throw myException<4, string, size_t>{{ string("out of range in showTwoLines"), 0 },
{ "Max index", files.size() - 1 },
{ "upIndexFile: ", upIndexFile },
{ "Argument row+1: ", row+1} };
string bufLine;
for (size_t a = 0; a < 2; ++a)
{
SetConsoleCursorPosition(hand, { posX, posY + a + row });
SetConsoleTextAttribute(hand, a + row + upIndexFile == indexFile ? 7 | 2 << 4 : 7);
makeLine(files[a + row + upIndexFile], bufLine);
cout << bufLine;
}
}
void side::makeLine(const _finddata_t &finfo, string &str) const
{
str.clear();
str += char(179);
/*name*/
if (strlen(finfo.name) > 19)
str += string(finfo.name, 19);
else
str += finfo.name + string(19 - strlen(finfo.name), ' ');
str += char(179);
/*size*/
string size;
if (finfo.size >= 1000 * 1000 * 1000) // > 1 gb
size = std::to_string(finfo.size / (1024 * 1024 * 1024)) + ',' + std::to_string(finfo.size % (1024 * 1024 * 1024) / 1024 / 1024 *100 / 1024) + 'G';
else if (finfo.size >= 1000 * 1000) // > 1 mb < 1 gb
size = std::to_string(finfo.size / (1024 * 1024)) + ',' + std::to_string(finfo.size % (1024 * 1024) * 100 / (1024 * 1024)) + 'M';
else if (finfo.size >= 1000) // > 1 kb < 1 mb
size = std::to_string(finfo.size / 1024) + ',' + std::to_string(finfo.size % 1024 * 100 / 1024) + 'K';
else // < 1 kb
size = std::to_string(finfo.size) + 'B';
if (size.size() > 7)
throw string("lenght of str:size > 7 : ") + size;
if (size.size() < 7)
size.insert(0, 7 - size.size(), ' ');
str += size + char(179);
/*date*/
tm t;
localtime_s(&t, &finfo.time_write);
if (t.tm_mday < 10)
str += '0';
str += std::to_string(t.tm_mday) + '.';
if (t.tm_mon < 9)
str += '0';
str += std::to_string(t.tm_mon + 1) + '.' + std::to_string(t.tm_year + 1900);
str += char(179);
}
bool side::mooveDown()
{
if (indexFile == files.size() - 1)
return false;
else if (indexRow == countFiles - 1) // ó ñàìîìó íèæíüîìó ïîëîæåíí³
{
++upIndexFile;
++indexFile;
show(); // òðåáà ïåðåïèñàòè âåñü ñïèñîê
}
else // ïðîñòî ñïóñòèòè êóðñîð íà 1
{
++indexRow;
++indexFile;
showTwoLines(indexRow - 1);
}
return true;
}
bool side::mooveUp()
{
if (indexFile == 0)
return false;
else if (indexRow == 0)
{
--upIndexFile;
--indexFile;
show();
}
else
{
--indexRow;
--indexFile;
showTwoLines(indexRow);
}
return true;
}
bool side::enter()
{
if (filesPath.size() < 2 && !(_stricmp(files[indexFile].name, "..")))
return false;
else if (!(_stricmp(files[indexFile].name, ".."))) //..
{
string tName(filesPath.back());
filesPath.pop_back();
makePath();
if (!fillFiles())
throw string("Can't read the directory: " + path);
indexFile = getIndexByNameInCurrent(tName);
if (indexFile >= side::countFiles) // íå âì³ùàºòüñÿ â ïåðøó (çíà÷èòü õç :) äåñü ïîñåðåäèíö³ ïîñòàâèìî :))
{
indexRow = 10;
upIndexFile = indexFile - 10;
}
else // âàì³ùàºòüñÿ â ïåðøèé êîìëåêò
{
upIndexFile = 0;
indexRow = indexFile;
}
show();
writePath();
return true;
}
else if ((_stricmp(files[indexFile].name, "..")) && !(files[indexFile].attrib & _A_SUBDIR))
return false;
else // not .. but folder
{
filesPath.emplace_back(files[indexFile].name);
makePath();
if (!fillFiles())
throw string("Can't read the directory: " + path);
clearIndex();
show();
writePath();
return true;
}
}
void side::makeDir(string name)
{
_mkdir((path + name).c_str());
fillFiles();
indexFile = getIndexByNameInCurrent(name);
if (indexFile >= side::countFiles) // íå âì³ùàºòüñÿ â ïåðøó (çíà÷èòü õç :) äåñü ïîñåðåäèíö³ ïîñòàâèìî :))
{
indexRow = 10;
upIndexFile = indexFile - 10;
}
else // âàì³ùàºòüñÿ â ïåðøèé êîìëåêò
{
upIndexFile = 0;
indexRow = indexFile;
}
show();
}
size_t side::getIndexByNameInCurrent(string name) const
{
/* _finddata_t finfo;
size_t counter = 0;
intptr_t done = _findfirst((path + "*.*").c_str(), &finfo);
if (done == -1)
return false;
if (_stricmp(finfo.name, name.c_str())) return 0; // íà íóëüîâîìó ³íäåêñ³
while (_findnext(done, &finfo) != -1)
{
++counter;
if (_stricmp(finfo.name, name.c_str())) return counter;
}
_findclose(done);
throw string("We didn't find dir: " + name + "in: " + path);
*/
for (size_t a = 0; a < files.size(); ++a)
{
if (!_stricmp(files[a].name, name.c_str())) // ÿêùî ³äåíòè÷í³
return a;
}
throw string("We didn't find dir: " + name + "in: " + path);
}
void side::makePath()
{
path.clear();
for (auto &a : filesPath)
path += (a + '\\');
}
bool side::inactive()
{
if (!isActive)
return false;
SetConsoleCursorPosition(hand, { posX, posY + indexRow });
SetConsoleTextAttribute(hand, 7 | 1 << 4);
string buf;
makeLine(files[indexFile], buf);
cout << buf;
isActive = false;
return true;
}
bool side::active()
{
if (isActive)
return false;
SetConsoleCursorPosition(hand, { posX, posY + indexRow });
SetConsoleTextAttribute(hand, 7 | 2 << 4);
string buf;
makeLine(files[indexFile], buf);
cout << buf;
isActive = true;
return true;
}
void side::writePath() const
{
SetConsoleCursorPosition(side::hand, { posX, posY - 1 });
SetConsoleTextAttribute(side::hand, 7);
for (size_t a = 39; a; --a) cout << ' ';
cout << char(179);
SetConsoleCursorPosition(side::hand, { posX, posY - 1 });
// øèðèíà ó íàñ 40
// |c:\... - 7
// 33 - çàëèøêó
cout << char(179);
if (path.size() <= 39)
cout << path;
else
cout << path.substr(0, 3) << "..." << path.substr(path.size() - 31 - 1);
}