Version v0.3.49 - Working on directory naviagation

This commit is contained in:
2026-01-27 10:24:23 -04:00
parent 1e017d81b7
commit d8e65b1799
4 changed files with 313 additions and 15 deletions

290
src/ui.c
View File

@@ -184,7 +184,7 @@ int handle_encrypt_menu(void) {
printf("\nSelect encryption type:\n");
printf(" 1. Text message\n");
printf(" 2. File\n");
printf("Enter choice (1-2): ");
printf("Enter choice (1-2) or 'esc' to cancel: ");
char choice_input[10];
if (!fgets(choice_input, sizeof(choice_input), stdin)) {
@@ -192,6 +192,14 @@ int handle_encrypt_menu(void) {
return 1;
}
choice_input[strcspn(choice_input, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(choice_input)) {
printf("Returning to main menu...\n");
return 0;
}
int choice = atoi(choice_input);
if (choice == 1) {
@@ -213,7 +221,7 @@ int handle_encrypt_menu(void) {
printf("\nFile selection options:\n");
printf(" 1. Type file path directly\n");
printf(" 2. Use file manager\n");
printf("Enter choice (1-2): ");
printf("Enter choice (1-2) or 'esc' to cancel: ");
char file_choice[10];
char input_file[512];
@@ -223,25 +231,45 @@ int handle_encrypt_menu(void) {
return 1;
}
file_choice[strcspn(file_choice, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(file_choice)) {
printf("Returning to main menu...\n");
return 0;
}
if (atoi(file_choice) == 2) {
// Use file manager
if (launch_file_manager(".", input_file, sizeof(input_file)) != 0) {
printf("Falling back to manual file path entry.\n");
printf("Enter input file path: ");
printf("Enter input file path (or 'esc' to cancel): ");
if (!fgets(input_file, sizeof(input_file), stdin)) {
printf("Error: Failed to read input\n");
return 1;
}
input_file[strcspn(input_file, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(input_file)) {
printf("Returning to main menu...\n");
return 0;
}
}
} else {
// Direct file path input
printf("Enter input file path: ");
printf("Enter input file path (or 'esc' to cancel): ");
if (!fgets(input_file, sizeof(input_file), stdin)) {
printf("Error: Failed to read input\n");
return 1;
}
input_file[strcspn(input_file, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(input_file)) {
printf("Returning to main menu...\n");
return 0;
}
}
// Check if file exists
@@ -263,14 +291,24 @@ int handle_encrypt_menu(void) {
printf("\nSelect output format:\n");
printf(" 1. Binary (.otp) - preserves file permissions\n");
printf(" 2. ASCII (.otp.asc) - text-safe format\n");
printf("Enter choice (1-2): ");
printf("Enter choice (1-2) or 'esc' to cancel: ");
char format_input[10];
if (!fgets(format_input, sizeof(format_input), stdin)) {
printf("Error: Failed to read input\n");
free(selected_pad);
return 1;
}
format_input[strcspn(format_input, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(format_input)) {
printf("Returning to main menu...\n");
free(selected_pad);
return 0;
}
int ascii_armor = (atoi(format_input) == 2) ? 1 : 0;
// Generate default output filename with files directory and use enhanced input function
@@ -309,6 +347,7 @@ int handle_decrypt_menu(void) {
printf("\n");
print_centered_header("Smart Decrypt", 0);
printf("Enter encrypted data (paste ASCII armor), file path, or press Enter to browse files:\n");
printf("(Type 'esc' or 'q' to return to main menu)\n");
char input_line[MAX_LINE_LENGTH];
if (!fgets(input_line, sizeof(input_line), stdin)) {
@@ -319,6 +358,12 @@ int handle_decrypt_menu(void) {
// Remove newline
input_line[strcspn(input_line, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(input_line)) {
printf("Returning to main menu...\n");
return 0;
}
// Trim leading whitespace to handle pasted content better
char* trimmed_input = input_line;
while (*trimmed_input == ' ' || *trimmed_input == '\t') {
@@ -532,14 +577,24 @@ int handle_file_encrypt(void) {
printf("\nSelect output format:\n");
printf(" 1. Binary (.otp) - preserves file permissions\n");
printf(" 2. ASCII (.otp.asc) - text-safe format\n");
printf("Enter choice (1-2): ");
printf("Enter choice (1-2) or 'esc' to cancel: ");
char format_input[10];
if (!fgets(format_input, sizeof(format_input), stdin)) {
printf("Error: Failed to read input\n");
free(selected_pad);
return 1;
}
format_input[strcspn(format_input, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(format_input)) {
printf("Returning to main menu...\n");
free(selected_pad);
return 0;
}
int ascii_armor = (atoi(format_input) == 2) ? 1 : 0;
// Generate default output filename
@@ -564,6 +619,184 @@ int handle_file_encrypt(void) {
return result;
}
// Comparison function for qsort (case-insensitive)
static int compare_dir_strings(const void *a, const void *b) {
return strcasecmp(*(const char**)a, *(const char**)b);
}
// Function to get list of subdirectories
static int get_subdirs(const char *path, char ***subdirs) {
DIR *dir = opendir(path);
if (!dir) return 0;
int count = 0;
int capacity = 10;
*subdirs = malloc(capacity * sizeof(char*));
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
// Skip . and ..
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
if (count >= capacity) {
capacity *= 2;
*subdirs = realloc(*subdirs, capacity * sizeof(char*));
}
(*subdirs)[count++] = strdup(entry->d_name);
}
}
closedir(dir);
// Sort alphabetically
if (count > 0) {
qsort(*subdirs, count, sizeof(char*), compare_dir_strings);
}
return count;
}
// Function to get a single keypress without echo
static int getch_nav(void) {
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
// Navigate directories with arrow keys
static char* navigate_directory_interactive(void) {
char current_path[PATH_MAX];
char original_path[PATH_MAX];
getcwd(original_path, sizeof(original_path));
getcwd(current_path, sizeof(current_path));
char **subdirs = NULL;
int subdir_count = 0;
int current_index = 0;
printf("\nNavigate with arrow keys (UP/DOWN: cycle, RIGHT: enter, LEFT: back, ENTER: select, Q: cancel)\n");
printf("\033[?25l"); // Hide cursor
while (1) {
// Clear line and show current path
printf("\r\033[K%s", current_path);
// If we have subdirectories and an index, show current selection
if (subdir_count > 0 && current_index < subdir_count) {
printf("/%s", subdirs[current_index]);
}
fflush(stdout);
int ch = getch_nav();
// Handle arrow keys (they come as escape sequences)
if (ch == 27) { // ESC sequence
getch_nav(); // Skip '['
ch = getch_nav();
if (ch == 'A') { // UP arrow
// Load subdirs if not loaded
if (subdirs == NULL) {
subdir_count = get_subdirs(current_path, &subdirs);
current_index = 0;
} else if (subdir_count > 0) {
current_index = (current_index - 1 + subdir_count) % subdir_count;
}
}
else if (ch == 'B') { // DOWN arrow
// Load subdirs if not loaded
if (subdirs == NULL) {
subdir_count = get_subdirs(current_path, &subdirs);
current_index = 0;
} else if (subdir_count > 0) {
current_index = (current_index + 1) % subdir_count;
}
}
else if (ch == 'C') { // RIGHT arrow - go deeper
if (subdir_count > 0 && current_index < subdir_count) {
// Navigate into selected directory
char new_path[PATH_MAX];
snprintf(new_path, sizeof(new_path), "%s/%s", current_path, subdirs[current_index]);
if (chdir(new_path) == 0) {
getcwd(current_path, sizeof(current_path));
// Free old subdirs
for (int i = 0; i < subdir_count; i++) {
free(subdirs[i]);
}
free(subdirs);
subdirs = NULL;
// Load subdirs of new directory and show first one
subdir_count = get_subdirs(current_path, &subdirs);
current_index = 0;
}
}
}
else if (ch == 'D') { // LEFT arrow - go up
if (chdir("..") == 0) {
getcwd(current_path, sizeof(current_path));
// Free old subdirs
for (int i = 0; i < subdir_count; i++) {
free(subdirs[i]);
}
free(subdirs);
subdirs = NULL;
subdir_count = 0;
current_index = 0;
}
}
}
else if (ch == '\n' || ch == '\r') { // ENTER - confirm selection
// If a subdirectory is displayed, navigate into it first
if (subdir_count > 0 && current_index < subdir_count) {
char new_path[PATH_MAX];
snprintf(new_path, sizeof(new_path), "%s/%s", current_path, subdirs[current_index]);
if (chdir(new_path) == 0) {
getcwd(current_path, sizeof(current_path));
}
}
break;
}
else if (ch == 'q' || ch == 'Q') { // Q to quit
printf("\033[?25h\n"); // Show cursor
// Restore original directory
chdir(original_path);
// Clean up
for (int i = 0; i < subdir_count; i++) {
free(subdirs[i]);
}
free(subdirs);
return NULL;
}
}
printf("\033[?25h\n"); // Show cursor
// Clean up
for (int i = 0; i < subdir_count; i++) {
free(subdirs[i]);
}
free(subdirs);
// Restore original directory before returning
char* result = strdup(current_path);
chdir(original_path);
return result;
}
int handle_directory_encrypt(void) {
printf("\n");
print_centered_header("Directory Encrypt", 0);
@@ -571,8 +804,9 @@ int handle_directory_encrypt(void) {
// Directory selection options
printf("\nDirectory selection options:\n");
printf(" 1. Type directory path directly\n");
printf(" 2. Use file manager (navigate to directory)\n");
printf("Enter choice (1-2): ");
printf(" 2. Navigate with arrow keys\n");
printf(" 3. Use file manager (navigate to directory)\n");
printf("Enter choice (1-3) or 'esc' to cancel: ");
char choice_input[10];
char dir_path[512];
@@ -582,25 +816,59 @@ int handle_directory_encrypt(void) {
return 1;
}
if (atoi(choice_input) == 2) {
choice_input[strcspn(choice_input, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(choice_input)) {
printf("Returning to main menu...\n");
return 0;
}
int choice = atoi(choice_input);
if (choice == 2) {
// Use arrow key navigation
char* selected = navigate_directory_interactive();
if (!selected) {
printf("Directory selection cancelled.\n");
return 0;
}
strncpy(dir_path, selected, sizeof(dir_path) - 1);
dir_path[sizeof(dir_path) - 1] = '\0';
free(selected);
printf("Selected: %s\n", dir_path);
}
else if (choice == 3) {
// Use directory manager
if (launch_directory_manager(".", dir_path, sizeof(dir_path)) != 0) {
printf("Falling back to manual directory path entry.\n");
printf("Enter directory path to encrypt: ");
printf("Enter directory path to encrypt (or 'esc' to cancel): ");
if (!fgets(dir_path, sizeof(dir_path), stdin)) {
printf("Error: Failed to read input\n");
return 1;
}
dir_path[strcspn(dir_path, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(dir_path)) {
printf("Returning to main menu...\n");
return 0;
}
}
} else {
// Direct directory path input
printf("Enter directory path to encrypt: ");
printf("Enter directory path to encrypt (or 'esc' to cancel): ");
if (!fgets(dir_path, sizeof(dir_path), stdin)) {
printf("Error: Failed to read input\n");
return 1;
}
dir_path[strcspn(dir_path, "\n")] = 0;
// Check for ESC/cancel
if (is_escape_input(dir_path)) {
printf("Returning to main menu...\n");
return 0;
}
}
// Check if directory exists