-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
244 lines (213 loc) · 7.44 KB
/
main.cpp
File metadata and controls
244 lines (213 loc) · 7.44 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
/**
* @file main.cpp
* @brief Main entry point for the CLI fuzzy finder application
*
* This file contains the main application logic including:
* - Command line argument parsing
* - Directory scanning in background thread
* - ncurses terminal interface setup
* - Main event loop for user interaction
* - Path selection and output
*/
#include "src/headers/fuzzy_find.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <set>
#include <vector>
#include <filesystem>
#include <locale.h>
#include <ncurses.h>
namespace fs = std::filesystem;
/**
* @brief Recursively scans directory for all files and subdirectories
*
* Traverses the filesystem starting from the given path, collecting
* all accessible files and directories. Skips directories that cause
* permission errors.
*
* @param path Starting directory path to scan
* @param dirs Output vector to store all discovered paths
*/
void get_directories(const fs::path path, std::vector<std::string> &dirs) {
for (fs::directory_entry dir_entry: fs::recursive_directory_iterator(path, fs::directory_options::skip_permission_denied)) {
dirs.push_back(dir_entry.path());
}
}
/**
* @brief Prints a limited number of directory paths (unused in current implementation)
*
* @param dirs Vector of directory paths
* @param entries Maximum number of entries to print
*/
void print_dirs(const std::vector<std::string> &dirs, int entries) {
for (int i = 0; i < entries && i < dirs.size(); i++) {
printw("%s\n", dirs[i].c_str());
}
}
/**
* @brief Creates a character set from search string for highlighting
*
* Converts the search string into a set of lowercase characters
* used for syntax highlighting in the terminal interface.
*
* @param search Search string to process
* @param char_set Output set to store unique characters
*/
void get_search_hashmap(const std::string &search, std::set<char> &char_set) {
char_set.clear();
for(int i = 0; i < search.size(); i++) char_set.insert(tolower(search[i]));
}
/**
* @brief Main application entry point
*
* Handles command line arguments, sets up the terminal interface,
* launches background directory scanning, and runs the main
* interactive loop for fuzzy finding.
*
* Command line options:
* - No args: Search from current directory
* - -h, --help: Show help (exits with code 5)
* - -s, --start: Search from home directory
* - [path]: Search from specified directory
*
* @param argc Number of command line arguments
* @param argv Array of command line argument strings
* @return 0 on success, 1 on error
*/
int main(int argc, char *argv[]) {
// Set locale for proper Unicode support
setlocale(LC_ALL, "");
char *home = getenv("HOME");
// Parse command line arguments
fs::path path(".");
if (argc > 2) {
std::cout << "Incorrect Amount of Arguments Given" << std::endl;
exit(1);
}
if (argc == 2) {
if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") {
exit(5);
} else if ( std::string(argv[1]) == "-s" || std::string(argv[1]) == "--start") {
path = home;
} else {
// Validate custom path argument
if (!fs::exists(argv[1])) {
std::cout << "Unknown argument given -- Try using -h for help" << std::endl;
exit(1);
}
path = std::string(argv[1]);
}
}
// Initialize directory list and start background scanning
std::vector<std::string> dirs;
dirs.push_back(path.string());
std::thread create_dir_thread(get_directories, std::cref(path), std::ref(dirs));
// Set up ncurses terminal interface
SCREEN *s;
FILE *out = stdout;
// Handle non-TTY output (e.g., when piped)
if(!isatty(fileno(stdout))) {
out = fopen("/dev/tty", "w");
setbuf(out, NULL);
}
s = newterm(NULL, out, stdin);
// Configure ncurses settings
scrollok(stdscr, true);
keypad(stdscr, true); // Enable function keys
cbreak(); // Disable line buffering
start_color();
use_default_colors();
// Set up color pairs for syntax highlighting
init_pair(1, COLOR_MAGENTA, -1); // Matched characters
init_pair(2, COLOR_WHITE, -1); // Normal characters
printw("Loading ...");
refresh();
clear();
// Wait for directory scanning to complete
create_dir_thread.join();
// Initialize search state
int c;
int entries = 10; // Number of results to display
int choice = 0; // Currently selected result
std::string selected_path = "";
std::string search = "";
std::set<char> char_set; // Characters to highlight
std::vector<std::string> result;
// Initial search and render
search_paths(dirs, search, result);
selected_path = render(result, choice, entries, char_set);
// Display UI instructions
printw("\n Choice Index: %d\n", choice);
printw(" Selected Choice: %s\n\n", selected_path.c_str());
printw(" Use left or right arrow keys to increase/decrease entries\n");
printw(" Use up / down / tab to navigate up or down\n");
printw(" Search 🔍: %s", search.c_str());
refresh();
// Main event loop
while (c != '\n') {
c = getch();
switch(c) {
case KEY_BACKSPACE:
// Remove last character from search
if (search.size() > 0) search.pop_back();
search_paths(dirs, search, result);
get_search_hashmap(search, char_set);
break;
case KEY_UP:
// Navigate up in results
choice--;
break;
case KEY_LEFT:
// Decrease number of displayed entries
if(entries > 0) entries--;
break;
case KEY_RIGHT:
// Increase number of displayed entries
entries++;
break;
case '\t':
case KEY_STAB:
case KEY_DOWN:
// Navigate down in results
choice++;
break;
case '\n':
case KEY_ENTER:
// Select current choice (exit loop)
break;
default:
// Add character to search string
search += c;
search_paths(dirs, search, result);
get_search_hashmap(search, char_set);
choice = 0; // Reset selection to top
break;
}
// Update display
clear();
selected_path = render(result, choice, entries, char_set);
printw("\n Choice Index: %d\n", choice);
printw(" Selected Choice: %s\n\n", selected_path.c_str());
printw(" Use left or right arrow keys to increase/decrease entries\n");
printw(" Use up / down / tab to navigate up or down\n");
printw(" Search 🔍: %s", search.c_str());
refresh();
}
// Clean up ncurses
endwin();
// Handle no selection case
if (selected_path == "No Directory Chosen") {
std::cout << selected_path << std::endl;
exit(1);
}
// Output the selected directory path
fs::path res(selected_path);
if(!fs::is_directory(res)) {
res.remove_filename(); // If file selected, use its directory
}
std::cout << fs::absolute(res).string() << std::endl;
return 0;
}