-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·598 lines (500 loc) · 18 KB
/
main.cpp
File metadata and controls
executable file
·598 lines (500 loc) · 18 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <ncurses.h>
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <cstring>
#include <memory>
#include <unordered_set>
namespace fs = std::filesystem;
// Define color pairs
#define COLOR_DIRECTORY 1
#define COLOR_TEXT_FILE 2
// Function to initialize the set of supported file extensions
std::unordered_set<std::string> initSupportedExtensions() {
std::unordered_set<std::string> extensions;
// Add all supported extensions with dot prefix
// Common text and config files
extensions.insert(".txt");
extensions.insert(".md");
extensions.insert(".markdown");
extensions.insert(".conf");
extensions.insert(".ini");
extensions.insert(".cfg");
extensions.insert(".properties");
// Shell scripts
extensions.insert(".sh");
extensions.insert(".bash");
extensions.insert(".ksh");
extensions.insert(".csh");
extensions.insert(".zsh");
// Programming languages
extensions.insert(".c");
extensions.insert(".h");
extensions.insert(".cpp");
extensions.insert(".hpp");
extensions.insert(".cc");
extensions.insert(".cxx");
extensions.insert(".cs");
extensions.insert(".java");
extensions.insert(".py");
extensions.insert(".rb");
extensions.insert(".js");
extensions.insert(".ts");
extensions.insert(".php");
extensions.insert(".pl");
extensions.insert(".pm");
extensions.insert(".go");
extensions.insert(".rs");
extensions.insert(".swift");
extensions.insert(".lua");
extensions.insert(".r");
extensions.insert(".scala");
extensions.insert(".groovy");
extensions.insert(".kt");
extensions.insert(".dart");
// Web development
extensions.insert(".html");
extensions.insert(".htm");
extensions.insert(".css");
extensions.insert(".scss");
extensions.insert(".sass");
extensions.insert(".less");
extensions.insert(".json");
extensions.insert(".xml");
extensions.insert(".svg");
extensions.insert(".jsx");
extensions.insert(".tsx");
// Data formats
extensions.insert(".csv");
extensions.insert(".yaml");
extensions.insert(".yml");
extensions.insert(".toml");
// Documentation
extensions.insert(".rst");
extensions.insert(".adoc");
extensions.insert(".tex");
extensions.insert(".man");
// Build and project files
extensions.insert(".pro");
extensions.insert(".cmake");
extensions.insert(".make");
extensions.insert(".mk");
extensions.insert(".mak");
extensions.insert(".gradle");
extensions.insert(".pom");
// Other common text-based files
extensions.insert(".log");
extensions.insert(".diff");
extensions.insert(".patch");
extensions.insert(".sql");
return extensions;
}
// Structure to hold file/directory information
struct FileEntry {
std::string name;
bool isDirectory;
uintmax_t size;
FileEntry(const std::string& n, bool isDir, uintmax_t s = 0)
: name(n), isDirectory(isDir), size(s) {}
};
// Class to manage the file browser
class FileBrowser {
private:
std::string currentPath;
std::vector<FileEntry> entries;
int selectedIndex = 0;
int startIndex = 0;
int maxDisplayEntries = 0;
WINDOW* window;
std::unordered_set<std::string> supportedExtensions;
public:
FileBrowser(WINDOW* win) : window(win) {
currentPath = fs::current_path().string();
// Initialize supported extensions
supportedExtensions = initSupportedExtensions();
refreshEntries();
}
void refreshEntries() {
entries.clear();
// Add parent directory entry
entries.push_back(FileEntry("..", true));
// Add directories and files
for (const auto& entry : fs::directory_iterator(currentPath)) {
if (entry.is_directory()) {
entries.push_back(FileEntry(entry.path().filename().string(), true));
} else if (entry.is_regular_file()) {
// Get file extension and check if it's supported
std::string ext = entry.path().extension().string();
if (supportedExtensions.find(ext) != supportedExtensions.end()) {
entries.push_back(FileEntry(entry.path().filename().string(), false, entry.file_size()));
}
}
}
// Sort entries: directories first, then files
std::sort(entries.begin() + 1, entries.end(),
[](const FileEntry& a, const FileEntry& b) {
if (a.isDirectory != b.isDirectory) {
return a.isDirectory > b.isDirectory;
}
return a.name < b.name;
});
// Reset selection
if (selectedIndex >= entries.size()) {
selectedIndex = entries.size() - 1;
}
if (selectedIndex < 0) {
selectedIndex = 0;
}
}
void display() {
int height, width;
getmaxyx(window, height, width);
// Calculate max entries that can be displayed
maxDisplayEntries = height - 3; // Reserve 3 lines for header
// Adjust startIndex if needed
if (selectedIndex < startIndex) {
startIndex = selectedIndex;
} else if (selectedIndex >= startIndex + maxDisplayEntries) {
startIndex = selectedIndex - maxDisplayEntries + 1;
}
// Clear window
werase(window);
// Draw border
box(window, 0, 0);
// Display current path
std::string pathDisplay = " " + currentPath + " ";
if (pathDisplay.length() > width - 4) {
pathDisplay = " ..." + pathDisplay.substr(pathDisplay.length() - width + 8) + " ";
}
mvwprintw(window, 0, 2, "%s", pathDisplay.c_str());
// Display column headers
mvwprintw(window, 1, 2, "%-*s %10s", width - 15, "Name", "Size");
mvwhline(window, 2, 1, ACS_HLINE, width - 2);
// Display entries
int displayCount = std::min(maxDisplayEntries, static_cast<int>(entries.size()));
for (int i = 0; i < displayCount; i++) {
int entryIndex = startIndex + i;
if (entryIndex >= entries.size()) break;
const FileEntry& entry = entries[entryIndex];
// Highlight selected entry
if (entryIndex == selectedIndex) {
wattron(window, A_REVERSE);
}
// Format entry display
std::string entryDisplay;
if (entry.isDirectory) {
// Apply blue color for directories
wattron(window, COLOR_PAIR(COLOR_DIRECTORY));
entryDisplay = "[" + entry.name + "]";
mvwprintw(window, i + 3, 2, "%-*s %10s", width - 15, entryDisplay.c_str(), "<DIR>");
wattroff(window, COLOR_PAIR(COLOR_DIRECTORY));
} else {
// Apply green color for text files
wattron(window, COLOR_PAIR(COLOR_TEXT_FILE));
mvwprintw(window, i + 3, 2, "%-*s %10lu", width - 15, entry.name.c_str(), entry.size);
wattroff(window, COLOR_PAIR(COLOR_TEXT_FILE));
}
if (entryIndex == selectedIndex) {
wattroff(window, A_REVERSE);
}
}
// Refresh window
wrefresh(window);
}
void moveUp() {
if (selectedIndex > 0) {
selectedIndex--;
display();
}
}
void moveDown() {
if (selectedIndex < entries.size() - 1) {
selectedIndex++;
display();
}
}
bool enter() {
if (entries.empty()) return false;
const FileEntry& selected = entries[selectedIndex];
if (selected.isDirectory) {
// Navigate to directory
if (selected.name == "..") {
fs::path parent = fs::path(currentPath).parent_path();
if (!parent.empty()) {
currentPath = parent.string();
}
} else {
currentPath = (fs::path(currentPath) / selected.name).string();
}
selectedIndex = 0;
startIndex = 0;
refreshEntries();
display();
return false;
} else {
// Selected a file
return true;
}
}
std::string getSelectedFilePath() {
if (selectedIndex >= 0 && selectedIndex < entries.size() && !entries[selectedIndex].isDirectory) {
return (fs::path(currentPath) / entries[selectedIndex].name).string();
}
return "";
}
};
// Class to display file contents
class FileViewer {
private:
WINDOW* window;
std::string filePath;
std::vector<std::string> lines;
int startLine = 0;
int maxDisplayLines = 0;
public:
FileViewer(WINDOW* win) : window(win) {}
bool loadFile(const std::string& path) {
filePath = path;
lines.clear();
startLine = 0;
// Try to open the file
std::ifstream file(path);
if (!file.is_open()) {
return false;
}
// Read file line by line
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
return true;
}
void display() {
int height, width;
getmaxyx(window, height, width);
// Calculate max lines that can be displayed
maxDisplayLines = height - 3; // Reserve 3 lines for header
// Clear window
werase(window);
// Draw border
box(window, 0, 0);
// Display file name
std::string fileDisplay = " " + fs::path(filePath).filename().string() + " ";
if (fileDisplay.length() > width - 4) {
fileDisplay = fileDisplay.substr(0, width - 7) + "... ";
}
mvwprintw(window, 0, 2, "%s", fileDisplay.c_str());
// Display line number header
mvwprintw(window, 1, 2, "Line");
mvwhline(window, 2, 1, ACS_HLINE, width - 2);
// Display file contents
if (lines.empty()) {
mvwprintw(window, 3, 2, "(Empty file)");
} else {
int displayCount = std::min(maxDisplayLines, static_cast<int>(lines.size() - startLine));
for (int i = 0; i < displayCount; i++) {
int lineIndex = startLine + i;
if (lineIndex >= lines.size()) break;
// Format line number
mvwprintw(window, i + 3, 2, "%4d", lineIndex + 1);
// Display line content (truncate if too long)
std::string lineContent = lines[lineIndex];
if (lineContent.length() > width - 8) {
lineContent = lineContent.substr(0, width - 11) + "...";
}
mvwprintw(window, i + 3, 7, "%s", lineContent.c_str());
}
}
// Refresh window
wrefresh(window);
}
void scrollUp() {
if (startLine > 0) {
startLine--;
display();
}
}
void scrollDown() {
if (startLine < lines.size() - maxDisplayLines) {
startLine++;
display();
}
}
void pageUp() {
startLine = std::max(0, startLine - maxDisplayLines);
display();
}
void pageDown() {
startLine = std::min(static_cast<int>(lines.size() - maxDisplayLines),
startLine + maxDisplayLines);
if (startLine < 0) startLine = 0;
display();
}
};
// Main application class
class Application {
private:
WINDOW* browserWindow;
WINDOW* viewerWindow;
FileBrowser* browser;
FileViewer* viewer;
bool running = true;
bool browserFocus = true; // Track which panel has focus
public:
Application() {
// Initialize ncurses
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(0); // Hide cursor
// Initialize colors if terminal supports them
if (has_colors()) {
start_color();
init_pair(COLOR_DIRECTORY, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_TEXT_FILE, COLOR_GREEN, COLOR_BLACK);
}
// Create windows
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
// Reserve the bottom line for help menu
browserWindow = newwin(maxY - 1, maxX / 2, 0, 0);
viewerWindow = newwin(maxY - 1, maxX / 2, 0, maxX / 2);
// Create browser and viewer
browser = new FileBrowser(browserWindow);
viewer = new FileViewer(viewerWindow);
// Display initial state
browser->display();
// Highlight the active panel (browser by default)
wattron(browserWindow, A_BOLD);
box(browserWindow, 0, 0);
wattroff(browserWindow, A_BOLD);
wrefresh(browserWindow);
// Display help at the bottom
updateHelpMenu();
}
void updateHelpMenu() {
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
// Clear the bottom line
move(maxY - 1, 0);
clrtoeol();
// Display help at the bottom
attron(A_REVERSE);
mvprintw(maxY - 1, 0, "Tab: Switch panels | Up/Down: Navigate/Scroll | Enter: Open | q: Quit");
attroff(A_REVERSE);
// Fill the rest of the line with spaces (for consistent highlighting)
int helpLen = 66; // Length of the help text
for (int i = helpLen; i < maxX; i++) {
mvaddch(maxY - 1, i, ' ' | A_REVERSE);
}
refresh();
}
~Application() {
delete browser;
delete viewer;
delwin(browserWindow);
delwin(viewerWindow);
endwin();
}
void run() {
// Make sure the help menu is always visible
updateHelpMenu();
// Make sure the browser and viewer are displayed initially
browser->display();
// Highlight the active panel (browser by default)
wattron(browserWindow, A_BOLD);
box(browserWindow, 0, 0);
wattroff(browserWindow, A_BOLD);
wrefresh(browserWindow);
// Make sure the viewer window is properly displayed too
box(viewerWindow, 0, 0);
wrefresh(viewerWindow);
// Refresh the help menu
updateHelpMenu();
// Refresh the screen to ensure everything is displayed
doupdate();
while (running) {
int ch = getch();
switch (ch) {
case '\t': // Tab key - switch focus
browserFocus = !browserFocus;
// Highlight the active panel by redrawing both
if (browserFocus) {
// Add highlight to browser window title
wattron(browserWindow, A_BOLD);
box(browserWindow, 0, 0);
wattroff(browserWindow, A_BOLD);
wrefresh(browserWindow);
// Remove highlight from viewer window
box(viewerWindow, 0, 0);
wrefresh(viewerWindow);
} else {
// Add highlight to viewer window title
wattron(viewerWindow, A_BOLD);
box(viewerWindow, 0, 0);
wattroff(viewerWindow, A_BOLD);
wrefresh(viewerWindow);
// Remove highlight from browser window
box(browserWindow, 0, 0);
wrefresh(browserWindow);
}
// Ensure help menu stays visible
updateHelpMenu();
break;
case KEY_UP:
if (browserFocus) {
browser->moveUp();
} else {
viewer->scrollUp();
}
break;
case KEY_DOWN:
if (browserFocus) {
browser->moveDown();
} else {
viewer->scrollDown();
}
break;
case 10: // Enter key
if (browserFocus && browser->enter()) {
// File selected, load it in viewer
std::string filePath = browser->getSelectedFilePath();
if (!filePath.empty() && viewer->loadFile(filePath)) {
viewer->display();
}
}
break;
case KEY_PPAGE: // Page Up
viewer->pageUp();
break;
case KEY_NPAGE: // Page Down
viewer->pageDown();
break;
case 'q':
case 'Q':
running = false;
break;
default:
// Ensure help menu stays visible after any action
updateHelpMenu();
break;
}
}
}
};
int main() {
try {
Application app;
app.run();
return 0;
} catch (const std::exception& e) {
endwin(); // Ensure terminal is restored
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}