-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterScan.cpp
More file actions
299 lines (258 loc) · 9.73 KB
/
InterScan.cpp
File metadata and controls
299 lines (258 loc) · 9.73 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
/*
============================================================
InterScan
============================================================
Description:
A lightweight terminal-based tool to visually display the
file structure of any directory in a tree-like format.
Features:
- Recursive directory traversal (full recursion)
- Optional ignored file extensions using `--ignore:` or `--ignore`
(e.g. --ignore: .cpp .json .py)
- Color-coded output for folders, files, extensions, and tree
- Sanitized user input and robust ignore-parsing
- Folder/File count displayed at the end
- Windows console APIs (GetFileAttributesA, FindFirstFileA)
============================================================
*/
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <conio.h>
// Console color enum (Windows console attribute values)
enum ConsoleColor {
/*
============================================================
Windows Console Color Reference (Foreground)
------------------------------------------------------------
Value | Color
------+----------------
0 | Black
1 | Blue
2 | Green
3 | Cyan
4 | Red
5 | Magenta
6 | Yellow
7 | Light Gray
8 | Dark Gray
9 | Light Blue
10 | Light Green
11 | Light Cyan
12 | Light Red
13 | Light Magenta
14 | Light Yellow
15 | White
============================================================
*/
DEFAULT = 7,
FOLDER = 1,
EXTENSION = 4,
TREE = 5,
PROMPT = 4
};
// --- trim both ends
static inline std::string trim(const std::string& s) {
size_t a = s.find_first_not_of(" \t\n\r");
if (a == std::string::npos) return "";
size_t b = s.find_last_not_of(" \t\n\r");
return s.substr(a, b - a + 1);
}
// --- lowercase copy
static inline std::string to_lower(const std::string& s) {
std::string out = s;
std::transform(out.begin(), out.end(), out.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return out;
}
// --- clear console
void clear_screen() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole == INVALID_HANDLE_VALUE) return;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
DWORD cellCount = csbi.dwSize.X * csbi.dwSize.Y;
DWORD count;
COORD home = { 0, 0 };
FillConsoleOutputCharacterA(hConsole, ' ', cellCount, home, &count);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, cellCount, home, &count);
SetConsoleCursorPosition(hConsole, home);
}
// --- is directory
bool is_directory(const std::string& path) {
DWORD attr = GetFileAttributesA(path.c_str());
return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
}
// --- get directory entries sorted (case-insensitive)
std::vector<std::string> get_directory_entries(const std::string& path) {
std::vector<std::string> entries;
std::string search = trim(path);
if (search.empty()) return entries;
std::string pattern = search + "\\*";
WIN32_FIND_DATAA fd;
HANDLE h = FindFirstFileA(pattern.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) return entries;
do {
std::string name = fd.cFileName;
if (name != "." && name != "..") entries.push_back(name);
} while (FindNextFileA(h, &fd) != 0);
FindClose(h);
std::sort(entries.begin(), entries.end(), [](const std::string& a, const std::string& b) {
return to_lower(a) < to_lower(b);
});
return entries;
}
// --- sanitize path (trim + remove surrounding quotes)
std::string sanitize_path(std::string input) {
input = trim(input);
if (!input.empty() && (input.front() == '"' || input.front() == '\'')) input.erase(0, 1);
if (!input.empty() && (input.back() == '"' || input.back() == '\'')) input.pop_back();
return trim(input);
}
// --- parse ignored extensions substring into normalized extensions (.ext lowercase)
std::vector<std::string> parse_ignored_extensions(const std::string& raw) {
std::string s = raw;
for (char& c : s) {
if (c == ',' || c == '&') c = ' ';
}
std::stringstream ss(s);
std::string tok;
std::vector<std::string> out;
while (ss >> tok) {
tok = trim(tok);
if (tok.empty()) continue;
if (tok.front() != '.') tok = "." + tok;
tok = to_lower(tok);
out.push_back(tok);
}
std::sort(out.begin(), out.end());
out.erase(std::unique(out.begin(), out.end()), out.end());
return out;
}
// --- check if filename's extension is ignored (case-insensitive)
bool is_ignored_extension(const std::string& filename, const std::vector<std::string>& ignored_exts) {
size_t pos = filename.find_last_of('.');
if (pos == std::string::npos) return false;
std::string ext = filename.substr(pos);
ext = to_lower(ext);
return std::find(ignored_exts.begin(), ignored_exts.end(), ext) != ignored_exts.end();
}
// --- recursive tree printer (full recursion) with counters
void print_tree(const std::string& path, const std::string& prefix, const std::vector<std::string>& ignored_exts,
int& folderCount, int& fileCount) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
auto items = get_directory_entries(path);
for (size_t i = 0; i < items.size(); ++i) {
bool isLast = (i == items.size() - 1);
std::string item = items[i];
std::string full = path + (path.back() == '\\' ? "" : "\\") + item;
std::string branch = isLast ? "#-->" : "|-->";
if (is_directory(full)) {
folderCount++;
SetConsoleTextAttribute(hConsole, TREE);
std::cout << prefix << branch;
SetConsoleTextAttribute(hConsole, FOLDER);
std::cout << "[" << item << "]" << std::endl;
SetConsoleTextAttribute(hConsole, DEFAULT);
std::string newPrefix = prefix + (isLast ? " " : "| ");
print_tree(full, newPrefix, ignored_exts, folderCount, fileCount);
}
else {
if (is_ignored_extension(item, ignored_exts)) continue;
fileCount++;
SetConsoleTextAttribute(hConsole, TREE);
std::cout << prefix << branch;
SetConsoleTextAttribute(hConsole, DEFAULT);
size_t dot = item.find_last_of('.');
if (dot != std::string::npos) {
std::cout << item.substr(0, dot);
SetConsoleTextAttribute(hConsole, EXTENSION);
std::cout << item.substr(dot) << std::endl;
}
else {
std::cout << item << std::endl;
}
SetConsoleTextAttribute(hConsole, DEFAULT);
}
}
}
// --- main
int main() {
SetConsoleOutputCP(CP_UTF8);
clear_screen();
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, PROMPT);
std::cout << "Enter Path : ";
SetConsoleTextAttribute(hConsole, DEFAULT);
std::string line;
std::getline(std::cin, line);
line = trim(line);
if (line.empty()) {
std::cout << "No input provided. Exiting." << std::endl;
_getch();
return 0;
}
// --- detect --ignore or --ignore: (case-insensitive)
std::string lowerLine = to_lower(line);
size_t ignorePos = std::string::npos;
size_t ignoreLen = 0;
const std::string key1 = "--ignore:";
const std::string key2 = "--ignore";
for (size_t i = 0; i < lowerLine.size(); ++i) {
if (lowerLine.compare(i, key1.size(), key1) == 0) { ignorePos = i; ignoreLen = key1.size(); break; }
if (lowerLine.compare(i, key2.size(), key2) == 0) { ignorePos = i; ignoreLen = key2.size(); break; }
}
std::string pathPart;
std::vector<std::string> ignored_exts;
if (ignorePos != std::string::npos) {
pathPart = line.substr(0, ignorePos);
std::string rawIgnore = line.substr(ignorePos + ignoreLen);
rawIgnore = trim(rawIgnore);
if (!rawIgnore.empty() && rawIgnore.front() == ':') rawIgnore.erase(0, 1);
ignored_exts = parse_ignored_extensions(rawIgnore);
}
else {
pathPart = line;
}
std::string path = sanitize_path(pathPart);
if (path.empty() || !is_directory(path)) {
std::cout << "Invalid or inaccessible directory path. Exiting." << std::endl;
_getch();
return 0;
}
size_t lastSlash = path.find_last_of("/\\");
std::string root = (lastSlash == std::string::npos) ? path : path.substr(lastSlash + 1);
if (root.empty()) root = path;
// Print root folder
SetConsoleTextAttribute(hConsole, FOLDER);
std::cout << "\n" << root << "\\" << std::endl;
SetConsoleTextAttribute(hConsole, DEFAULT);
// Show ignored extensions
if (!ignored_exts.empty()) {
SetConsoleTextAttribute(hConsole, TREE);
std::cout << "(Ignoring extensions:";
SetConsoleTextAttribute(hConsole, EXTENSION);
for (size_t i = 0; i < ignored_exts.size(); ++i) {
std::cout << " " << ignored_exts[i];
if (i + 1 < ignored_exts.size()) std::cout << ",";
}
SetConsoleTextAttribute(hConsole, DEFAULT);
std::cout << ")\n\n";
}
// --- folder/file counters
int folderCount = 0;
int fileCount = 0;
// Print directory tree with counters
print_tree(path, "", ignored_exts, folderCount, fileCount);
// Display totals
SetConsoleTextAttribute(hConsole, TREE);
std::cout << "\nFolders: " << folderCount << "\nFiles : " << fileCount << std::endl;
SetConsoleTextAttribute(hConsole, DEFAULT);
std::cout << "\nPress any key to exit..." << std::endl;
_getch();
return 0;
}