This repository has been archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
128 lines (101 loc) · 3.27 KB
/
main.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
#include <conio.h>
#include <iostream>
#include <filesystem>
#include <windows.h>
#include <map>
using namespace std;
using namespace filesystem;
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KILO 1000
#define MEGA 1000000
#define GIGA 1000000000
filesystem::path selectedPath;
struct DiveData {
string filename;
unsigned long long int size = 0;
};
struct DiveDirectory : DiveData {
map<string, DiveData> content;
};
string fileSizeInUnits(unsigned long long int fileSize) {
if (fileSize >= GIGA) {
return to_string(fileSize / GIGA) + " GB";
} else if (fileSize >= MEGA) {
return to_string(fileSize / MEGA) + " MB";
} else if (fileSize >= KILO) {
return to_string(fileSize / KILO) + " kB";
}
return to_string(fileSize) + " B";
}
DiveDirectory dive(filesystem::path path) {
DiveDirectory data = {{.filename = path.filename().string()},
{}};
try {
for (const auto &entry: directory_iterator(path.string(), directory_options::skip_permission_denied)) {
filesystem::path entryPath = entry.path();
string filename = entryPath.filename().string();
if (filename.starts_with("$")) {
continue;
}
if (entry.is_regular_file()) {
unsigned long long int filesize = entry.file_size();
data.content[entryPath.filename().string()] = {.filename = filename, .size = filesize};
data.size += filesize;
} else if (entry.is_directory()) {
DiveData diveData = dive(entry.path());
data.content[entryPath.filename().string()] = diveData;
data.size += diveData.size;
}
}
} catch (...) {}
return data;
}
void clearConsole() {
COORD topLeft = {0, 0};
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written);
FillConsoleOutputAttribute(console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written);
SetConsoleCursorPosition(console, topLeft);
}
void drawConsole(DiveData data) {
cout << "Size: " << fileSizeInUnits(data.size);
}
void handleInput(short keyCode) {
switch (keyCode) {
case KEY_UP:
cout << endl << "Up" << endl;
break;
case KEY_DOWN:
cout << endl << "Down" << endl;
break;
case KEY_LEFT:
cout << endl << "Left" << endl;
break;
case KEY_RIGHT:
cout << endl << "Right" << endl;
break;
default:
cout << endl << "null" << endl;
break;
}
}
int main() {
short offset = 80;
filesystem::path initialPath = filesystem::path("C:\\Users\\lukas\\Desktop\\Repositories");
selectedPath = initialPath;
cout << "Reading: " << initialPath.string() << endl;
DiveData data = dive(initialPath);
clearConsole();
drawConsole(data);
while (true) {
handleInput(getch());
}
return 0;
}